Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 12, 2024
1 parent 2248977 commit 99bc39f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 34 deletions.
6 changes: 3 additions & 3 deletions cryptobin/ecdh/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (this ECDH) ParseECDHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, erro
return nil, ErrNotPrivateKey
}

return cryptobin_ecdh.ToPrivateKey(pkey)
return cryptobin_ecdh.MarshalPrivateKey(pkey)
}

// 解析私钥带密码
Expand Down Expand Up @@ -175,7 +175,7 @@ func (this ECDH) ParseECDHPrivateKeyFromPEMWithPassword(key []byte, password str
return nil, ErrNotPrivateKey
}

return cryptobin_ecdh.ToPrivateKey(pkey)
return cryptobin_ecdh.MarshalPrivateKey(pkey)
}

// 解析公钥
Expand All @@ -201,5 +201,5 @@ func (this ECDH) ParseECDHPublicKeyFromPEM(key []byte) (crypto.PublicKey, error)
return nil, ErrNotPublicKey
}

return cryptobin_ecdh.ToPublicKey(pkey)
return cryptobin_ecdh.MarshalPublicKey(pkey)
}
50 changes: 50 additions & 0 deletions cryptobin/ecdsa/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,53 @@ func Test_PublicKey_Der(t *testing.T) {

assertEqual(res.GetPublicKey(), obj.GetPublicKey(), "PublicKey_Der-res")
}

var testPublicKeyForGet = `
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqktVUz5Og3mBcnhpnfWWSOhrZqO+
Vu0zCh5hkl/0r9vPzPeqGpHJv3eJw/zF+gZWxn2LvLcKkQTcGutSwVdVRQ==
-----END PUBLIC KEY-----
`
var testPrivateKeyForGet = `
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIGfqpFWW2kecvy/V0mxus+ZMuODGcqfyZVJMgBbWRhYJoAoGCCqGSM49
AwEHoUQDQgAEqktVUz5Og3mBcnhpnfWWSOhrZqO+Vu0zCh5hkl/0r9vPzPeqGpHJ
v3eJw/zF+gZWxn2LvLcKkQTcGutSwVdVRQ==
-----END EC PRIVATE KEY-----
`

func Test_PublicKeyForGet_Check(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)

xStringCheck := `aa4b55533e4e8379817278699df59648e86b66a3be56ed330a1e61925ff4afdb`
yStringCheck := `cfccf7aa1a91c9bf7789c3fcc5fa0656c67d8bbcb70a9104dc1aeb52c1575545`
xyStringCheck := `aa4b55533e4e8379817278699df59648e86b66a3be56ed330a1e61925ff4afdbcfccf7aa1a91c9bf7789c3fcc5fa0656c67d8bbcb70a9104dc1aeb52c1575545`
xyUncompressStringCheck := `04aa4b55533e4e8379817278699df59648e86b66a3be56ed330a1e61925ff4afdbcfccf7aa1a91c9bf7789c3fcc5fa0656c67d8bbcb70a9104dc1aeb52c1575545`
dStringCheck := `67eaa45596da479cbf2fd5d26c6eb3e64cb8e0c672a7f265524c8016d6461609`

xString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyXString()

yString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyYString()

xyString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyXYString()

xyUncompressString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyUncompressString()

dString := New().
FromPrivateKey([]byte(testPrivateKeyForGet)).
GetPrivateKeyDString()

assertEqual(xString, xStringCheck, "Test_PublicKeyForGet_x_Check")
assertEqual(yString, yStringCheck, "Test_PublicKeyForGet_y_Check")
assertEqual(xyString, xyStringCheck, "Test_PublicKeyForGet_xy_Check")
assertEqual(xyUncompressString, xyUncompressStringCheck, "Test_PublicKeyForGet_xyu_Check")
assertEqual(dString, dStringCheck, "Test_PublicKeyForGet_d_Check")
}
20 changes: 7 additions & 13 deletions cryptobin/ecdsa/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func (this ECDSA) GetPrivateKeyCurve() elliptic.Curve {
func (this ECDSA) GetPrivateKeyDString() string {
data := this.privateKey.D

dataHex := tool.HexEncode(data.Bytes())

return dataHex
return tool.HexEncode(data.Bytes())
}

// get PrivateKey data hex string
Expand All @@ -43,27 +41,23 @@ func (this ECDSA) GetPublicKeyCurve() elliptic.Curve {

// get PublicKey X hex string
func (this ECDSA) GetPublicKeyXString() string {
data := this.publicKey.X

dataHex := tool.HexEncode(data.Bytes())
x := this.publicKey.X

return dataHex
return tool.HexEncode(x.Bytes())
}

// get PublicKey Y hex string
func (this ECDSA) GetPublicKeyYString() string {
data := this.publicKey.Y
y := this.publicKey.Y

dataHex := tool.HexEncode(data.Bytes())

return dataHex
return tool.HexEncode(y.Bytes())
}

// get PublicKey X and Y Hex string
func (this ECDSA) GetPublicKeyXYString() string {
dataHex := this.GetPublicKeyXString() + this.GetPublicKeyYString()
key := elliptic.Marshal(this.publicKey.Curve, this.publicKey.X, this.publicKey.Y)

return dataHex
return tool.HexEncode(key[1:])
}

// get PublicKey Uncompress Hex string
Expand Down
4 changes: 2 additions & 2 deletions cryptobin/gost/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (this Gost) GetPrivateKeyBytes() []byte {
return nil
}

return gost.ToPrivateKey(privateKey)
return gost.MarshalPrivateKey(privateKey)

Check failure on line 19 in cryptobin/gost/get.go

View workflow job for this annotation

GitHub Actions / test

too many return values
}

// 获取 PublicKey
Expand All @@ -31,7 +31,7 @@ func (this Gost) GetPublicKeyBytes() []byte {
return nil
}

return gost.ToPublicKey(publicKey)
return gost.MarshalPublicKey(publicKey)

Check failure on line 34 in cryptobin/gost/get.go

View workflow job for this annotation

GitHub Actions / test

too many return values
}

