Skip to content

Commit

Permalink
完善注释
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Dec 11, 2024
1 parent 64a91c1 commit fda7604
Show file tree
Hide file tree
Showing 24 changed files with 190 additions and 223 deletions.
18 changes: 7 additions & 11 deletions pubkey/bign/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ func init() {
AddNamedCurve(bign.P512v1(), oidNamedCurveBign512v1)
}

// 私钥 - 包装
// Marshal privateKey struct
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}

// 公钥 - 包装
// Marshal publicKey struct
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}

// 公钥信息 - 解析
// Parse publicKey struct
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
Expand All @@ -68,7 +68,7 @@ type ecPrivateKey struct {
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
}

// 包装公钥
// Marshal PublicKey to der
func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
Expand Down Expand Up @@ -105,7 +105,7 @@ func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
return asn1.Marshal(pkix)
}

// 解析公钥
// Parse PublicKey der
func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
var pki publicKeyInfo
rest, err := asn1.Unmarshal(derBytes, &pki)
Expand All @@ -121,7 +121,6 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
return
}

// 解析
keyData := &pki

oid := keyData.Algorithm.Algorithm
Expand Down Expand Up @@ -160,9 +159,7 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
return
}

// ====================

// 包装私钥
// Marshal PrivateKey to der
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
var privKey pkcs8

Expand All @@ -171,7 +168,6 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
return nil, errors.New("bign: unsupported bign curve")
}

// 创建数据
oidBytes, err := asn1.Marshal(oid)
if err != nil {
return nil, errors.New("bign: failed to marshal algo param: " + err.Error())
Expand All @@ -192,7 +188,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
return asn1.Marshal(privKey)
}

// 解析私钥
// Parse PrivateKey der
func ParsePrivateKey(derBytes []byte) (*PrivateKey, error) {
var privKey pkcs8
var err error
Expand Down
4 changes: 0 additions & 4 deletions pubkey/bip0340/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,3 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *P

return priv, nil
}

func bitsToBytes(bits int) int {
return (bits + 7) / 8
}
4 changes: 4 additions & 0 deletions pubkey/bip0340/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func rotl(x, n uint32) uint32 {
return bits.RotateLeft32(x, int(n))
}

func bitsToBytes(bits int) int {
return (bits + 7) / 8
}

func bigFromHex(s string) *big.Int {
b, ok := new(big.Int).SetString(s, 16)
if !ok {
Expand Down
18 changes: 8 additions & 10 deletions pubkey/dh/curve25519/key.go → pubkey/dh/curve25519/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import (
)

var (
// DH 公钥 oid
// DH PublicKey oid
oidPublicKeyDH = asn1.ObjectIdentifier{1, 3, 132, 1, 12}
)

// 私钥 - 包装
// Marshal privateKey struct
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}

// 公钥 - 包装
// Marshal publicKey struct
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}

// 公钥信息 - 解析
// Parse publicKey struct
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}

// 包装公钥
// Marshal PublicKey to der
func MarshalPublicKey(key *PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
Expand All @@ -56,7 +56,7 @@ func MarshalPublicKey(key *PublicKey) ([]byte, error) {
return asn1.Marshal(pkix)
}

// 解析公钥
// Parse PublicKey der
func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
var pki publicKeyInfo
rest, err := asn1.Unmarshal(derBytes, &pki)
Expand Down Expand Up @@ -86,9 +86,7 @@ func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
return pub, nil
}

// ====================

// 包装私钥
// Marshal PrivateKey to der
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
var privKey pkcs8

Expand All @@ -102,7 +100,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
return asn1.Marshal(privKey)
}

// 解析私钥
// Parse PrivateKey der
func ParsePrivateKey(derBytes []byte) (*PrivateKey, error) {
var privKey pkcs8
var err error
Expand Down
18 changes: 8 additions & 10 deletions pubkey/dh/dh/key.go → pubkey/dh/dh/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
// DH 公钥 oid
// DH PublicKey oid
oidPublicKeyDH = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 3, 1}
)

Expand All @@ -21,28 +21,28 @@ type dhAlgorithmParameters struct {
P, G *big.Int
}

// 私钥 - 包装
// Marshal privateKey struct
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}

// 公钥 - 包装
// Marshal publicKey struct
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}

// 公钥信息 - 解析
// Parse publicKey struct
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}

// 包装公钥
// Marshal PublicKey to der
func MarshalPublicKey(key *PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
Expand Down Expand Up @@ -79,7 +79,7 @@ func MarshalPublicKey(key *PublicKey) ([]byte, error) {
return asn1.Marshal(pkix)
}

// 解析公钥
// Parse PublicKey der
func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
var pki publicKeyInfo
rest, err := asn1.Unmarshal(derBytes, &pki)
Expand Down Expand Up @@ -135,9 +135,7 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
return
}

// ====================

// 包装私钥
// Marshal PrivateKey to der
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
var privKey pkcs8

Expand Down Expand Up @@ -170,7 +168,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
return asn1.Marshal(privKey)
}

// 解析私钥
// Parse PrivateKey der
func ParsePrivateKey(derBytes []byte) (*PrivateKey, error) {
var privKey pkcs8
var err error
Expand Down
18 changes: 8 additions & 10 deletions pubkey/dh/ecdh/key.go → pubkey/dh/ecdh/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
// DH 公钥 oid
// DH PublicKey oid
oidPublicKeyDH = asn1.ObjectIdentifier{1, 3, 132, 1, 12}

oidNamedCurveP224 = asn1.ObjectIdentifier{1, 3, 132, 0, 33}
Expand All @@ -21,28 +21,28 @@ var (
oidNamedCurveP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}
)

// 私钥 - 包装
// Marshal privateKey struct
type pkcs8 struct {
Version int
Algo pkix.AlgorithmIdentifier
PrivateKey []byte
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
}

// 公钥 - 包装
// Marshal publicKey struct
type pkixPublicKey struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}

// 公钥信息 - 解析
// Marshal publicKey struct
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}

// 包装公钥
// Marshal PublicKey to der
func MarshalPublicKey(key *PublicKey) ([]byte, error) {
var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier
Expand Down Expand Up @@ -75,7 +75,7 @@ func MarshalPublicKey(key *PublicKey) ([]byte, error) {
return asn1.Marshal(pkix)
}

// 解析公钥
// Parse PublicKey der
func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
var pki publicKeyInfo
rest, err := asn1.Unmarshal(derBytes, &pki)
Expand Down Expand Up @@ -119,9 +119,7 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
return
}

// ====================

// 包装私钥
// Marshal PrivateKey to der
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
var privKey pkcs8

Expand All @@ -148,7 +146,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
return asn1.Marshal(privKey)
}

// 解析私钥
// Parse PrivateKey der
func ParsePrivateKey(derBytes []byte) (*PrivateKey, error) {
var privKey pkcs8
var err error
Expand Down
Loading

0 comments on commit fda7604

Please sign in to comment.