Skip to content

Commit e174f66

Browse files
committed
优化 elgamal
1 parent 0d5ca2f commit e174f66

File tree

20 files changed

+263
-185
lines changed

20 files changed

+263
-185
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ go-cryptobin 是 go 的常用加密解密库
1111
* 对称加密解密(Aes/Des/TripleDes/SM4/Tea/Twofish/Xts)
1212
* 对称加密解密模式(ECB/CBC/PCBC/CFB/NCFB/OFB/NOFB/CTR/GCM/CCM)
1313
* 对称加密解密补码(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO97971Padding/ISO7816_4Padding/PBOC2Padding/TBCPadding/PKCS1Padding)
14-
* 非对称加密解密(RSA/SM2/EIGamal
15-
* 非对称签名验证(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/EIGamal/ED448/Gost)
14+
* 非对称加密解密(RSA/SM2/ElGamal
15+
* 非对称签名验证(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/ElGamal/ED448/Gost)
1616
* 默认 `Aes`, `ECB`, `NoPadding`
1717

1818

README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ go-cryptobin is a go encrypt or decrypt library
1111
* sym encrypts(Aes/Des/TripleDes/SM4/Tea/Twofish/Xts)
1212
* encrypt mode(ECB/CBC/PCBC/CFB/NCFB/OFB/NOFB/CTR/GCM/CCM)
1313
* encrypt padding(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO97971Padding/ISO7816_4Padding/PBOC2Padding/TBCPadding/PKCS1Padding)
14-
* asym encrypts(Aes(RSA/SM2/EIGamal
15-
* asym sign(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/EIGamal/ED448/Gost)
14+
* asym encrypts(Aes(RSA/SM2/ElGamal
15+
* asym sign(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/ElGamal/ED448/Gost)
1616
* default setting `Aes`, `ECB`, `NoPadding`
1717

1818

cryptobin/elgamal/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package elgamal
22

33
// 检测公钥私钥是否匹配
4-
func (this EIGamal) CheckKeyPair() bool {
4+
func (this ElGamal) CheckKeyPair() bool {
55
// 私钥导出的公钥
66
pubKeyFromPriKey := this.MakePublicKey().
77
CreatePublicKey().

cryptobin/elgamal/create.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ var (
2929
// 生成私钥 pem 数据
3030
// elgamal := New().GenerateKey("L2048N256")
3131
// priKey := elgamal.CreatePrivateKey().ToKeyString()
32-
func (this EIGamal) CreatePrivateKey() EIGamal {
32+
func (this ElGamal) CreatePrivateKey() ElGamal {
3333
return this.CreatePKCS1PrivateKey()
3434
}
3535

3636
// 生成私钥带密码 pem 数据
3737
// CreatePrivateKeyWithPassword("123", "AES256CBC")
3838
// PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC
39-
func (this EIGamal) CreatePrivateKeyWithPassword(password string, opts ...string) EIGamal {
39+
func (this ElGamal) CreatePrivateKeyWithPassword(password string, opts ...string) ElGamal {
4040
return this.CreatePKCS1PrivateKeyWithPassword(password, opts...)
4141
}
4242

4343
// 生成公钥 pem 数据
44-
func (this EIGamal) CreatePublicKey() EIGamal {
44+
func (this ElGamal) CreatePublicKey() ElGamal {
4545
return this.CreatePKCS1PublicKey()
4646
}
4747

4848
// ==========
4949

5050
// 生成 pkcs1 私钥 pem 数据
51-
func (this EIGamal) CreatePKCS1PrivateKey() EIGamal {
51+
func (this ElGamal) CreatePKCS1PrivateKey() ElGamal {
5252
if this.privateKey == nil {
5353
err := errors.New("privateKey empty.")
5454
return this.AppendError(err)
@@ -60,7 +60,7 @@ func (this EIGamal) CreatePKCS1PrivateKey() EIGamal {
6060
}
6161

6262
privateBlock := &pem.Block{
63-
Type: "EIGamal PRIVATE KEY",
63+
Type: "ElGamal PRIVATE KEY",
6464
Bytes: privateKeyBytes,
6565
}
6666

@@ -72,7 +72,7 @@ func (this EIGamal) CreatePKCS1PrivateKey() EIGamal {
7272
// 生成 pkcs1 私钥带密码 pem 数据
7373
// CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC")
7474
// PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC
75-
func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) EIGamal {
75+
func (this ElGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) ElGamal {
7676
if this.privateKey == nil {
7777
err := errors.New("privateKey empty.")
7878
return this.AppendError(err)
@@ -99,7 +99,7 @@ func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...s
9999
// 生成加密数据
100100
privateBlock, err := pkcs1.EncryptPEMBlock(
101101
rand.Reader,
102-
"EIGamal PRIVATE KEY",
102+
"ElGamal PRIVATE KEY",
103103
privateKeyBytes,
104104
[]byte(password),
105105
cipher,
@@ -114,7 +114,7 @@ func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...s
114114
}
115115

116116
// 生成 pkcs1 公钥 pem 数据
117-
func (this EIGamal) CreatePKCS1PublicKey() EIGamal {
117+
func (this ElGamal) CreatePKCS1PublicKey() ElGamal {
118118
if this.publicKey == nil {
119119
err := errors.New("publicKey empty.")
120120
return this.AppendError(err)
@@ -126,7 +126,7 @@ func (this EIGamal) CreatePKCS1PublicKey() EIGamal {
126126
}
127127

128128
publicBlock := &pem.Block{
129-
Type: "EIGamal PUBLIC KEY",
129+
Type: "ElGamal PUBLIC KEY",
130130
Bytes: publicKeyBytes,
131131
}
132132

@@ -138,7 +138,7 @@ func (this EIGamal) CreatePKCS1PublicKey() EIGamal {
138138
// ==========
139139

140140
// 生成 pkcs8 私钥 pem 数据
141-
func (this EIGamal) CreatePKCS8PrivateKey() EIGamal {
141+
func (this ElGamal) CreatePKCS8PrivateKey() ElGamal {
142142
if this.privateKey == nil {
143143
err := errors.New("privateKey empty.")
144144
return this.AppendError(err)
@@ -161,7 +161,7 @@ func (this EIGamal) CreatePKCS8PrivateKey() EIGamal {
161161

162162
// 生成 PKCS8 私钥带密码 pem 数据
163163
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")
164-
func (this EIGamal) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) EIGamal {
164+
func (this ElGamal) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) ElGamal {
165165
if this.privateKey == nil {
166166
err := errors.New("privateKey empty.")
167167
return this.AppendError(err)
@@ -196,7 +196,7 @@ func (this EIGamal) CreatePKCS8PrivateKeyWithPassword(password string, opts ...a
196196
}
197197

198198
// 生成公钥 pem 数据
199-
func (this EIGamal) CreatePKCS8PublicKey() EIGamal {
199+
func (this ElGamal) CreatePKCS8PublicKey() ElGamal {
200200
if this.publicKey == nil {
201201
err := errors.New("publicKey empty.")
202202
return this.AppendError(err)
@@ -220,7 +220,7 @@ func (this EIGamal) CreatePKCS8PublicKey() EIGamal {
220220
// ====================
221221

222222
// 生成私钥 xml 数据
223-
func (this EIGamal) CreateXMLPrivateKey() EIGamal {
223+
func (this ElGamal) CreateXMLPrivateKey() ElGamal {
224224
if this.privateKey == nil {
225225
err := errors.New("privateKey empty.")
226226
return this.AppendError(err)
@@ -237,7 +237,7 @@ func (this EIGamal) CreateXMLPrivateKey() EIGamal {
237237
}
238238

239239
// 生成公钥 xml 数据
240-
func (this EIGamal) CreateXMLPublicKey() EIGamal {
240+
func (this ElGamal) CreateXMLPublicKey() ElGamal {
241241
if this.publicKey == nil {
242242
err := errors.New("publicKey empty.")
243243
return this.AppendError(err)

cryptobin/elgamal/elgamal.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ type (
1313
)
1414

1515
/**
16-
* EIGamal
16+
* ElGamal
1717
*
1818
* @create 2023-6-22
1919
* @author deatil
2020
*/
21-
type EIGamal struct {
21+
type ElGamal struct {
2222
// 私钥
2323
privateKey *elgamal.PrivateKey
2424

@@ -45,20 +45,20 @@ type EIGamal struct {
4545
}
4646

4747
// 构造函数
48-
func NewEIGamal() EIGamal {
49-
return EIGamal{
48+
func NewElGamal() ElGamal {
49+
return ElGamal{
5050
signHash: sha256.New,
5151
verify: false,
5252
Errors: make([]error, 0),
5353
}
5454
}
5555

5656
// 构造函数
57-
func New() EIGamal {
58-
return NewEIGamal()
57+
func New() ElGamal {
58+
return NewElGamal()
5959
}
6060

6161
var (
6262
// 默认
63-
defaultEIGamal = NewEIGamal()
63+
defaultElGamal = NewElGamal()
6464
)

cryptobin/elgamal/elgamal_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99

1010
var (
1111
prikeyXML = `
12-
<EIGamalKeyValue>
13-
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
12+
<ElGamalKeyValue>
1413
<P>9W35RbKvFgfHndG9wVvFDMDw86BClpDk6kdeGr1ygLc=</P>
14+
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
1515
<Y>120jHKCdPWjLGrqH3HiCZ2GezWyEjfEIPBMhULymfzM=</Y>
1616
<X>BjtroR34tS5cvF5YNJaxmOjGDas43wKFunHCYS4P6CQ=</X>
17-
</EIGamalKeyValue>
17+
</ElGamalKeyValue>
1818
`
1919
pubkeyXML = `
20-
<EIGamalKeyValue>
21-
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
20+
<ElGamalKeyValue>
2221
<P>9W35RbKvFgfHndG9wVvFDMDw86BClpDk6kdeGr1ygLc=</P>
22+
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
2323
<Y>120jHKCdPWjLGrqH3HiCZ2GezWyEjfEIPBMhULymfzM=</Y>
24-
</EIGamalKeyValue>
24+
</ElGamalKeyValue>
2525
`
2626
)
2727

@@ -137,28 +137,30 @@ func Test_GenerateKeyPKCS8(t *testing.T) {
137137

138138
var (
139139
pkcs1Prikey = `
140-
-----BEGIN EIGamal PRIVATE KEY-----
141-
MIGMAgEAAiEAz5k+pG+6n9UNyvAcbGLEcTfJ3NN8XWBpc27zqWbRY/cCIDXGstaZ
142-
qIVrSp3hnXtTmu/8rcbfFmhui7+Ubb37ldUrAiA1ozJHvzXn5m3cMs++nV2oT8Ij
143-
+c8T6Sq/5txnIQXytgIgLCd+/uxQSB05Y2xWtzz9UTVBC9Sj9uh2k5ZZlqfY8v8=
144-
-----END EIGamal PRIVATE KEY-----
140+
-----BEGIN ElGamal PRIVATE KEY-----
141+
MIGuAgEAAiEA9cZBJDM0L+NCYt4vlsMg+HdufcNdUF7Z7W2OtGogl9cCIFdwSXyl
142+
APOj8NYVTmMwSxZMZxoXr25rL884i3vQYOd5AiB64yCSGZoX8aExbxfLYZB8O7c+
143+
4a6oL2z2tsdaNRBL6wIgQTk/bOUCzJNfknsX+LvmpdQ9GVCd9ZiuR9t9nNi9VFsC
144+
IFNu9DFgP10mqzkn/fB/bBrV5Xc7UPhgceZneZ5yzCDk
145+
-----END ElGamal PRIVATE KEY-----
145146
`
146147
pkcs1EnPrikey = `
147-
-----BEGIN EIGamal PRIVATE KEY-----
148+
-----BEGIN ElGamal PRIVATE KEY-----
148149
Proc-Type: 4,ENCRYPTED
149-
DEK-Info: DES-EDE3-CBC,a26bd66b277099bd
150+
DEK-Info: DES-EDE3-CBC,b1e4e15253a2dd4f
150151
151-
YFVfbgFspb0Pr21832B3xwm1LUrf2SDu4K/Wc8MtD5Is4NCzOatQw8kYA5qkIf9p
152-
6CyR57wtsJCakIw3E0DKSZYNe3tQbfxRK+ZUgYAda/E0UpiLe9j/WJRQ8G9/DAOi
153-
mXXdz6pq4omiDVh3DcSlQJskJvoM6KE5OgJh81iuP7jbiX109ayEYPNuVODvuMTC
154-
-----END EIGamal PRIVATE KEY-----
152+
mA/dhLT55xsoeCG8uyvRhbHRGA8JXNCTbCn8cy5IUHsJ7FSwf/9x0r09xOq3m8G1
153+
U4RwuDU6+amtXY+yveQCoSphoA3KkW1px4WRlLnq4CGwaHj9vrc41NiSRthg8W/w
154+
ub3s3o+E3QRX2SMysKNchTz/jdsWvryliYOUYb+HnkWywOBBcaGsSn08mSDhIwQ/
155+
paLCOWhDigkGw55hfKARdwZKzcoiZwZNg1C7Qk82kZpTr+fww0Iqlw==
156+
-----END ElGamal PRIVATE KEY-----
155157
`
156158
pkcs1Pubkey = `
157-
-----BEGIN EIGamal PUBLIC KEY-----
158-
MGcCIQDPmT6kb7qf1Q3K8BxsYsRxN8nc03xdYGlzbvOpZtFj9wIgNcay1pmohWtK
159-
neGde1Oa7/ytxt8WaG6Lv5RtvfuV1SsCIDWjMke/Nefmbdwyz76dXahPwiP5zxPp
160-
Kr/m3GchBfK2
161-
-----END EIGamal PUBLIC KEY-----
159+
-----BEGIN ElGamal PUBLIC KEY-----
160+
MIGJAiEA9cZBJDM0L+NCYt4vlsMg+HdufcNdUF7Z7W2OtGogl9cCIFdwSXylAPOj
161+
8NYVTmMwSxZMZxoXr25rL884i3vQYOd5AiB64yCSGZoX8aExbxfLYZB8O7c+4a6o
162+
L2z2tsdaNRBL6wIgQTk/bOUCzJNfknsX+LvmpdQ9GVCd9ZiuR9t9nNi9VFs=
163+
-----END ElGamal PUBLIC KEY-----
162164
`
163165
)
164166

cryptobin/elgamal/encryption.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// 公钥加密
9-
func (this EIGamal) Encrypt() EIGamal {
9+
func (this ElGamal) Encrypt() ElGamal {
1010
if this.publicKey == nil {
1111
err := errors.New("publicKey empty.")
1212
return this.AppendError(err)
@@ -23,7 +23,7 @@ func (this EIGamal) Encrypt() EIGamal {
2323
}
2424

2525
// 私钥解密
26-
func (this EIGamal) Decrypt() EIGamal {
26+
func (this ElGamal) Decrypt() ElGamal {
2727
if this.privateKey == nil {
2828
err := errors.New("privateKey empty.")
2929
return this.AppendError(err)

cryptobin/elgamal/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
)
66

77
// 添加错误
8-
func (this EIGamal) AppendError(err ...error) EIGamal {
8+
func (this ElGamal) AppendError(err ...error) ElGamal {
99
this.Errors = append(this.Errors, err...)
1010

1111
return this
1212
}
1313

1414
// 获取错误
15-
func (this EIGamal) Error() error {
15+
func (this ElGamal) Error() error {
1616
return tool.NewError(this.Errors...)
1717
}

0 commit comments

Comments
 (0)