Skip to content

Commit

Permalink
优化 elgamal
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Oct 11, 2024
1 parent 0d5ca2f commit e174f66
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 185 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ go-cryptobin 是 go 的常用加密解密库
* 对称加密解密(Aes/Des/TripleDes/SM4/Tea/Twofish/Xts)
* 对称加密解密模式(ECB/CBC/PCBC/CFB/NCFB/OFB/NOFB/CTR/GCM/CCM)
* 对称加密解密补码(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO97971Padding/ISO7816_4Padding/PBOC2Padding/TBCPadding/PKCS1Padding)
* 非对称加密解密(RSA/SM2/EIGamal
* 非对称签名验证(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/EIGamal/ED448/Gost)
* 非对称加密解密(RSA/SM2/ElGamal
* 非对称签名验证(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/ElGamal/ED448/Gost)
* 默认 `Aes`, `ECB`, `NoPadding`


Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ go-cryptobin is a go encrypt or decrypt library
* sym encrypts(Aes/Des/TripleDes/SM4/Tea/Twofish/Xts)
* encrypt mode(ECB/CBC/PCBC/CFB/NCFB/OFB/NOFB/CTR/GCM/CCM)
* encrypt padding(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO97971Padding/ISO7816_4Padding/PBOC2Padding/TBCPadding/PKCS1Padding)
* asym encrypts(Aes(RSA/SM2/EIGamal
* asym sign(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/EIGamal/ED448/Gost)
* asym encrypts(Aes(RSA/SM2/ElGamal
* asym sign(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/ElGamal/ED448/Gost)
* default setting `Aes`, `ECB`, `NoPadding`


Expand Down
2 changes: 1 addition & 1 deletion cryptobin/elgamal/check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package elgamal

// 检测公钥私钥是否匹配
func (this EIGamal) CheckKeyPair() bool {
func (this ElGamal) CheckKeyPair() bool {
// 私钥导出的公钥
pubKeyFromPriKey := this.MakePublicKey().
CreatePublicKey().
Expand Down
28 changes: 14 additions & 14 deletions cryptobin/elgamal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ var (
// 生成私钥 pem 数据
// elgamal := New().GenerateKey("L2048N256")
// priKey := elgamal.CreatePrivateKey().ToKeyString()
func (this EIGamal) CreatePrivateKey() EIGamal {
func (this ElGamal) CreatePrivateKey() ElGamal {
return this.CreatePKCS1PrivateKey()
}

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

// 生成公钥 pem 数据
func (this EIGamal) CreatePublicKey() EIGamal {
func (this ElGamal) CreatePublicKey() ElGamal {
return this.CreatePKCS1PublicKey()
}

// ==========

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

privateBlock := &pem.Block{
Type: "EIGamal PRIVATE KEY",
Type: "ElGamal PRIVATE KEY",
Bytes: privateKeyBytes,
}

Expand All @@ -72,7 +72,7 @@ func (this EIGamal) CreatePKCS1PrivateKey() EIGamal {
// 生成 pkcs1 私钥带密码 pem 数据
// CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC")
// PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC
func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) EIGamal {
func (this ElGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) ElGamal {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
Expand All @@ -99,7 +99,7 @@ func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...s
// 生成加密数据
privateBlock, err := pkcs1.EncryptPEMBlock(
rand.Reader,
"EIGamal PRIVATE KEY",
"ElGamal PRIVATE KEY",
privateKeyBytes,
[]byte(password),
cipher,
Expand All @@ -114,7 +114,7 @@ func (this EIGamal) CreatePKCS1PrivateKeyWithPassword(password string, opts ...s
}

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

publicBlock := &pem.Block{
Type: "EIGamal PUBLIC KEY",
Type: "ElGamal PUBLIC KEY",
Bytes: publicKeyBytes,
}

Expand All @@ -138,7 +138,7 @@ func (this EIGamal) CreatePKCS1PublicKey() EIGamal {
// ==========

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

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

// 生成公钥 pem 数据
func (this EIGamal) CreatePKCS8PublicKey() EIGamal {
func (this ElGamal) CreatePKCS8PublicKey() ElGamal {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
Expand All @@ -220,7 +220,7 @@ func (this EIGamal) CreatePKCS8PublicKey() EIGamal {
// ====================

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

// 生成公钥 xml 数据
func (this EIGamal) CreateXMLPublicKey() EIGamal {
func (this ElGamal) CreateXMLPublicKey() ElGamal {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
Expand Down
14 changes: 7 additions & 7 deletions cryptobin/elgamal/elgamal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type (
)

/**
* EIGamal
* ElGamal
*
* @create 2023-6-22
* @author deatil
*/
type EIGamal struct {
type ElGamal struct {
// 私钥
privateKey *elgamal.PrivateKey

Expand All @@ -45,20 +45,20 @@ type EIGamal struct {
}

// 构造函数
func NewEIGamal() EIGamal {
return EIGamal{
func NewElGamal() ElGamal {
return ElGamal{
signHash: sha256.New,
verify: false,
Errors: make([]error, 0),
}
}

// 构造函数
func New() EIGamal {
return NewEIGamal()
func New() ElGamal {
return NewElGamal()
}

var (
// 默认
defaultEIGamal = NewEIGamal()
defaultElGamal = NewElGamal()
)
46 changes: 24 additions & 22 deletions cryptobin/elgamal/elgamal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (

var (
prikeyXML = `
<EIGamalKeyValue>
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
<ElGamalKeyValue>
<P>9W35RbKvFgfHndG9wVvFDMDw86BClpDk6kdeGr1ygLc=</P>
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
<Y>120jHKCdPWjLGrqH3HiCZ2GezWyEjfEIPBMhULymfzM=</Y>
<X>BjtroR34tS5cvF5YNJaxmOjGDas43wKFunHCYS4P6CQ=</X>
</EIGamalKeyValue>
</ElGamalKeyValue>
`
pubkeyXML = `
<EIGamalKeyValue>
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
<ElGamalKeyValue>
<P>9W35RbKvFgfHndG9wVvFDMDw86BClpDk6kdeGr1ygLc=</P>
<G>vG406oGr5OqG0mMOtq5wWo/aGWWE8EPiPl09/I+ySxs=</G>
<Y>120jHKCdPWjLGrqH3HiCZ2GezWyEjfEIPBMhULymfzM=</Y>
</EIGamalKeyValue>
</ElGamalKeyValue>
`
)

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

var (
pkcs1Prikey = `
-----BEGIN EIGamal PRIVATE KEY-----
MIGMAgEAAiEAz5k+pG+6n9UNyvAcbGLEcTfJ3NN8XWBpc27zqWbRY/cCIDXGstaZ
qIVrSp3hnXtTmu/8rcbfFmhui7+Ubb37ldUrAiA1ozJHvzXn5m3cMs++nV2oT8Ij
+c8T6Sq/5txnIQXytgIgLCd+/uxQSB05Y2xWtzz9UTVBC9Sj9uh2k5ZZlqfY8v8=
-----END EIGamal PRIVATE KEY-----
-----BEGIN ElGamal PRIVATE KEY-----
MIGuAgEAAiEA9cZBJDM0L+NCYt4vlsMg+HdufcNdUF7Z7W2OtGogl9cCIFdwSXyl
APOj8NYVTmMwSxZMZxoXr25rL884i3vQYOd5AiB64yCSGZoX8aExbxfLYZB8O7c+
4a6oL2z2tsdaNRBL6wIgQTk/bOUCzJNfknsX+LvmpdQ9GVCd9ZiuR9t9nNi9VFsC
IFNu9DFgP10mqzkn/fB/bBrV5Xc7UPhgceZneZ5yzCDk
-----END ElGamal PRIVATE KEY-----
`
pkcs1EnPrikey = `
-----BEGIN EIGamal PRIVATE KEY-----
-----BEGIN ElGamal PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,a26bd66b277099bd
DEK-Info: DES-EDE3-CBC,b1e4e15253a2dd4f
YFVfbgFspb0Pr21832B3xwm1LUrf2SDu4K/Wc8MtD5Is4NCzOatQw8kYA5qkIf9p
6CyR57wtsJCakIw3E0DKSZYNe3tQbfxRK+ZUgYAda/E0UpiLe9j/WJRQ8G9/DAOi
mXXdz6pq4omiDVh3DcSlQJskJvoM6KE5OgJh81iuP7jbiX109ayEYPNuVODvuMTC
-----END EIGamal PRIVATE KEY-----
mA/dhLT55xsoeCG8uyvRhbHRGA8JXNCTbCn8cy5IUHsJ7FSwf/9x0r09xOq3m8G1
U4RwuDU6+amtXY+yveQCoSphoA3KkW1px4WRlLnq4CGwaHj9vrc41NiSRthg8W/w
ub3s3o+E3QRX2SMysKNchTz/jdsWvryliYOUYb+HnkWywOBBcaGsSn08mSDhIwQ/
paLCOWhDigkGw55hfKARdwZKzcoiZwZNg1C7Qk82kZpTr+fww0Iqlw==
-----END ElGamal PRIVATE KEY-----
`
pkcs1Pubkey = `
-----BEGIN EIGamal PUBLIC KEY-----
MGcCIQDPmT6kb7qf1Q3K8BxsYsRxN8nc03xdYGlzbvOpZtFj9wIgNcay1pmohWtK
neGde1Oa7/ytxt8WaG6Lv5RtvfuV1SsCIDWjMke/Nefmbdwyz76dXahPwiP5zxPp
Kr/m3GchBfK2
-----END EIGamal PUBLIC KEY-----
-----BEGIN ElGamal PUBLIC KEY-----
MIGJAiEA9cZBJDM0L+NCYt4vlsMg+HdufcNdUF7Z7W2OtGogl9cCIFdwSXylAPOj
8NYVTmMwSxZMZxoXr25rL884i3vQYOd5AiB64yCSGZoX8aExbxfLYZB8O7c+4a6o
L2z2tsdaNRBL6wIgQTk/bOUCzJNfknsX+LvmpdQ9GVCd9ZiuR9t9nNi9VFs=
-----END ElGamal PUBLIC KEY-----
`
)

Expand Down
4 changes: 2 additions & 2 deletions cryptobin/elgamal/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// 公钥加密
func (this EIGamal) Encrypt() EIGamal {
func (this ElGamal) Encrypt() ElGamal {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
Expand All @@ -23,7 +23,7 @@ func (this EIGamal) Encrypt() EIGamal {
}

// 私钥解密
func (this EIGamal) Decrypt() EIGamal {
func (this ElGamal) Decrypt() ElGamal {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
Expand Down
4 changes: 2 additions & 2 deletions cryptobin/elgamal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

// 添加错误
func (this EIGamal) AppendError(err ...error) EIGamal {
func (this ElGamal) AppendError(err ...error) ElGamal {
this.Errors = append(this.Errors, err...)

return this
}

// 获取错误
func (this EIGamal) Error() error {
func (this ElGamal) Error() error {
return tool.NewError(this.Errors...)
}
Loading

0 comments on commit e174f66

Please sign in to comment.