// 获取 Curve
Expand Down
24 changes: 8 additions & 16 deletions cryptobin/sm2/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func (this SM2) GetPrivateKeyCurve() elliptic.Curve {
func (this SM2) GetPrivateKeyDString() string {
data := this.privateKey.D

dataHex := tool.HexEncode(data.Bytes())

return dataHex
return tool.HexEncode(data.Bytes())
}

// get PrivateKey data hex string
Expand All @@ -45,41 +43,35 @@ func (this SM2) GetPublicKeyCurve() elliptic.Curve {
func (this SM2) GetPublicKeyXString() string {
data := this.publicKey.X

dataHex := tool.HexEncode(data.Bytes())

return dataHex
return tool.HexEncode(data.Bytes())
}

// get PublicKey Y hex string
func (this SM2) GetPublicKeyYString() string {
data := this.publicKey.Y

dataHex := tool.HexEncode(data.Bytes())

return dataHex
return tool.HexEncode(data.Bytes())
}

// get PublicKey X and Y Hex string
func (this SM2) GetPublicKeyXYString() string {
dataHex := this.GetPublicKeyXString() + this.GetPublicKeyYString()
data := sm2.ToPublicKey(this.publicKey)

return dataHex
return tool.HexEncode(data[1:])
}

// get PublicKey Uncompress Hex string
func (this SM2) GetPublicKeyUncompressString() string {
dataHex := "04" + this.GetPublicKeyXString() + this.GetPublicKeyYString()
data := sm2.ToPublicKey(this.publicKey)

return dataHex
return tool.HexEncode(data)
}

// get PublicKey Compress Hex string
func (this SM2) GetPublicKeyCompressString() string {
data := sm2.Compress(this.publicKey)

dataHex := tool.HexEncode(data)

return dataHex
return tool.HexEncode(data)
}

// get key Data
Expand Down
50 changes: 50 additions & 0 deletions cryptobin/sm2/sm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,3 +1164,53 @@ func Test_SM2_EncryptASN1ECB(t *testing.T) {

assertEqual(deData, data, "Test_SM2_EncryptASN1ECB-Dedata")
}

var testPublicKeyForGet = `
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEd5tZ/XlQgV9AbJbU5JuzZimcK/LC
OX+xNwdI1XHHkIGl3W0VBmGRBK3VxkBSvp8tsGkZsxEmA7ngXyECzrDiuA==
-----END PUBLIC KEY-----
`
var testPrivateKeyForGet = `
-----BEGIN SM2 PRIVATE KEY-----
MHcCAQEEIAVunzkO+VYC1MFl3TfjjEHkc21eRBz+qRxbgEA6BP/FoAoGCCqBHM9V
AYItoUQDQgAEAnfcXztAc2zQ+uHuRlXuMohDdsncWxQFjrpxv5Ae3/PgH9vewt4A
oEvRqcwOBWtAXNDP6E74e5ocagfMUbq4hQ==
-----END SM2 PRIVATE KEY-----
`

func Test_PublicKeyForGet_Check(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)

xStringCheck := `779b59fd7950815f406c96d4e49bb366299c2bf2c2397fb1370748d571c79081`
yStringCheck := `a5dd6d1506619104add5c64052be9f2db06919b3112603b9e05f2102ceb0e2b8`
xyStringCheck := `779b59fd7950815f406c96d4e49bb366299c2bf2c2397fb1370748d571c79081a5dd6d1506619104add5c64052be9f2db06919b3112603b9e05f2102ceb0e2b8`
xyUncompressStringCheck := `04779b59fd7950815f406c96d4e49bb366299c2bf2c2397fb1370748d571c79081a5dd6d1506619104add5c64052be9f2db06919b3112603b9e05f2102ceb0e2b8`
dStringCheck := `056e9f390ef95602d4c165dd37e38c41e4736d5e441cfea91c5b80403a04ffc5`

xString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyXString()

yString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyYString()

xyString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyXYString()

xyUncompressString := New().
FromPublicKey([]byte(testPublicKeyForGet)).
GetPublicKeyUncompressString()

dString := New().
FromPrivateKey([]byte(testPrivateKeyForGet)).
GetPrivateKeyDString()

assertEqual(xString, xStringCheck, "Test_PublicKeyForGet_x_Check")
assertEqual(yString, yStringCheck, "Test_PublicKeyForGet_y_Check")
assertEqual(xyString, xyStringCheck, "Test_PublicKeyForGet_xy_Check")
assertEqual(xyUncompressString, xyUncompressStringCheck, "Test_PublicKeyForGet_xyu_Check")
assertEqual(dString, dStringCheck, "Test_PublicKeyForGet_d_Check")
}
File renamed without changes.

0 comments on commit 99bc39f

Please sign in to comment.