Skip to content

Commit dd257d7

Browse files
committed
fixed
1 parent 3e99c2c commit dd257d7

File tree

12 files changed

+267
-19
lines changed

12 files changed

+267
-19
lines changed

cryptobin/bign/bign_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package bign
22

33
import (
44
"testing"
5+
"encoding/asn1"
56
"crypto/rand"
7+
"crypto/elliptic"
68

79
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
810
)
@@ -181,6 +183,31 @@ func Test_PrivateKey_Bytes(t *testing.T) {
181183
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
182184
}
183185

186+
func Test_PrivateKey_Bytes_2(t *testing.T) {
187+
assertNoError := cryptobin_test.AssertNoErrorT(t)
188+
assertEqual := cryptobin_test.AssertEqualT(t)
189+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
190+
191+
oidNamedCurveP256 := asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
192+
AddNamedCurve(elliptic.P256(), oidNamedCurveP256)
193+
194+
obj := New().WithCurve(elliptic.P256()).GenerateKey()
195+
196+
assertNoError(obj.Error(), "PrivateKeyD")
197+
198+
priv := obj.GetPrivateKey()
199+
200+
d := priv.D.Bytes()
201+
202+
assertNotEmpty(d, "PrivateKey_Bytes")
203+
204+
xk := New().WithCurve(elliptic.P256()).FromPrivateKeyBytes(d)
205+
206+
assertNoError(xk.Error(), "PrivateKey_Bytes-xk")
207+
208+
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
209+
}
210+
184211
func Test_GetPrivateKeyString(t *testing.T) {
185212
assertNoError := cryptobin_test.AssertNoErrorT(t)
186213
assertEqual := cryptobin_test.AssertEqualT(t)

cryptobin/bign/with.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package bign
22

33
import (
4+
"encoding/asn1"
45
"crypto/elliptic"
56

67
"github.com/deatil/go-cryptobin/tool/hash"
78
"github.com/deatil/go-cryptobin/pubkey/bign"
89
ecbign "github.com/deatil/go-cryptobin/elliptic/bign"
910
)
1011

12+
// Add Named Curve
13+
func (this Bign) AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) Bign {
14+
bign.AddNamedCurve(curve, oid)
15+
return this
16+
}
17+
18+
// Add Named Curve
19+
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) Bign {
20+
return defaultBign.AddNamedCurve(curve, oid)
21+
}
22+
1123
// 设置 PrivateKey
1224
func (this Bign) WithPrivateKey(data *bign.PrivateKey) Bign {
1325
this.privateKey = data

cryptobin/bip0340/bip0340_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"crypto/rand"
66

7+
"github.com/deatil/go-cryptobin/elliptic/secp"
78
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
89
)
910

@@ -145,6 +146,30 @@ func Test_PrivateKey_Bytes(t *testing.T) {
145146
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
146147
}
147148

