Skip to content

Commit 2d595da

Browse files
committed
fixed
1 parent 131b5e2 commit 2d595da

File tree

13 files changed

+317
-7
lines changed

13 files changed

+317
-7
lines changed

cryptobin/ed448/ed448_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,23 @@ func Test_MakePublicKey(t *testing.T) {
161161
assertNoError(ed.Error(), "MakePublicKey")
162162
assertEqual(newPubkey, testPubkey, "MakePublicKey")
163163
}
164+
165+
func Test_CheckKeyString(t *testing.T) {
166+
ed := New().GenerateKey()
167+
168+
priString := ed.GetPrivateKeyString()
169+
pubString := ed.GetPublicKeyString()
170+
171+
cryptobin_test.NotEmpty(t, priString)
172+
cryptobin_test.NotEmpty(t, pubString)
173+
174+
pri := New().
175+
FromPrivateKeyString(priString).
176+
GetPrivateKey()
177+
pub := New().
178+
FromPublicKeyString(pubString).
179+
GetPublicKey()
180+
181+
cryptobin_test.Equal(t, ed.GetPrivateKey(), pri)
182+
cryptobin_test.Equal(t, ed.GetPublicKey(), pub)
183+
}

cryptobin/ed448/from.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ func FromPrivateKeySeed(seed []byte) ED448 {
136136

137137
// ==========
138138

139+
// PublicKey hex string
140+
func (this ED448) FromPublicKeyString(keyString string) ED448 {
141+
k, _ := encoding.HexDecode(keyString)
142+
143+
this.publicKey = ed448.PublicKey(k)
144+
145+
return this
146+
}
147+
148+
// PrivateKey hex string
149+
// private-key: 07e4********;
150+
func (this ED448) FromPrivateKeyString(keyString string) ED448 {
151+
k, _ := encoding.HexDecode(keyString)
152+
153+
this.privateKey = ed448.PrivateKey(k)
154+
155+
return this
156+
}
157+
158+
// ==========
159+
139160
// 字节
140161
func (this ED448) FromBytes(data []byte) ED448 {
141162
this.data = data

cryptobin/ed448/get.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ed448
22

33
import (
44
"github.com/deatil/go-cryptobin/pubkey/ed448"
5+
"github.com/deatil/go-cryptobin/tool/encoding"
56
)
67

78
// 获取 PrivateKey
@@ -14,11 +15,25 @@ func (this ED448) GetPrivateKeySeed() []byte {
1415
return this.privateKey.Seed()
1516
}
1617

18+
// get PrivateKey data hex string
19+
func (this ED448) GetPrivateKeyString() string {
20+
data := this.privateKey
21+
22+
return encoding.HexEncode([]byte(data))
23+
}
24+
1725
// 获取 PublicKey
1826
func (this ED448) GetPublicKey() ed448.PublicKey {
1927
return this.publicKey
2028
}
2129

30+
// get PublicKey data hex string
31+
func (this ED448) GetPublicKeyString() string {
32+
data := this.publicKey
33+
34+
return encoding.HexEncode([]byte(data))
35+
}
36+
2237
// 获取 Options
2338
func (this ED448) GetOptions() *Options {
2439
return this.options

cryptobin/eddsa/eddsa_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package eddsa
2+
3+
import (
4+
"testing"
5+
6+
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
7+
)
8+
9+
func Test_CreateKey(t *testing.T) {
10+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
11+
assertNoError := cryptobin_test.AssertNoErrorT(t)
12+
13+
obj := New().GenerateKey()
14+
15+
objPriKey := obj.CreatePrivateKey()
16+
priKey := objPriKey.ToKeyString()
17+
18+
assertNoError(objPriKey.Error(), "CreateKey-priKey")
19+
assertNotEmpty(priKey, "CreateKey-priKey")
20+
21+
objPriKeyEn := obj.CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
22+
priKeyEn := objPriKeyEn.ToKeyString()
23+
24+
assertNoError(objPriKeyEn.Error(), "CreateKey-priKeyEn")
25+
assertNotEmpty(priKeyEn, "CreateKey-priKeyEn")
26+
27+
objPubKey := obj.CreatePublicKey()
28+
pubKey := objPubKey.ToKeyString()
29+
30+
assertNoError(objPubKey.Error(), "CreateKey-pubKey")
31+
assertNotEmpty(pubKey, "CreateKey-pubKey")
32+
}
33+
34+
func Test_CheckKeyPair(t *testing.T) {
35+
assertTrue := cryptobin_test.AssertTrueT(t)
36+
assertNoError := cryptobin_test.AssertNoErrorT(t)
37+
38+
check := New().
39+
FromPublicKey([]byte(testPubkey)).
40+
FromPrivateKey([]byte(testPrikey))
41+
checkData := check.CheckKeyPair()
42+
43+
assertNoError(check.Error(), "CheckKeyPair")
44+
assertTrue(checkData, "CheckKeyPair")
45+
46+
// ==========
47+
48+
checkEn := New().
49+
FromPublicKey([]byte(testPubkeyEn)).
50+
FromPrivateKeyWithPassword([]byte(testPrikeyEn), "123")
51+
checkDataEn := checkEn.CheckKeyPair()
52+
53+
assertNoError(checkEn.Error(), "CheckKeyPair-EnPri")
54+
assertTrue(checkDataEn, "CheckKeyPair-EnPri")
55+
}
56+
57+
func Test_MakePublicKey(t *testing.T) {
58+
assertEqual := cryptobin_test.AssertEqualT(t)
59+
assertNoError := cryptobin_test.AssertNoErrorT(t)
60+
61+
ed := New().FromPrivateKey([]byte(testPrikey))
62+
newPubkey := ed.MakePublicKey().
63+
CreatePublicKey().
64+
ToKeyString()
65+
66+
assertNoError(ed.Error(), "MakePublicKey")
67+
assertEqual(newPubkey, testPubkey, "MakePublicKey")
68+
}
69+
70+
func Test_CheckKeyString(t *testing.T) {
71+
ed := New().GenerateKey()
72+
73+
priString := ed.GetPrivateKeyString()
74+
pubString := ed.GetPublicKeyString()
75+
76+
cryptobin_test.NotEmpty(t, priString)
77+
cryptobin_test.NotEmpty(t, pubString)
78+
79+
pri := New().
80+
FromPrivateKeyString(priString).
81+
GetPrivateKey()
82+
pub := New().
83+
FromPublicKeyString(pubString).
84+
GetPublicKey()
85+
86+
cryptobin_test.Equal(t, ed.GetPrivateKey(), pri)
87+
cryptobin_test.Equal(t, ed.GetPublicKey(), pub)
88+
}

cryptobin/eddsa/from.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ func FromPrivateKeySeed(seed []byte) EdDSA {
136136

137137
// ==========
138138

139+
// PublicKey hex string
140+
func (this EdDSA) FromPublicKeyString(keyString string) EdDSA {
141+
k, _ := encoding.HexDecode(keyString)
142+
143+
this.publicKey = ed25519.PublicKey(k)
144+
145+
return this
146+
}
147+
148+
// PrivateKey hex string
149+
// private-key: 07e4********;
150+
func (this EdDSA) FromPrivateKeyString(keyString string) EdDSA {
151+
k, _ := encoding.HexDecode(keyString)
152+
153+
this.privateKey = ed25519.PrivateKey(k)
154+
155+
return this
156+
}
157+
158+
// ==========
159+
139160
// 字节
140161
func (this EdDSA) FromBytes(data []byte) EdDSA {
141162
this.data = data

cryptobin/eddsa/get.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package eddsa
22

33
import (
44
"crypto/ed25519"
5+
6+
"github.com/deatil/go-cryptobin/tool/encoding"
57
)
68

79
// 获取 PrivateKey
@@ -14,11 +16,25 @@ func (this EdDSA) GetPrivateKeySeed() []byte {
1416
return this.privateKey.Seed()
1517
}
1618

19+
// get PrivateKey data hex string
20+
func (this EdDSA) GetPrivateKeyString() string {
21+
data := this.privateKey
22+
23+
return encoding.HexEncode([]byte(data))
24+
}
25+
1726
// 获取 PublicKey
1827
func (this EdDSA) GetPublicKey() ed25519.PublicKey {
1928
return this.publicKey
2029
}
2130

31+
// get PublicKey data hex string
32+
func (this EdDSA) GetPublicKeyString() string {
33+
data := this.publicKey
34+
35+
return encoding.HexEncode([]byte(data))
36+
}
37+
2238
// 获取 Options
2339
func (this EdDSA) GetOptions() *Options {
2440
return this.options

cryptobin/eddsa/sign_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@ import (
1010
var (
1111
testPrikey = `
1212
-----BEGIN PRIVATE KEY-----
13-
MC4CAQAwBQYDK2VwBCIEIOXADd7nzUp08BdkP9n9h/sFrjsi0xcK3gm8tHKBHCvK
13+
MC4CAQAwBQYDK2VwBCIEIFEs/Su+yy7RjO04AIsq7x+Bg9NRq1FFykJe7gPJXqWY
1414
-----END PRIVATE KEY-----
1515
`
16-
testPubkey = `
16+
testPubkey = `-----BEGIN PUBLIC KEY-----
17+
MCowBQYDK2VwAyEAvJgQNRwfWO53Hy2vSaBlz4wytmobPga00sRKaYenmgQ=
18+
-----END PUBLIC KEY-----
19+
`
20+
21+
testPrikeyEn = `
22+
-----BEGIN ENCRYPTED PRIVATE KEY-----
23+
MIGjMF8GCSqGSIb3DQEFDTBSMDEGCSqGSIb3DQEFDDAkBBA4MT4RO/TanOhtYFlU
24+
5YeVAgInEDAMBggqhkiG9w0CBwUAMB0GCWCGSAFlAwQBAgQQvSN7URYWp8xlhIaL
25+
t6K47wRALJ6ATPXrLQ8DGCAax2llMsB9TFJPAqnZX+lkdtzEELCaDpmkd/O9EYc3
26+
Fv7U+2E59pDpj3Vmen2xaKZ30xdpTQ==
27+
-----END ENCRYPTED PRIVATE KEY-----
28+
`
29+
testPubkeyEn = `
1730
-----BEGIN PUBLIC KEY-----
18-
MCowBQYDK2VwAyEA1NkD+0884Ol0mqyreYT+I6AA2y/rKDS+eIueB/vxMVc=
31+
MCowBQYDK2VwAyEAPZhFbGV49GRe/V0OHRimYBNT9EyL+fNYYKRXblB5VMw=
1932
-----END PUBLIC KEY-----
2033
`
34+
2135
)
2236

2337
func useEdDSASign(t *testing.T, opts *Options) {

elliptic/nums/nums.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nums
22

33
import (
4+
"sync"
45
"encoding/asn1"
56
"crypto/elliptic"
67
)
@@ -18,6 +19,9 @@ var (
1819
OIDNumsp512t1 = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 0, 6}
1920
)
2021

22+
// sync.Once variable to ensure initialization occurs only once
23+
var once sync.Once
24+
2125
// P256d1() returns a Curve which implements p256d1 of Microsoft's Nothing Up My Sleeve (NUMS)
2226
func P256d1() elliptic.Curve {
2327
once.Do(initAll)

elliptic/nums/nums_curves.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package nums
22

33
import (
4-
"sync"
54
"math/big"
65
"crypto/elliptic"
76
)
87

98
// see http://www.watersprings.org/pub/id/draft-black-numscurves-01.html
109

1110
var (
12-
once sync.Once
13-
1411
p256d1, p384d1, p512d1 *elliptic.CurveParams
1512
p256t1, p384t1, p512t1 *rcurve
1613
)

elliptic/nums/nums_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type testData struct {
3030
curve elliptic.Curve
3131
}
3232

33-
func Test_Brainpool(t *testing.T) {
33+
func Test_Curve(t *testing.T) {
3434
tests := []testData{
3535
{"P256d1", P256d1()},
3636
{"P384d1", P384d1()},

0 commit comments

Comments
 (0)