Skip to content

Commit 8cca850

Browse files
committed
fixed
1 parent 8490456 commit 8cca850

File tree

16 files changed

+1135
-2
lines changed

16 files changed

+1135
-2
lines changed

cryptobin/crypto/cryptobin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
/**
9-
* 对称加密 / Cryptobin
9+
* Cryptobin
1010
*
1111
* @create 2022-3-19
1212
* @author deatil

cryptobin/ed521/check.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ed521
2+
3+
// Check KeyPair
4+
func (this ED521) CheckKeyPair() bool {
5+
pubKeyFromPriKey := this.MakePublicKey().
6+
CreatePublicKey().
7+
ToKeyString()
8+
9+
pubKeyFromPubKey := this.
10+
CreatePublicKey().
11+
ToKeyString()
12+
13+
if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" {
14+
return false
15+
}
16+
17+
if pubKeyFromPriKey == pubKeyFromPubKey {
18+
return true
19+
}
20+
21+
return false
22+
}

cryptobin/ed521/create.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package ed521
2+
3+
import (
4+
"errors"
5+
"crypto/rand"
6+
"encoding/pem"
7+
8+
"github.com/deatil/go-cryptobin/pkcs8"
9+
"github.com/deatil/go-cryptobin/pubkey/ed521"
10+
)
11+
12+
type (
13+
// 配置
14+
Opts = pkcs8.Opts
15+
// PBKDF2 配置
16+
PBKDF2Opts = pkcs8.PBKDF2Opts
17+
// Scrypt 配置
18+
ScryptOpts = pkcs8.ScryptOpts
19+
)
20+
21+
var (
22+
// 获取 Cipher 类型
23+
GetCipherFromName = pkcs8.GetCipherFromName
24+
// 获取 hash 类型
25+
GetHashFromName = pkcs8.GetHashFromName
26+
)
27+
28+
// 生成私钥 pem 数据
29+
// Create PrivateKey PEM data
30+
func (this ED521) CreatePrivateKey() ED521 {
31+
if this.privateKey == nil {
32+
err := errors.New("go-cryptobin/ed521: privateKey empty.")
33+
return this.AppendError(err)
34+
}
35+
36+
privateKeyBytes, err := ed521.MarshalPrivateKey(this.privateKey)
37+
if err != nil {
38+
return this.AppendError(err)
39+
}
40+
41+
privateBlock := &pem.Block{
42+
Type: "PRIVATE KEY",
43+
Bytes: privateKeyBytes,
44+
}
45+
46+
this.keyData = pem.EncodeToMemory(privateBlock)
47+
48+
return this
49+
}
50+
51+
// 生成 PKCS8 私钥带密码 pem 数据
52+
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
53+
func (this ED521) CreatePrivateKeyWithPassword(password string, opts ...any) ED521 {
54+
if this.privateKey == nil {
55+
err := errors.New("go-cryptobin/ed521: privateKey empty.")
56+
return this.AppendError(err)
57+
}
58+
59+
opt, err := pkcs8.ParseOpts(opts...)
60+
if err != nil {
61+
return this.AppendError(err)
62+
}
63+
64+
// 生成私钥
65+
privateKeyBytes, err := ed521.MarshalPrivateKey(this.privateKey)
66+
if err != nil {
67+
return this.AppendError(err)
68+
}
69+
70+
// 生成加密数据
71+
privateBlock, err := pkcs8.EncryptPEMBlock(
72+
rand.Reader,
73+
"ENCRYPTED PRIVATE KEY",
74+
privateKeyBytes,
75+
[]byte(password),
76+
opt,
77+
)
78+
if err != nil {
79+
return this.AppendError(err)
80+
}
81+
82+
this.keyData = pem.EncodeToMemory(privateBlock)
83+
84+
return this
85+
}
86+
87+
// 生成公钥 pem 数据
88+
func (this ED521) CreatePublicKey() ED521 {
89+
if this.publicKey == nil {
90+
err := errors.New("go-cryptobin/ed521: publicKey empty.")
91+
return this.AppendError(err)
92+
}
93+
94+
publicKeyBytes, err := ed521.MarshalPublicKey(this.publicKey)
95+
if err != nil {
96+
return this.AppendError(err)
97+
}
98+
99+
publicBlock := &pem.Block{
100+
Type: "PUBLIC KEY",
101+
Bytes: publicKeyBytes,
102+
}
103+
104+
this.keyData = pem.EncodeToMemory(publicBlock)
105+
106+
return this
107+
}

cryptobin/ed521/ed521.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ed521
2+
3+
import (
4+
"crypto"
5+
6+
"github.com/deatil/go-cryptobin/pubkey/ed521"
7+
)
8+
9+
type (
10+
// 设置
11+
Options = ed521.Options
12+
)
13+
14+
const (
15+
SchemeED521 = ed521.ED521
16+
SchemeED521Ph = ed521.ED521Ph
17+
)
18+
19+
/**
20+
* ED521
21+
*
22+
* @create 2025-12-2
23+
* @author deatil
24+
*/
25+
type ED521 struct {
26+
// PrivateKey
27+
privateKey *ed521.PrivateKey
28+
29+
// PublicKey
30+
publicKey *ed521.PublicKey
31+
32+
// Options
33+
options *Options
34+
35+
// [PrivateKey/PublicKey]data
36+
keyData []byte
37+
38+
// input data
39+
data []byte
40+
41+
// parsed data
42+
parsedData []byte
43+
44+
// verify data
45+
verify bool
46+
47+
// error list
48+
Errors []error
49+
}
50+
51+
// NewED521
52+
func NewED521() ED521 {
53+
return ED521{
54+
options: &Options{
55+
Hash: crypto.Hash(0),
56+
Context: "",
57+
Scheme: SchemeED521,
58+
},
59+
verify: false,
60+
Errors: make([]error, 0),
61+
}
62+
}
63+
64+
// NewED521
65+
func New() ED521 {
66+
return NewED521()
67+
}
68+
69+
var (
70+
// default New ED521
71+
defaultED521 = NewED521()
72+
)

