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()},

elliptic/tom/tom.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tom
2+
3+
import (
4+
"sync"
5+
"encoding/asn1"
6+
"crypto/elliptic"
7+
)
8+
9+
var (
10+
OIDTom = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1}
11+
12+
OIDNamedCurveTom256 = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1, 1}
13+
OIDNamedCurveTom384 = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1, 2}
14+
)
15+
16+
// sync.Once variable to ensure initialization occurs only once
17+
var once sync.Once
18+
19+
func P256() elliptic.Curve {
20+
once.Do(initAll)
21+
return p256
22+
}
23+
24+
func P384() elliptic.Curve {
25+
once.Do(initAll)
26+
return p384
27+
}

elliptic/tom/tom_curves.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tom
2+
3+
import (
4+
"math/big"
5+
"crypto/elliptic"
6+
)
7+
8+
var p256, p384 *elliptic.CurveParams
9+
10+
func initAll() {
11+
initP256()
12+
initP384()
13+
}
14+
15+
func initP256() {
16+
p256 = &elliptic.CurveParams{
17+
P: bigFromHex("FFFFFFFF0000000100000000000000017E72B42B30E7317793135661B1C4B117"),
18+
N: bigFromHex("FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"),
19+
B: bigFromHex("B441071B12F4A0366FB552F8E21ED4AC36B06ACEEB354224863E60F20219FC56"),
20+
Gx: bigFromHex("03"),
21+
Gy: bigFromHex("5A6DD32DF58708E64E97345CBE66600DECD9D538A351BB3C30B4954925B1F02D"),
22+
BitSize: 256,
23+
Name: "Tom-256",
24+
}
25+
}
26+
27+
func initP384() {
28+
p384 = &elliptic.CurveParams{
29+
P: bigFromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"),
30+
N: bigFromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"),
31+
B: bigFromHex("B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"),
32+
Gx: bigFromHex("AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7"),
33+
Gy: bigFromHex("3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"),
34+
BitSize: 384,
35+
Name: "Tom-384",
36+
}
37+
}
38+
39+
func bigFromHex(s string) (i *big.Int) {
40+
i = new(big.Int)
41+
i.SetString(s, 16)
42+
43+
return
44+
}

0 commit comments

Comments
 (0)