Skip to content

Commit

Permalink
优化更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Aug 5, 2022
1 parent cb9b144 commit 29b8e4d
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 32 deletions.
15 changes: 9 additions & 6 deletions cryptobin/dsa/dsa_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ var (
GetHashFromName = cryptobin_pkcs8.GetHashFromName
)

// 私钥
// 生成私钥 pem 数据
// 使用:
// dsa := New().GenerateKey("L2048N256")
// priKey := dsa.CreatePrivateKey().ToKeyString()
func (this DSA) CreatePrivateKey() DSA {
if this.privateKey == nil {
this.Error = errors.New("dsa: [CreatePrivateKey()] privateKey error.")
Expand All @@ -50,7 +53,7 @@ func (this DSA) CreatePrivateKey() DSA {
return this
}

// 私钥带密码
// 生成私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC")
func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DSA {
if this.privateKey == nil {
Expand Down Expand Up @@ -97,7 +100,7 @@ func (this DSA) CreatePrivateKeyWithPassword(password string, opts ...string) DS
return this
}

// 公钥
// 生成公钥 pem 数据
func (this DSA) CreatePublicKey() DSA {
var publicKey *dsa.PublicKey

Expand Down Expand Up @@ -131,7 +134,7 @@ func (this DSA) CreatePublicKey() DSA {

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

// 私钥
// 生成 pkcs8 私钥 pem 数据
func (this DSA) CreatePKCS8PrivateKey() DSA {
if this.privateKey == nil {
this.Error = errors.New("dsa: [CreatePKCS8PrivateKey()] privateKey error.")
Expand All @@ -154,7 +157,7 @@ func (this DSA) CreatePKCS8PrivateKey() DSA {
return this
}

// PKCS8 私钥带密码
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this DSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) DSA {
if this.privateKey == nil {
Expand Down Expand Up @@ -193,7 +196,7 @@ func (this DSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any)
return this
}

// 公钥
// 生成公钥 pem 数据
func (this DSA) CreatePKCS8PublicKey() DSA {
var publicKey *dsa.PublicKey

Expand Down
13 changes: 8 additions & 5 deletions cryptobin/ecdsa/ecdsa_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ var (
GetHashFromName = cryptobin_pkcs8.GetHashFromName
)

// 私钥
// 生成私钥 pem 数据
// 使用:
// obj := New().WithCurve("P521").GenerateKey()
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this Ecdsa) CreatePrivateKey() Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("Ecdsa: [CreatePrivateKey()] privateKey error.")
Expand All @@ -50,7 +53,7 @@ func (this Ecdsa) CreatePrivateKey() Ecdsa {
return this
}

// 私钥带密码
// 生成私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC")
func (this Ecdsa) CreatePrivateKeyWithPassword(password string, opts ...string) Ecdsa {
if this.privateKey == nil {
Expand Down Expand Up @@ -97,7 +100,7 @@ func (this Ecdsa) CreatePrivateKeyWithPassword(password string, opts ...string)
return this
}

// PKCS8 私钥
// 生成 PKCS8 私钥 pem 数据
func (this Ecdsa) CreatePKCS8PrivateKey() Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("Ecdsa: [CreatePKCS8PrivateKey()] privateKey error.")
Expand All @@ -121,7 +124,7 @@ func (this Ecdsa) CreatePKCS8PrivateKey() Ecdsa {
return this
}

// PKCS8 私钥带密码
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Ecdsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) Ecdsa {
if this.privateKey == nil {
Expand Down Expand Up @@ -160,7 +163,7 @@ func (this Ecdsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any
return this
}

// 公钥
// 生成公钥 pem 数据
func (this Ecdsa) CreatePublicKey() Ecdsa {
var publicKey *ecdsa.PublicKey

Expand Down
6 changes: 3 additions & 3 deletions cryptobin/ecdsa/ecdsa_with.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (this Ecdsa) WithPublicKey(data *ecdsa.PublicKey) Ecdsa {
return this
}

// 设置 data
// 可选 [P521 | P384 | P256 | P224]
// 设置曲线类型
// 可选参数 [P521 | P384 | P256 | P224]
func (this Ecdsa) WithCurve(curve string) Ecdsa {
switch curve {
case "P521":
Expand Down Expand Up @@ -57,7 +57,7 @@ func (this Ecdsa) WithSignHash(hash string) Ecdsa {
return this
}

// 设置 veryed
// 设置验证结果
func (this Ecdsa) WithVeryed(data bool) Ecdsa {
this.veryed = data

Expand Down
9 changes: 6 additions & 3 deletions cryptobin/eddsa/eddsa_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ var (
GetHashFromName = cryptobin_pkcs8.GetHashFromName
)

// 私钥
// 生成私钥 pem 数据
// 使用:
// obj := New().GenerateKey()
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this EdDSA) CreatePrivateKey() EdDSA {
if this.privateKey == nil {
this.Error = errors.New("EdDSA: [CreatePrivateKey()] privateKey error.")
Expand All @@ -49,7 +52,7 @@ func (this EdDSA) CreatePrivateKey() EdDSA {
return this
}

// 私钥带密码
// 生成私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this EdDSA) CreatePrivateKeyWithPassword(password string, opts ...any) EdDSA {
if this.privateKey == nil {
Expand Down Expand Up @@ -88,7 +91,7 @@ func (this EdDSA) CreatePrivateKeyWithPassword(password string, opts ...any) EdD
return this
}

// 公钥
// 生成公钥 pem 数据
func (this EdDSA) CreatePublicKey() EdDSA {
var publicKey ed25519.PublicKey

Expand Down
17 changes: 10 additions & 7 deletions cryptobin/rsa/rsa_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ var (
GetHashFromName = cryptobin_pkcs8.GetHashFromName
)

// 私钥, PKCS1 别名
// 生成私钥 pem 数据, PKCS1 别名
// 使用:
// obj := New().GenerateKey(2048)
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this Rsa) CreatePrivateKey() Rsa {
return this.CreatePKCS1PrivateKey()
}

// 私钥带密码, PKCS1 别名
// 生成私钥带密码 pem 数据, PKCS1 别名
func (this Rsa) CreatePrivateKeyWithPassword(password string, opts ...string) Rsa {
return this.CreatePKCS1PrivateKeyWithPassword(password, opts...)
}

// PKCS1 私钥
// 生成 PKCS1 私钥
func (this Rsa) CreatePKCS1PrivateKey() Rsa {
if this.privateKey == nil {
this.Error = errors.New("Rsa: [CreatePKCS1PrivateKey()] privateKey error.")
Expand All @@ -55,7 +58,7 @@ func (this Rsa) CreatePKCS1PrivateKey() Rsa {
return this
}

// PKCS1 私钥带密码
// 生成 PKCS1 私钥带密码 pem 数据
// CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC")
func (this Rsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) Rsa {
if this.privateKey == nil {
Expand Down Expand Up @@ -98,7 +101,7 @@ func (this Rsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...strin
return this
}

// PKCS8 私钥
// 生成 PKCS8 私钥 pem 数据
func (this Rsa) CreatePKCS8PrivateKey() Rsa {
if this.privateKey == nil {
this.Error = errors.New("Rsa: [CreatePKCS8PrivateKey()] privateKey error.")
Expand All @@ -121,7 +124,7 @@ func (this Rsa) CreatePKCS8PrivateKey() Rsa {
return this
}

// PKCS8 私钥带密码
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Rsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) Rsa {
if this.privateKey == nil {
Expand Down Expand Up @@ -160,7 +163,7 @@ func (this Rsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any)
return this
}

// 公钥
// 生成公钥 pem 数据
func (this Rsa) CreatePublicKey() Rsa {
var publicKey *rsa.PublicKey

Expand Down
9 changes: 6 additions & 3 deletions cryptobin/sm2/sm2_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"github.com/tjfoc/gmsm/x509"
)

// 国密私钥
// 生成私钥 pem 数据
// 使用:
// obj := New().GenerateKey()
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this SM2) CreatePrivateKey() SM2 {
if this.privateKey == nil {
this.Error = errors.New("SM2: [CreatePrivateKey()] privateKey error.")
Expand All @@ -19,7 +22,7 @@ func (this SM2) CreatePrivateKey() SM2 {
return this
}

// 国密私钥带密码
// 生成私钥带密码 pem 数据
func (this SM2) CreatePrivateKeyWithPassword(password string) SM2 {
if this.privateKey == nil {
this.Error = errors.New("SM2: [CreatePrivateKeyWithPassword()] privateKey error.")
Expand All @@ -31,7 +34,7 @@ func (this SM2) CreatePrivateKeyWithPassword(password string) SM2 {
return this
}

// 国密公钥
// 生成公钥 pem 数据
func (this SM2) CreatePublicKey() SM2 {
var publicKey *sm2.PublicKey

Expand Down
15 changes: 15 additions & 0 deletions docs/dh.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
dhStatus = true
}

fmt.Println("生成的密钥是否相同结果: ", dhStatus)
}
~~~

Expand All @@ -51,6 +52,13 @@ func main() {

dh1secret := dh1.ComputeSecret(dh1pri, dh2pub)
dh2secret := dh2.ComputeSecret(dh2pri, dh1pub)

dhStatus := false
if string(dh1secret) == string(dh2secret) {
dhStatus = true
}

fmt.Println("生成的密钥是否相同结果: ", dhStatus)
}
~~~

Expand All @@ -74,5 +82,12 @@ func main() {

dh1secret := dh1.ComputeSecret(dh1pri, dh2pub)
dh2secret := dh2.ComputeSecret(dh2pri, dh1pub)

dhStatus := false
if string(dh1secret) == string(dh2secret) {
dhStatus = true
}

fmt.Println("生成的密钥是否相同结果: ", dhStatus)
}
~~~
1 change: 1 addition & 0 deletions docs/dsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
fs := filesystem.New()

// 生成证书
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
dsa := cryptobin_dsa.New().GenerateKey("L2048N256")
dsaPriKey := dsa.
CreatePrivateKey().
Expand Down
1 change: 1 addition & 0 deletions docs/ecdsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
fs := filesystem.New()

// 生成证书
// 可选参数 [P521 | P384 | P256 | P224]
obj := cryptobin.
NewEcdsa().
WithCurve("P521").
Expand Down
3 changes: 2 additions & 1 deletion docs/rsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
fs := filesystem.New()

// 生成证书
// bits = 512 | 1024 | 2048 | 4096
obj := cryptobin.
NewRsa().
GenerateKey(2048)
Expand Down Expand Up @@ -64,7 +65,7 @@ func main() {

objPriKey := obj.
CreatePKCS8PrivateKeyWithPassword("123", cryptobin_rsa.Opts{
Cipher: cryptobin_rsa.CipherMap["AES256CBC"],
Cipher: cryptobin_rsa.GetCipherFromName("AES256CBC"),
KDFOpts: cryptobin_rsa.ScryptOpts{
CostParameter: 1 << 15,
BlockSize: 8,
Expand Down
2 changes: 2 additions & 0 deletions pkcs8/cipher_gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func (this CipherGCM) Encrypt(key, plaintext []byte) ([]byte, []byte, error) {

// 加密数据
ciphertext := aead.Seal(nil, nonce, plaintext, nil)

// 需要编码的参数
paramSeq := gcmParams{
Nonce: nonce,
ICVLen: aead.Overhead(),
Expand Down
13 changes: 9 additions & 4 deletions pkcs8/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,20 @@ func EncryptPKCS8PrivateKey(

cipher := opt.Cipher
if cipher == nil {
return nil, errors.New("failed to encrypt PEM: unknown algorithm")
return nil, errors.New("failed to encrypt PEM: unknown opts cipher")
}

salt := make([]byte, opt.KDFOpts.GetSaltSize())
kdfOpts := opt.KDFOpts
if kdfOpts == nil {
return nil, errors.New("failed to encrypt PEM: unknown opts kdfOpts")
}

salt := make([]byte, kdfOpts.GetSaltSize())
if _, err := io.ReadFull(rand, salt); err != nil {
return nil, errors.New(err.Error() + " failed to generate salt")
}

key, kdfParams, err := opt.KDFOpts.DeriveKey(password, salt, cipher.KeySize())
key, kdfParams, err := kdfOpts.DeriveKey(password, salt, cipher.KeySize())
if err != nil {
return nil, err
}
Expand All @@ -133,7 +138,7 @@ func EncryptPKCS8PrivateKey(
}

keyDerivationFunc := pkix.AlgorithmIdentifier{
Algorithm: opt.KDFOpts.OID(),
Algorithm: kdfOpts.OID(),
Parameters: asn1.RawValue{
FullBytes: marshalledParams,
},
Expand Down

0 comments on commit 29b8e4d

Please sign in to comment.