diff --git a/cryptobin/dh/curve25519/from.go b/cryptobin/dh/curve25519/from.go index 497599b7..7e0129ad 100644 --- a/cryptobin/dh/curve25519/from.go +++ b/cryptobin/dh/curve25519/from.go @@ -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 @@ -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() } // ========== diff --git a/cryptobin/dh/dh/from.go b/cryptobin/dh/dh/from.go index c9b89237..59c94a71 100644 --- a/cryptobin/dh/dh/from.go +++ b/cryptobin/dh/dh/from.go @@ -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 @@ -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() } // ========== diff --git a/cryptobin/dh/ecdh/from.go b/cryptobin/dh/ecdh/from.go index d610d0c9..b684b77e 100644 --- a/cryptobin/dh/ecdh/from.go +++ b/cryptobin/dh/ecdh/from.go @@ -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 @@ -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() } // ========== diff --git a/cryptobin/dsa/from.go b/cryptobin/dsa/from.go index cc59b759..6374d85b 100644 --- a/cryptobin/dsa/from.go +++ b/cryptobin/dsa/from.go @@ -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 // 算法类型 @@ -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 @@ -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) } // ========== diff --git a/cryptobin/ecdh/from.go b/cryptobin/ecdh/from.go index 94456d78..97051184 100644 --- a/cryptobin/ecdh/from.go +++ b/cryptobin/ecdh/from.go @@ -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) } @@ -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() } // ========== diff --git a/cryptobin/ecdsa/from.go b/cryptobin/ecdsa/from.go index b592bea8..f8ae19fb 100644 --- a/cryptobin/ecdsa/from.go +++ b/cryptobin/ecdsa/from.go @@ -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) } @@ -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() } // ========== diff --git a/cryptobin/ed448/from.go b/cryptobin/ed448/from.go index 6f7ba900..a7e5c7b0 100644 --- a/cryptobin/ed448/from.go +++ b/cryptobin/ed448/from.go @@ -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) } @@ -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() } // ========== diff --git a/cryptobin/eddsa/from.go b/cryptobin/eddsa/from.go index 163c9128..84bb7729 100644 --- a/cryptobin/eddsa/from.go +++ b/cryptobin/eddsa/from.go @@ -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) } @@ -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() } // ========== diff --git a/cryptobin/elgamal/from.go b/cryptobin/elgamal/from.go index 2a950992..f153fa80 100644 --- a/cryptobin/elgamal/from.go +++ b/cryptobin/elgamal/from.go @@ -8,24 +8,6 @@ import ( cryptobin_elgamal "github.com/deatil/go-cryptobin/elgamal" ) -// 生成密钥 -func (this EIGamal) GenerateKey(bitsize, probability int) EIGamal { - priv, err := cryptobin_elgamal.GenerateKey(rand.Reader, bitsize, probability) - if err != nil { - return this.AppendError(err) - } - - this.privateKey = priv - this.publicKey = &priv.PublicKey - - return this -} - -// 生成密钥 -func GenerateKey(bitsize, probability int) EIGamal { - return defaultEIGamal.GenerateKey(bitsize, probability) -} - // 使用数据生成密钥对 func (this EIGamal) GenerateKeyWithSeed(reader io.Reader, bitsize, probability int) EIGamal { priv, err := cryptobin_elgamal.GenerateKey(reader, bitsize, probability) @@ -44,6 +26,16 @@ func GenerateKeyWithSeed(reader io.Reader, bitsize, probability int) EIGamal { return defaultEIGamal.GenerateKeyWithSeed(reader, bitsize, probability) } +// 生成密钥 +func (this EIGamal) GenerateKey(bitsize, probability int) EIGamal { + return this.GenerateKeyWithSeed(rand.Reader, bitsize, probability) +} + +// 生成密钥 +func GenerateKey(bitsize, probability int) EIGamal { + return defaultEIGamal.GenerateKey(bitsize, probability) +} + // ========== // 私钥 diff --git a/cryptobin/rsa/from.go b/cryptobin/rsa/from.go index 7fe0d26f..54032104 100644 --- a/cryptobin/rsa/from.go +++ b/cryptobin/rsa/from.go @@ -12,8 +12,8 @@ import ( // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func (this RSA) GenerateKey(bits int) RSA { - privateKey, err := rsa.GenerateKey(rand.Reader, bits) +func (this RSA) GenerateKeyWithSeed(reader io.Reader, bits int) RSA { + privateKey, err := rsa.GenerateKey(reader, bits) if err != nil { return this.AppendError(err) } @@ -28,13 +28,13 @@ func (this RSA) GenerateKey(bits int) RSA { // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func GenerateKey(bits int) RSA { - return defaultRSA.GenerateKey(bits) +func GenerateKeyWithSeed(reader io.Reader, bits int) RSA { + return defaultRSA.GenerateKeyWithSeed(reader, bits) } // 生成密钥 -func (this RSA) GenerateMultiPrimeKey(nprimes int, bits int) RSA { - privateKey, err := rsa.GenerateMultiPrimeKey(rand.Reader, nprimes, bits) +func (this RSA) GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) RSA { + privateKey, err := rsa.GenerateMultiPrimeKey(reader, nprimes, bits) if err != nil { return this.AppendError(err) } @@ -48,52 +48,32 @@ func (this RSA) GenerateMultiPrimeKey(nprimes int, bits int) RSA { } // 生成密钥 -func GenerateMultiPrimeKey(nprimes int, bits int) RSA { - return defaultRSA.GenerateMultiPrimeKey(nprimes, bits) +func GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) RSA { + return defaultRSA.GenerateMultiPrimeKeyWithSeed(reader, nprimes, bits) } // ========== // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func (this RSA) GenerateKeyWithSeed(reader io.Reader, bits int) RSA { - privateKey, err := rsa.GenerateKey(reader, bits) - if err != nil { - return this.AppendError(err) - } - - this.privateKey = privateKey - - // 生成公钥 - this.publicKey = &this.privateKey.PublicKey - - return this +func (this RSA) GenerateKey(bits int) RSA { + return this.GenerateKeyWithSeed(rand.Reader, bits) } // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func GenerateKeyWithSeed(reader io.Reader, bits int) RSA { - return defaultRSA.GenerateKeyWithSeed(reader, bits) +func GenerateKey(bits int) RSA { + return defaultRSA.GenerateKey(bits) } // 生成密钥 -func (this RSA) GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) RSA { - privateKey, err := rsa.GenerateMultiPrimeKey(reader, nprimes, bits) - if err != nil { - return this.AppendError(err) - } - - this.privateKey = privateKey - - // 生成公钥 - this.publicKey = &this.privateKey.PublicKey - - return this +func (this RSA) GenerateMultiPrimeKey(nprimes int, bits int) RSA { + return this.GenerateMultiPrimeKeyWithSeed(rand.Reader, nprimes, bits) } // 生成密钥 -func GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) RSA { - return defaultRSA.GenerateMultiPrimeKeyWithSeed(reader, nprimes, bits) +func GenerateMultiPrimeKey(nprimes int, bits int) RSA { + return defaultRSA.GenerateMultiPrimeKey(nprimes, bits) } // ========== diff --git a/cryptobin/sm2/from.go b/cryptobin/sm2/from.go index 478c4951..0cd2343f 100644 --- a/cryptobin/sm2/from.go +++ b/cryptobin/sm2/from.go @@ -11,26 +11,6 @@ import ( cryptobin_tool "github.com/deatil/go-cryptobin/tool" ) -// 生成密钥对 -func (this SM2) GenerateKey() SM2 { - privateKey, err := sm2.GenerateKey(rand.Reader) - if err != nil { - return this.AppendError(err) - } - - this.privateKey = privateKey - - // 生成公钥 - this.publicKey = &this.privateKey.PublicKey - - return this -} - -// 生成密钥对 -func GenerateKey() SM2 { - return defaultSM2.GenerateKey() -} - // 使用自定义数据生成密钥对 func (this SM2) GenerateKeyWithSeed(reader io.Reader) SM2 { privateKey, err := sm2.GenerateKey(reader) @@ -51,6 +31,16 @@ func GenerateKeyWithSeed(reader io.Reader) SM2 { return defaultSM2.GenerateKeyWithSeed(reader) } +// 生成密钥对 +func (this SM2) GenerateKey() SM2 { + return this.GenerateKeyWithSeed(rand.Reader) +} + +// 生成密钥对 +func GenerateKey() SM2 { + return defaultSM2.GenerateKey() +} + // ========== // 私钥