cryptobin/ed521/ed521_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package ed521
2+
3+
import (
4+
"fmt"
5+
"crypto"
6+
"testing"
7+
8+
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
9+
)
10+
11+
var (
12+
testPrikey = `-----BEGIN PRIVATE KEY-----
13+
MFcCAQAwDgYKKwYBBAGC3CwCAQYABEIAKG1VJuFCsAxsDCr6uBrNfQaYCPOvGu7D
14+
kLJ/CQkUhCmg0DKrQrhLWwwp6TXUekXr5LOlo0rzl7frT+g46YSbiQI=
15+
-----END PRIVATE KEY-----
16+
`
17+
testPubkey = `-----BEGIN PUBLIC KEY-----
18+
MFUwDgYKKwYBBAGC3CwCAQYAA0MAmni8ls4g54Kqpk5gMM7/dscHeV6nm4Eq8zY3
19+
OX1BYZZaEp6KX4P7PhwHv5ZYhHZK23bH4O483nRnAkitNHNKMLoA
20+
-----END PUBLIC KEY-----
21+
`
22+
23+
testPrikeyEn = `-----BEGIN ENCRYPTED PRIVATE KEY-----
24+
MIHDMF8GCSqGSIb3DQEFDTBSMDEGCSqGSIb3DQEFDDAkBBAUciNePUdR/1Yam7+s
25+
ESzEAgInEDAMBggqhkiG9w0CCQUAMB0GCWCGSAFlAwQBKgQQPSnhje7xky4oXvb4
26+
JRNkqARgHbRbcy46pxZL3nMYvZDHrgAYmdzSMaDNXqtASgKtc2YMH0vi/3Ej9ai9
27+
OWfCAC6k7Jf8QmH4SSV73ubvcpi+WXGMqqAnPh52HWN7apIyrUvBRz2n2bz5EEBp
28+
FbfFSxL1
29+
-----END ENCRYPTED PRIVATE KEY-----
30+
`
31+
testPubkeyEn = `-----BEGIN PUBLIC KEY-----
32+
MFUwDgYKKwYBBAGC3CwCAQYAA0MAlFBP2O8rujvdW3bE3SoLJfGe3P33p0RyHZkW
33+
Rvx91ZZQXrNe4vEgylU2BcQm3nXMm2Z8rB6fWr9fEKu57WU6ENgB
34+
-----END PUBLIC KEY-----
35+
`
36+
)
37+
38+
func testED521Sign(t *testing.T, opts *Options) {
39+
assertTrue := cryptobin_test.AssertTrueT(t)
40+
assertNoError := cryptobin_test.AssertNoErrorT(t)
41+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
42+
43+
data := []byte("test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass3333333333333333333333333333333333333333333333333333test-pa2222222222222222222222222222222222222222222sstest-passt111111111111111111111111111111111est-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passt-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass")
44+
45+
hashed := FromBytes(data).
46+
FromPrivateKey([]byte(testPrikey)).
47+
WithOptions(opts).
48+
Sign()
49+
hashedData := hashed.ToBase64String()
50+
51+
assertNoError(hashed.Error(), "ED521Sign-Sign")
52+
assertNotEmpty(hashedData, "ED521Sign-Sign")
53+
54+
// ===
55+
56+
dehashed := FromBase64String(hashedData).
57+
FromPublicKey([]byte(testPubkey)).
58+
WithOptions(opts).
59+
Verify(data)
60+
dehashedVerify := dehashed.ToVerify()
61+
62+
assertNoError(dehashed.Error(), "ED521Sign-Verify")
63+
assertTrue(dehashedVerify, "ED521Sign-Verify")
64+
}
65+
66+
func Test_ED521Sign(t *testing.T) {
67+
ctx := "ase3ertygfa1"
68+
69+
optses := []*Options{
70+
&Options{
71+
Hash: crypto.Hash(0),
72+
Context: ctx,
73+
Scheme: SchemeED521,
74+
},
75+
&Options{
76+
Hash: crypto.Hash(0),
77+
Context: ctx,
78+
Scheme: SchemeED521Ph,
79+
},
80+
&Options{
81+
Hash: crypto.Hash(0),
82+
Context: "",
83+
Scheme: SchemeED521,
84+
},
85+
&Options{
86+
Hash: crypto.Hash(0),
87+
Context: "",
88+
Scheme: SchemeED521Ph,
89+
},
90+
}
91+
92+
i := 1
93+
for _, opts := range optses {
94+
t.Run(fmt.Sprintf("ED521 index %d", i), func(t *testing.T) {
95+
testED521Sign(t, opts)
96+
})
97+
98+
i += 1
99+
}
100+
}
101+
102+
func Test_CreateKey(t *testing.T) {
103+
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)
104+
assertNoError := cryptobin_test.AssertNoErrorT(t)
105+
106+
obj := New().GenerateKey()
107+
108+
objPriKey := obj.CreatePrivateKey()
109+
priKey := objPriKey.ToKeyString()
110+
111+
assertNoError(objPriKey.Error(), "CreateKey-priKey")
112+
assertNotEmpty(priKey, "CreateKey-priKey")
113+
114+
objPriKeyEn := obj.CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
115+
priKeyEn := objPriKeyEn.ToKeyString()
116+
117+
assertNoError(objPriKeyEn.Error(), "CreateKey-priKeyEn")
118+
assertNotEmpty(priKeyEn, "CreateKey-priKeyEn")
119+
120+
objPubKey := obj.CreatePublicKey()
121+
pubKey := objPubKey.ToKeyString()
122+
123+
assertNoError(objPubKey.Error(), "CreateKey-pubKey")
124+
assertNotEmpty(pubKey, "CreateKey-pubKey")
125+
126+
// t.Errorf("pri: %s, pub: %s, prien: %s \n", priKey, pubKey, priKeyEn)
127+
}
128+
129+
func Test_CheckKeyPair(t *testing.T) {
130+
assertTrue := cryptobin_test.AssertTrueT(t)
131+
assertNoError := cryptobin_test.AssertNoErrorT(t)
132+
133+
check := New().
134+
FromPublicKey([]byte(testPubkey)).
135+
FromPrivateKey([]byte(testPrikey))
136+
checkData := check.CheckKeyPair()
137+
138+
assertNoError(check.Error(), "CheckKeyPair")
139+
assertTrue(checkData, "CheckKeyPair")
140+
141+
// ==========
142+
143+
checkEn := New().
144+
FromPublicKey([]byte(testPubkeyEn)).
145+
FromPrivateKeyWithPassword([]byte(testPrikeyEn), "123")
146+
checkDataEn := checkEn.CheckKeyPair()
147+
148+
assertNoError(checkEn.Error(), "CheckKeyPair-EnPri")
149+
assertTrue(checkDataEn, "CheckKeyPair-EnPri")
150+
}
151+
152+
func Test_MakePublicKey(t *testing.T) {
153+
assertEqual := cryptobin_test.AssertEqualT(t)
154+
assertNoError := cryptobin_test.AssertNoErrorT(t)
155+
156+
ed := New().FromPrivateKey([]byte(testPrikey))
157+
newPubkey := ed.MakePublicKey().
158+
CreatePublicKey().
159+
ToKeyString()
160+
161+
assertNoError(ed.Error(), "MakePublicKey")
162+
assertEqual(newPubkey, testPubkey, "MakePublicKey")
163+
}
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+
}

0 commit comments

Comments
 (0)