149+
func Test_PrivateKey_Bytes_2(t *testing.T) {
150+
assertNoError := cryptobin_test.AssertNoErrorT(t)
151+
assertEqual := cryptobin_test.AssertEqualT(t)
152+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
153+
154+
AddNamedCurve(secp.P192(), secp.OIDNamedCurveP192)
155+
156+
obj := New().WithCurve(secp.P192()).GenerateKey()
157+
158+
assertNoError(obj.Error(), "PrivateKeyD")
159+
160+
priv := obj.GetPrivateKey()
161+
162+
d := priv.D.Bytes()
163+
164+
assertNotEmpty(d, "PrivateKey_Bytes")
165+
166+
xk := New().WithCurve(secp.P192()).FromPrivateKeyBytes(d)
167+
168+
assertNoError(xk.Error(), "PrivateKey_Bytes-xk")
169+
170+
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
171+
}
172+
148173
func Test_GetPrivateKeyString(t *testing.T) {
149174
assertNoError := cryptobin_test.AssertNoErrorT(t)
150175
assertEqual := cryptobin_test.AssertEqualT(t)

cryptobin/bip0340/with.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package bip0340
22

33
import (
4+
"encoding/asn1"
45
"crypto/elliptic"
56

67
"github.com/deatil/go-cryptobin/tool/hash"
78
"github.com/deatil/go-cryptobin/pubkey/bip0340"
89
"github.com/deatil/go-cryptobin/elliptic/secp256k1"
910
)
1011

12+
// Add Named Curve
13+
func (this BIP0340) AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) BIP0340 {
14+
bip0340.AddNamedCurve(curve, oid)
15+
return this
16+
}
17+
18+
// Add Named Curve
19+
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) BIP0340 {
20+
return defaultBIP0340.AddNamedCurve(curve, oid)
21+
}
22+
1123
// 设置 PrivateKey
1224
func (this BIP0340) WithPrivateKey(data *bip0340.PrivateKey) BIP0340 {
1325
this.privateKey = data

cryptobin/ecdsa/ecdsa_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ import (
44
"testing"
55
"crypto/rand"
66

7-
"github.com/deatil/go-cryptobin/pubkey/ecdsa"
87
"github.com/deatil/go-cryptobin/elliptic/secp256k1"
98

109
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
1110
)
1211

13-
func init() {
14-
ecdsa.AddNamedCurve(secp256k1.S256(), secp256k1.OIDNamedCurveSecp256k1)
15-
}
16-
1712
var (
1813
prikeyRC2_40En = `
1914
-----BEGIN ENCRYPTED PRIVATE KEY-----
@@ -335,6 +330,8 @@ func Test_PrivateKey_Bytes_2(t *testing.T) {
335330
assertEqual := cryptobin_test.AssertEqualT(t)
336331
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
337332

333+
AddNamedCurve(secp256k1.S256(), secp256k1.OIDNamedCurveSecp256k1)
334+
338335
obj := New().WithCurve(secp256k1.S256()).GenerateKey()
339336

340337
assertNoError(obj.Error(), "PrivateKeyD")

cryptobin/ecdsa/with.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
package ecdsa
22

33
import (
4+
"encoding/asn1"
45
"crypto/ecdsa"
56
"crypto/elliptic"
67

78
"github.com/deatil/go-cryptobin/tool/hash"
9+
pubkey_ecdsa "github.com/deatil/go-cryptobin/pubkey/ecdsa"
810
)
911

10-
// 设置 PrivateKey
12+
// Add Named Curve
13+
func (this ECDSA) AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) ECDSA {
14+
pubkey_ecdsa.AddNamedCurve(curve, oid)
15+
return this
16+
}
17+
18+
// Add Named Curve
19+
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) ECDSA {
20+
return defaultECDSA.AddNamedCurve(curve, oid)
21+
}
22+
23+
// With PrivateKey
1124
func (this ECDSA) WithPrivateKey(data *ecdsa.PrivateKey) ECDSA {
1225
this.privateKey = data
1326

1427
return this
1528
}
1629

17-
// 设置 PublicKey
30+
// With PublicKey
1831
func (this ECDSA) WithPublicKey(data *ecdsa.PublicKey) ECDSA {
1932
this.publicKey = data
2033

2134
return this
2235
}
2336

24-
// 设置曲线类型
37+
// With curve
2538
func (this ECDSA) WithCurve(curve elliptic.Curve) ECDSA {
2639
this.curve = curve
2740

2841
return this
2942
}
3043

31-
// 设置曲线类型
32-
// 可选参数 [P521 | P384 | P256 | P224]
44+
// set curve
45+
// params [P521 | P384 | P256 | P224]
3346
func (this ECDSA) SetCurve(curve string) ECDSA {
3447
switch curve {
3548
case "P521":
@@ -45,14 +58,14 @@ func (this ECDSA) SetCurve(curve string) ECDSA {
4558
return this
4659
}
4760

48-
// 设置 hash 类型
61+
// With hash type
4962
func (this ECDSA) WithSignHash(hash HashFunc) ECDSA {
5063
this.signHash = hash
5164

5265
return this
5366
}
5467

55-
// 设置 hash 类型
68+
// With hash type
5669
func (this ECDSA) SetSignHash(name string) ECDSA {
5770
h, err := hash.GetHash(name)
5871
if err != nil {
@@ -64,45 +77,45 @@ func (this ECDSA) SetSignHash(name string) ECDSA {
6477
return this
6578
}
6679

67-
// 设置 data
80+
// With data
6881
func (this ECDSA) WithData(data []byte) ECDSA {
6982
this.data = data
7083

7184
return this
7285
}
7386

74-
// 设置 parsedData
87+
// With parsedData
7588
func (this ECDSA) WithParsedData(data []byte) ECDSA {
7689
this.parsedData = data
7790

7891
return this
7992
}
8093

81-
// 设置编码方式
94+
// With encoding
8295
func (this ECDSA) WithEncoding(encoding EncodingType) ECDSA {
8396
this.encoding = encoding
8497

8598
return this
8699
}
87100

88-
// 设置 ASN1 编码方式
101+
// encoding ASN1 encoding type
89102
func (this ECDSA) WithEncodingASN1() ECDSA {
90103
return this.WithEncoding(EncodingASN1)
91104
}
92105

93-
// 设置明文编码方式
106+
// encoding Bytes encoding type
94107
func (this ECDSA) WithEncodingBytes() ECDSA {
95108
return this.WithEncoding(EncodingBytes)
96109
}
97110

98-
// 设置验证结果
111+
// WithVerify
99112
func (this ECDSA) WithVerify(data bool) ECDSA {
100113
this.verify = data
101114

102115
return this
103116
}
104117

105-
// 设置错误
118+
// WithErrors
106119
func (this ECDSA) WithErrors(errs []error) ECDSA {
107120
this.Errors = errs
108121

cryptobin/ecgdsa/ecgdsa_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"crypto/rand"
66

7+
"github.com/deatil/go-cryptobin/elliptic/secp"
78
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
89
)
910

@@ -145,6 +146,30 @@ func Test_PrivateKey_Bytes(t *testing.T) {
145146
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
146147
}
147148

149+
func Test_PrivateKey_Bytes_2(t *testing.T) {
150+
assertNoError := cryptobin_test.AssertNoErrorT(t)
151+
assertEqual := cryptobin_test.AssertEqualT(t)
152+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
153+
154+
AddNamedCurve(secp.P192(), secp.OIDNamedCurveP192)
155+
156+
obj := New().WithCurve(secp.P192()).GenerateKey()
157+
158+
assertNoError(obj.Error(), "PrivateKeyD")
159+
160+
priv := obj.GetPrivateKey()
161+
162+
d := priv.D.Bytes()
163+
164+
assertNotEmpty(d, "PrivateKey_Bytes")
165+
166+
xk := New().WithCurve(secp.P192()).FromPrivateKeyBytes(d)
167+
168+
assertNoError(xk.Error(), "PrivateKey_Bytes-xk")
169+
170+
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
171+
}
172+
148173
func Test_GetPrivateKeyString(t *testing.T) {
149174
assertNoError := cryptobin_test.AssertNoErrorT(t)
150175
assertEqual := cryptobin_test.AssertEqualT(t)

cryptobin/ecgdsa/with.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package ecgdsa
22

33
import (
4+
"encoding/asn1"
45
"crypto/elliptic"
56

67
"github.com/deatil/go-cryptobin/tool/hash"
78
"github.com/deatil/go-cryptobin/pubkey/ecgdsa"
89
"github.com/deatil/go-cryptobin/elliptic/brainpool"
910
)
1011

12+
// Add Named Curve
13+
func (this ECGDSA) AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) ECGDSA {
14+
ecgdsa.AddNamedCurve(curve, oid)
15+
return this
16+
}
17+
18+
// Add Named Curve
19+
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) ECGDSA {
20+
return defaultECGDSA.AddNamedCurve(curve, oid)
21+
}
22+
1123
// 设置 PrivateKey
1224
func (this ECGDSA) WithPrivateKey(data *ecgdsa.PrivateKey) ECGDSA {
1325
this.privateKey = data

cryptobin/ecsdsa/ecsdsa_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"crypto/rand"
66

7+
"github.com/deatil/go-cryptobin/elliptic/secp"
78
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
89
)
910

@@ -145,6 +146,30 @@ func Test_PrivateKey_Bytes(t *testing.T) {
145146
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
146147
}
147148

149+
func Test_PrivateKey_Bytes_2(t *testing.T) {
150+
assertNoError := cryptobin_test.AssertNoErrorT(t)
151+
assertEqual := cryptobin_test.AssertEqualT(t)
152+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
153+
154+
AddNamedCurve(secp.P192(), secp.OIDNamedCurveP192)
155+
156+
obj := New().WithCurve(secp.P192()).GenerateKey()
157+
158+
assertNoError(obj.Error(), "PrivateKeyD")
159+
160+
priv := obj.GetPrivateKey()
161+
162+
d := priv.D.Bytes()
163+
164+
assertNotEmpty(d, "PrivateKey_Bytes")
165+
166+
xk := New().WithCurve(secp.P192()).FromPrivateKeyBytes(d)
167+
168+
assertNoError(xk.Error(), "PrivateKey_Bytes-xk")
169+
170+
assertEqual(xk.GetPrivateKey(), obj.GetPrivateKey(), "PrivateKey_Bytes-xk")
171+
}
172+
148173
func Test_GetPrivateKeyString(t *testing.T) {
149174
assertNoError := cryptobin_test.AssertNoErrorT(t)
150175
assertEqual := cryptobin_test.AssertEqualT(t)

0 commit comments

Comments
 (0)