Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 5, 2024
1 parent 35ad6cb commit 8021088
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 211 deletions.
21 changes: 8 additions & 13 deletions cryptobin/dh/curve25519/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// 生成密钥
func (this Curve25519) GenerateKey() Curve25519 {
privateKey, publicKey, err := curve25519.GenerateKey(rand.Reader)
func (this Curve25519) GenerateKeyWithSeed(reader io.Reader) Curve25519 {
privateKey, publicKey, err := curve25519.GenerateKey(reader)

this.privateKey = privateKey
this.publicKey = publicKey
Expand All @@ -19,23 +19,18 @@ func (this Curve25519) GenerateKey() Curve25519 {
}

// 生成密钥
func GenerateKey() Curve25519 {
return defaultCurve25519.GenerateKey()
func GenerateKeyWithSeed(reader io.Reader) Curve25519 {
return defaultCurve25519.GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this Curve25519) GenerateKeyWithSeed(reader io.Reader) Curve25519 {
privateKey, publicKey, err := curve25519.GenerateKey(reader)

this.privateKey = privateKey
this.publicKey = publicKey

return this.AppendError(err)
func (this Curve25519) GenerateKey() Curve25519 {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
func GenerateKeyWithSeed(reader io.Reader) Curve25519 {
return defaultCurve25519.GenerateKeyWithSeed(reader)
func GenerateKey() Curve25519 {
return defaultCurve25519.GenerateKey()
}

// ==========
Expand Down
21 changes: 8 additions & 13 deletions cryptobin/dh/dh/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

// 生成密钥
func (this DH) GenerateKey() DH {
privateKey, publicKey, err := dh.GenerateKeyWithGroup(this.group, rand.Reader)
func (this DH) GenerateKeyWithSeed(reader io.Reader) DH {
privateKey, publicKey, err := dh.GenerateKeyWithGroup(this.group, reader)

this.privateKey = privateKey
this.publicKey = publicKey
Expand All @@ -20,23 +20,18 @@ func (this DH) GenerateKey() DH {
}

// 生成密钥
func GenerateKey(name string) DH {
return defaultDH.SetGroup(name).GenerateKey()
func GenerateKeyWithSeed(reader io.Reader, name string) DH {
return defaultDH.SetGroup(name).GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this DH) GenerateKeyWithSeed(reader io.Reader) DH {
privateKey, publicKey, err := dh.GenerateKeyWithGroup(this.group, reader)

this.privateKey = privateKey
this.publicKey = publicKey

return this.AppendError(err)
func (this DH) GenerateKey() DH {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
func GenerateKeyWithSeed(reader io.Reader, name string) DH {
return defaultDH.SetGroup(name).GenerateKeyWithSeed(reader)
func GenerateKey(name string) DH {
return defaultDH.SetGroup(name).GenerateKey()
}

// ==========
Expand Down
21 changes: 8 additions & 13 deletions cryptobin/dh/ecdh/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// 生成密钥
func (this ECDH) GenerateKey() ECDH {
privateKey, publicKey, err := ecdh.GenerateKey(this.curve, rand.Reader)
func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH {
privateKey, publicKey, err := ecdh.GenerateKey(this.curve, reader)

this.privateKey = privateKey
this.publicKey = publicKey
Expand All @@ -20,24 +20,19 @@ func (this ECDH) GenerateKey() ECDH {

// 生成密钥
// 可用参数 [P521 | P384 | P256 | P224]
func GenerateKey(curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKey()
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH {
privateKey, publicKey, err := ecdh.GenerateKey(this.curve, reader)

this.privateKey = privateKey
this.publicKey = publicKey

return this.AppendError(err)
func (this ECDH) GenerateKey() ECDH {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
// 可用参数 [P521 | P384 | P256 | P224]
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader)
func GenerateKey(curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKey()
}

// ==========
Expand Down
41 changes: 9 additions & 32 deletions cryptobin/dsa/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// 生成密钥
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
func (this DSA) GenerateKey(ln string) DSA {
func (this DSA) GenerateKeyWithSeed(paramReader io.Reader, generateReader io.Reader, ln string) DSA {
var paramSize dsa.ParameterSizes

// 算法类型
Expand All @@ -28,8 +28,8 @@ func (this DSA) GenerateKey(ln string) DSA {
}

priv := &dsa.PrivateKey{}
dsa.GenerateParameters(&priv.Parameters, rand.Reader, paramSize)
dsa.GenerateKey(priv, rand.Reader)
dsa.GenerateParameters(&priv.Parameters, paramReader, paramSize)
dsa.GenerateKey(priv, generateReader)

this.privateKey = priv
this.publicKey = &priv.PublicKey
Expand All @@ -39,43 +39,20 @@ func (this DSA) GenerateKey(ln string) DSA {

// 生成密钥
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
func GenerateKey(ln string) DSA {
return defaultDSA.GenerateKey(ln)
func GenerateKeyWithSeed(paramReader io.Reader, generateReader io.Reader, ln string) DSA {
return defaultDSA.GenerateKeyWithSeed(paramReader, generateReader, ln)
}

// 生成密钥
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
func (this DSA) GenerateKeyWithSeed(paramReader io.Reader, generateReader io.Reader, ln string) DSA {
var paramSize dsa.ParameterSizes

// 算法类型
switch ln {
case "L1024N160":
paramSize = dsa.L1024N160
case "L2048N224":
paramSize = dsa.L2048N224
case "L2048N256":
paramSize = dsa.L2048N256
case "L3072N256":
paramSize = dsa.L3072N256
default:
paramSize = dsa.L1024N160
}

priv := &dsa.PrivateKey{}
dsa.GenerateParameters(&priv.Parameters, paramReader, paramSize)
dsa.GenerateKey(priv, generateReader)

this.privateKey = priv
this.publicKey = &priv.PublicKey

return this
func (this DSA) GenerateKey(ln string) DSA {
return this.GenerateKeyWithSeed(rand.Reader, rand.Reader, ln)
}

// 生成密钥
// 可用参数 [L1024N160 | L2048N224 | L2048N256 | L3072N256]
func GenerateKeyWithSeed(paramReader io.Reader, generateReader io.Reader, ln string) DSA {
return defaultDSA.GenerateKeyWithSeed(paramReader, generateReader, ln)
func GenerateKey(ln string) DSA {
return defaultDSA.GenerateKey(ln)
}

// ==========
Expand Down
24 changes: 8 additions & 16 deletions cryptobin/ecdh/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// 生成密钥
func (this ECDH) GenerateKey() ECDH {
privateKey, err := this.curve.GenerateKey(rand.Reader)
func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH {
privateKey, err := this.curve.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -22,26 +22,18 @@ func (this ECDH) GenerateKey() ECDH {
}

// 生成密钥
func GenerateKey(curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKey()
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH {
privateKey, err := this.curve.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}

this.privateKey = privateKey
this.publicKey = privateKey.PublicKey()

return this
func (this ECDH) GenerateKey() ECDH {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader)
func GenerateKey(curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKey()
}

// ==========
Expand Down
26 changes: 8 additions & 18 deletions cryptobin/ecdsa/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

// 生成密钥
func (this ECDSA) GenerateKey() ECDSA {
privateKey, err := ecdsa.GenerateKey(this.curve, rand.Reader)
func (this ECDSA) GenerateKeyWithSeed(reader io.Reader) ECDSA {
privateKey, err := ecdsa.GenerateKey(this.curve, reader)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -28,29 +28,19 @@ func (this ECDSA) GenerateKey() ECDSA {

// 生成密钥
// 可选 [P521 | P384 | P256 | P224]
func GenerateKey(curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKey()
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this ECDSA) GenerateKeyWithSeed(reader io.Reader) ECDSA {
privateKey, err := ecdsa.GenerateKey(this.curve, reader)
if err != nil {
return this.AppendError(err)
}

this.privateKey = privateKey

// 生成公钥
this.publicKey = &privateKey.PublicKey

return this
func (this ECDSA) GenerateKey() ECDSA {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
// 可选 [P521 | P384 | P256 | P224]
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKeyWithSeed(reader)
func GenerateKey(curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKey()
}

// ==========
Expand Down
24 changes: 8 additions & 16 deletions cryptobin/ed448/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// 生成密钥
func (this ED448) GenerateKey() ED448 {
publicKey, privateKey, err := ed448.GenerateKey(rand.Reader)
func (this ED448) GenerateKeyWithSeed(reader io.Reader) ED448 {
publicKey, privateKey, err := ed448.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -22,26 +22,18 @@ func (this ED448) GenerateKey() ED448 {
}

// 生成密钥
func GenerateKey() ED448 {
return defaultED448.GenerateKey()
func GenerateKeyWithSeed(reader io.Reader) ED448 {
return defaultED448.GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this ED448) GenerateKeyWithSeed(reader io.Reader) ED448 {
publicKey, privateKey, err := ed448.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}

this.publicKey = publicKey
this.privateKey = privateKey

return this
func (this ED448) GenerateKey() ED448 {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
func GenerateKeyWithSeed(reader io.Reader) ED448 {
return defaultED448.GenerateKeyWithSeed(reader)
func GenerateKey() ED448 {
return defaultED448.GenerateKey()
}

// ==========
Expand Down
24 changes: 8 additions & 16 deletions cryptobin/eddsa/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// 生成密钥
func (this EdDSA) GenerateKey() EdDSA {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
func (this EdDSA) GenerateKeyWithSeed(reader io.Reader) EdDSA {
publicKey, privateKey, err := ed25519.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -22,26 +22,18 @@ func (this EdDSA) GenerateKey() EdDSA {
}

// 生成密钥
func GenerateKey() EdDSA {
return defaultEdDSA.GenerateKey()
func GenerateKeyWithSeed(reader io.Reader) EdDSA {
return defaultEdDSA.GenerateKeyWithSeed(reader)
}

// 生成密钥
func (this EdDSA) GenerateKeyWithSeed(reader io.Reader) EdDSA {
publicKey, privateKey, err := ed25519.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}

this.publicKey = publicKey
this.privateKey = privateKey

return this
func (this EdDSA) GenerateKey() EdDSA {
return this.GenerateKeyWithSeed(rand.Reader)
}

// 生成密钥
func GenerateKeyWithSeed(reader io.Reader) EdDSA {
return defaultEdDSA.GenerateKeyWithSeed(reader)
func GenerateKey() EdDSA {
return defaultEdDSA.GenerateKey()
}

// ==========
Expand Down
Loading

0 comments on commit 8021088

Please sign in to comment.