diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0fa8a8f8..6df32a01 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,3 +42,6 @@ jobs: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: files: cover.out + + - name: Test Generic + run: go test -short -tags=purego -covermode=atomic ./... diff --git a/cryptobin/dh/dh/check.go b/cryptobin/dh/dh/check.go index ee481826..e64f9bf7 100644 --- a/cryptobin/dh/dh/check.go +++ b/cryptobin/dh/dh/check.go @@ -1,7 +1,7 @@ package dh // 检测公钥私钥是否匹配 -func (this Dh) CheckKeyPair() bool { +func (this DH) CheckKeyPair() bool { // 私钥导出的公钥 pubKeyFromPriKey := this.MakePublicKey(). CreatePublicKey(). diff --git a/cryptobin/dh/dh/create.go b/cryptobin/dh/dh/create.go index a35b1b87..3e7c12de 100644 --- a/cryptobin/dh/dh/create.go +++ b/cryptobin/dh/dh/create.go @@ -29,9 +29,9 @@ var ( // 使用: // obj := New().GenerateKey("P2048") // priKey := obj.CreatePrivateKey().ToKeyString() -func (this Dh) CreatePrivateKey() Dh { +func (this DH) CreatePrivateKey() DH { if this.privateKey == nil { - err := errors.New("Dh: privateKey error.") + err := errors.New("dh: privateKey error.") return this.AppendError(err) } @@ -52,9 +52,9 @@ func (this Dh) CreatePrivateKey() Dh { // 生成 PKCS8 私钥带密码 pem 数据 // CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256") -func (this Dh) CreatePrivateKeyWithPassword(password string, opts ...any) Dh { +func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH { if this.privateKey == nil { - err := errors.New("Dh: privateKey error.") + err := errors.New("dh: privateKey error.") return this.AppendError(err) } @@ -87,9 +87,9 @@ func (this Dh) CreatePrivateKeyWithPassword(password string, opts ...any) Dh { } // 生成公钥 pem 数据 -func (this Dh) CreatePublicKey() Dh { +func (this DH) CreatePublicKey() DH { if this.publicKey == nil { - err := errors.New("Dh: publicKey error.") + err := errors.New("dh: publicKey error.") return this.AppendError(err) } @@ -109,14 +109,14 @@ func (this Dh) CreatePublicKey() Dh { } // 根据公钥和私钥生成密钥 -func (this Dh) CreateSecretKey() Dh { +func (this DH) CreateSecretKey() DH { if this.privateKey == nil { - err := errors.New("Dh: privateKey error.") + err := errors.New("dh: privateKey error.") return this.AppendError(err) } if this.publicKey == nil { - err := errors.New("Dh: publicKey error.") + err := errors.New("dh: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/dh/dh/dh.go b/cryptobin/dh/dh/dh.go index 8a88643f..39fae350 100644 --- a/cryptobin/dh/dh/dh.go +++ b/cryptobin/dh/dh/dh.go @@ -15,7 +15,7 @@ type ( * @create 2022-8-7 * @author deatil */ -type Dh struct { +type DH struct { // 私钥 privateKey *cryptobin_dh.PrivateKey @@ -36,17 +36,17 @@ type Dh struct { } // 构造函数 -func NewDH() Dh { +func NewDH() DH { group, _ := cryptobin_dh.GetMODGroup(cryptobin_dh.P2048) - return Dh{ + return DH{ group: group, Errors: make([]error, 0), } } // 构造函数 -func New() Dh { +func New() DH { return NewDH() } diff --git a/cryptobin/dh/dh/error.go b/cryptobin/dh/dh/error.go index 387f2ebe..1766f628 100644 --- a/cryptobin/dh/dh/error.go +++ b/cryptobin/dh/dh/error.go @@ -5,13 +5,13 @@ import ( ) // 添加错误 -func (this Dh) AppendError(err ...error) Dh { +func (this DH) AppendError(err ...error) DH { this.Errors = append(this.Errors, err...) return this } // 获取错误 -func (this Dh) Error() error { +func (this DH) Error() error { return cryptobin_tool.NewError(this.Errors...) } diff --git a/cryptobin/dh/dh/from.go b/cryptobin/dh/dh/from.go index edb88c63..c9b89237 100644 --- a/cryptobin/dh/dh/from.go +++ b/cryptobin/dh/dh/from.go @@ -10,7 +10,7 @@ import ( ) // 生成密钥 -func (this Dh) GenerateKey() Dh { +func (this DH) GenerateKey() DH { privateKey, publicKey, err := dh.GenerateKeyWithGroup(this.group, rand.Reader) this.privateKey = privateKey @@ -20,12 +20,12 @@ func (this Dh) GenerateKey() Dh { } // 生成密钥 -func GenerateKey(name string) Dh { +func GenerateKey(name string) DH { return defaultDH.SetGroup(name).GenerateKey() } // 生成密钥 -func (this Dh) GenerateKeyWithSeed(reader io.Reader) Dh { +func (this DH) GenerateKeyWithSeed(reader io.Reader) DH { privateKey, publicKey, err := dh.GenerateKeyWithGroup(this.group, reader) this.privateKey = privateKey @@ -35,14 +35,14 @@ func (this Dh) GenerateKeyWithSeed(reader io.Reader) Dh { } // 生成密钥 -func GenerateKeyWithSeed(reader io.Reader, name string) Dh { +func GenerateKeyWithSeed(reader io.Reader, name string) DH { return defaultDH.SetGroup(name).GenerateKeyWithSeed(reader) } // ========== // 私钥 -func (this Dh) FromPrivateKey(key []byte) Dh { +func (this DH) FromPrivateKey(key []byte) DH { parsedKey, err := this.ParsePrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -54,12 +54,12 @@ func (this Dh) FromPrivateKey(key []byte) Dh { } // 私钥 -func FromPrivateKey(key []byte) Dh { +func FromPrivateKey(key []byte) DH { return defaultDH.FromPrivateKey(key) } // 私钥带密码 -func (this Dh) FromPrivateKeyWithPassword(key []byte, password string) Dh { +func (this DH) FromPrivateKeyWithPassword(key []byte, password string) DH { parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -71,12 +71,12 @@ func (this Dh) FromPrivateKeyWithPassword(key []byte, password string) Dh { } // 私钥 -func FromPrivateKeyWithPassword(key []byte, password string) Dh { +func FromPrivateKeyWithPassword(key []byte, password string) DH { return defaultDH.FromPrivateKeyWithPassword(key, password) } // 公钥 -func (this Dh) FromPublicKey(key []byte) Dh { +func (this DH) FromPublicKey(key []byte) DH { parsedKey, err := this.ParsePublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -88,14 +88,14 @@ func (this Dh) FromPublicKey(key []byte) Dh { } // 公钥 -func FromPublicKey(key []byte) Dh { +func FromPublicKey(key []byte) DH { return defaultDH.FromPublicKey(key) } // ========== // 根据密钥 x, y 生成 -func (this Dh) FromKeyXYHexString(xString string, yString string) Dh { +func (this DH) FromKeyXYHexString(xString string, yString string) DH { x, _ := new(big.Int).SetString(xString[:], 16) y, _ := new(big.Int).SetString(yString[:], 16) @@ -118,12 +118,12 @@ func (this Dh) FromKeyXYHexString(xString string, yString string) Dh { } // 根据私钥 x, y 生成 -func FromKeyXYHexString(xString string, yString string) Dh { +func FromKeyXYHexString(xString string, yString string) DH { return defaultDH.FromKeyXYHexString(xString, yString) } // 根据私钥 x 生成 -func (this Dh) FromPrivateKeyXHexString(xString string) Dh { +func (this DH) FromPrivateKeyXHexString(xString string) DH { x, _ := new(big.Int).SetString(xString[:], 16) group := this.group @@ -146,12 +146,12 @@ func (this Dh) FromPrivateKeyXHexString(xString string) Dh { } // 根据私钥 x 生成 -func FromPrivateKeyXHexString(xString string) Dh { +func FromPrivateKeyXHexString(xString string) DH { return defaultDH.FromPrivateKeyXHexString(xString) } // 根据公钥 y 生成 -func (this Dh) FromPublicKeyYHexString(yString string) Dh { +func (this DH) FromPublicKeyYHexString(yString string) DH { y, _ := new(big.Int).SetString(yString[:], 16) group := this.group @@ -171,14 +171,14 @@ func (this Dh) FromPublicKeyYHexString(yString string) Dh { } // 根据公钥 y 生成 -func FromPublicKeyYHexString(yString string) Dh { +func FromPublicKeyYHexString(yString string) DH { return defaultDH.FromPublicKeyYHexString(yString) } // ========== // DER 私钥 -func (this Dh) FromPrivateKeyDer(der []byte) Dh { +func (this DH) FromPrivateKeyDer(der []byte) DH { key := tool.EncodeDerToPem(der, "PRIVATE KEY") parsedKey, err := this.ParsePrivateKeyFromPEM(key) @@ -192,7 +192,7 @@ func (this Dh) FromPrivateKeyDer(der []byte) Dh { } // DER 公钥 -func (this Dh) FromPublicKeyDer(der []byte) Dh { +func (this DH) FromPublicKeyDer(der []byte) DH { key := tool.EncodeDerToPem(der, "PUBLIC KEY") parsedKey, err := this.ParsePublicKeyFromPEM(key) diff --git a/cryptobin/dh/dh/get.go b/cryptobin/dh/dh/get.go index 360a232a..808e5374 100644 --- a/cryptobin/dh/dh/get.go +++ b/cryptobin/dh/dh/get.go @@ -5,12 +5,12 @@ import ( ) // 获取 PrivateKey -func (this Dh) GetPrivateKey() *dh.PrivateKey { +func (this DH) GetPrivateKey() *dh.PrivateKey { return this.privateKey } // 获取 X 16进制字符 -func (this Dh) GetPrivateKeyXHexString() string { +func (this DH) GetPrivateKeyXHexString() string { if this.privateKey == nil { return "" } @@ -21,12 +21,12 @@ func (this Dh) GetPrivateKeyXHexString() string { } // 获取 PublicKey -func (this Dh) GetPublicKey() *dh.PublicKey { +func (this DH) GetPublicKey() *dh.PublicKey { return this.publicKey } // 获取 Y 16进制字符 -func (this Dh) GetPublicKeyYHexString() string { +func (this DH) GetPublicKeyYHexString() string { if this.publicKey == nil { return "" } @@ -37,7 +37,7 @@ func (this Dh) GetPublicKeyYHexString() string { } // 获取 P 16进制字符 -func (this Dh) GetPublicKeyParametersPHexString() string { +func (this DH) GetPublicKeyParametersPHexString() string { if this.publicKey == nil { return "" } @@ -48,7 +48,7 @@ func (this Dh) GetPublicKeyParametersPHexString() string { } // 获取 G 16进制字符 -func (this Dh) GetPublicKeyParametersGHexString() string { +func (this DH) GetPublicKeyParametersGHexString() string { if this.publicKey == nil { return "" } @@ -59,21 +59,21 @@ func (this Dh) GetPublicKeyParametersGHexString() string { } // 获取 keyData -func (this Dh) GetKeyData() []byte { +func (this DH) GetKeyData() []byte { return this.keyData } // 获取分组 -func (this Dh) GetGroup() *dh.Group { +func (this DH) GetGroup() *dh.Group { return this.group } // 获取 secretData -func (this Dh) GetSecretData() []byte { +func (this DH) GetSecretData() []byte { return this.secretData } // 获取错误 -func (this Dh) GetErrors() []error { +func (this DH) GetErrors() []error { return this.Errors } diff --git a/cryptobin/dh/dh/make.go b/cryptobin/dh/dh/make.go index a63b6100..059d691e 100644 --- a/cryptobin/dh/dh/make.go +++ b/cryptobin/dh/dh/make.go @@ -6,11 +6,11 @@ import ( ) // 生成公钥 -func (this Dh) MakePublicKey() Dh { +func (this DH) MakePublicKey() DH { this.publicKey = nil if this.privateKey == nil { - err := errors.New("Dh: privateKey error.") + err := errors.New("dh: privateKey error.") return this.AppendError(err) } @@ -21,10 +21,10 @@ func (this Dh) MakePublicKey() Dh { } // 生成密钥 der 数据 -func (this Dh) MakeKeyDer() Dh { +func (this DH) MakeKeyDer() DH { var block *pem.Block if block, _ = pem.Decode(this.keyData); block == nil { - err := errors.New("Dh: keyData error.") + err := errors.New("dh: keyData error.") return this.AppendError(err) } diff --git a/cryptobin/dh/dh/on.go b/cryptobin/dh/dh/on.go index 0b985374..e4794d51 100644 --- a/cryptobin/dh/dh/on.go +++ b/cryptobin/dh/dh/on.go @@ -6,7 +6,7 @@ type ( ) // 引出错误信息 -func (this Dh) OnError(fn ErrorFunc) Dh { +func (this DH) OnError(fn ErrorFunc) DH { fn(this.Errors) return this diff --git a/cryptobin/dh/dh/parse.go b/cryptobin/dh/dh/parse.go index 5a6020a3..306d6db1 100644 --- a/cryptobin/dh/dh/parse.go +++ b/cryptobin/dh/dh/parse.go @@ -17,7 +17,7 @@ var ( ) // 解析私钥 -func (this Dh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { +func (this DH) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -42,7 +42,7 @@ func (this Dh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { } // 解析私钥带密码 -func (this Dh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { +func (this DH) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -71,7 +71,7 @@ func (this Dh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) ( } // 解析公钥 -func (this Dh) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { +func (this DH) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { var err error // Parse PEM block diff --git a/cryptobin/dh/dh/to.go b/cryptobin/dh/dh/to.go index f955375e..a067fdda 100644 --- a/cryptobin/dh/dh/to.go +++ b/cryptobin/dh/dh/to.go @@ -5,33 +5,33 @@ import ( ) // 私钥/公钥 -func (this Dh) ToKeyBytes() []byte { +func (this DH) ToKeyBytes() []byte { return this.keyData } // 私钥/公钥 -func (this Dh) ToKeyString() string { +func (this DH) ToKeyString() string { return string(this.keyData) } // ================= // 输出字节 -func (this Dh) ToBytes() []byte { +func (this DH) ToBytes() []byte { return this.secretData } // 输出字符 -func (this Dh) ToString() string { +func (this DH) ToString() string { return string(this.secretData) } // 输出Base64 -func (this Dh) ToBase64String() string { +func (this DH) ToBase64String() string { return cryptobin_tool.NewEncoding().Base64Encode(this.secretData) } // 输出Hex -func (this Dh) ToHexString() string { +func (this DH) ToHexString() string { return cryptobin_tool.NewEncoding().HexEncode(this.secretData) } diff --git a/cryptobin/dh/dh/with.go b/cryptobin/dh/dh/with.go index dba94919..8ba8a177 100644 --- a/cryptobin/dh/dh/with.go +++ b/cryptobin/dh/dh/with.go @@ -7,28 +7,28 @@ import ( ) // 设置 PrivateKey -func (this Dh) WithPrivateKey(data *dh.PrivateKey) Dh { +func (this DH) WithPrivateKey(data *dh.PrivateKey) DH { this.privateKey = data return this } // 设置 PublicKey -func (this Dh) WithPublicKey(data *dh.PublicKey) Dh { +func (this DH) WithPublicKey(data *dh.PublicKey) DH { this.publicKey = data return this } // 设置分组 -func (this Dh) WithGroup(data *dh.Group) Dh { +func (this DH) WithGroup(data *dh.Group) DH { this.group = data return this } // 根据 Group 数据设置分组 -func (this Dh) SetGroup(name string) Dh { +func (this DH) SetGroup(name string) DH { var param dh.GroupID switch name { @@ -63,7 +63,7 @@ func (this Dh) SetGroup(name string) Dh { } // 根据 Group P和G 数据设置分组 -func (this Dh) SetGroupPG(p string, g int64) Dh { +func (this DH) SetGroupPG(p string, g int64) DH { pInt, _ := new(big.Int).SetString(p, 16) this.group = &dh.Group{ @@ -75,7 +75,7 @@ func (this Dh) SetGroupPG(p string, g int64) Dh { } // 随机数 -func (this Dh) SetRandGroup(num int64) Dh { +func (this DH) SetRandGroup(num int64) DH { hexLetters := []rune("0123456789abcdef") // p 值 @@ -92,21 +92,21 @@ func (this Dh) SetRandGroup(num int64) Dh { } // 设置 keyData -func (this Dh) WithKeyData(data []byte) Dh { +func (this DH) WithKeyData(data []byte) DH { this.keyData = data return this } // 设置 secretData -func (this Dh) WithSecretData(data []byte) Dh { +func (this DH) WithSecretData(data []byte) DH { this.secretData = data return this } // 设置错误 -func (this Dh) WithError(errs []error) Dh { +func (this DH) WithError(errs []error) DH { this.Errors = errs return this diff --git a/cryptobin/dh/ecdh/check.go b/cryptobin/dh/ecdh/check.go index 73c516a5..689c76fe 100644 --- a/cryptobin/dh/ecdh/check.go +++ b/cryptobin/dh/ecdh/check.go @@ -1,7 +1,7 @@ package ecdh // 检测公钥私钥是否匹配 -func (this Ecdh) CheckKeyPair() bool { +func (this ECDH) CheckKeyPair() bool { // 私钥导出的公钥 pubKeyFromPriKey := this.MakePublicKey(). CreatePublicKey(). diff --git a/cryptobin/dh/ecdh/create.go b/cryptobin/dh/ecdh/create.go index 505a8b33..a62a5970 100644 --- a/cryptobin/dh/ecdh/create.go +++ b/cryptobin/dh/ecdh/create.go @@ -29,9 +29,9 @@ var ( // 使用: // obj := New().GenerateKey("P2048") // priKey := obj.CreatePrivateKey().ToKeyString() -func (this Ecdh) CreatePrivateKey() Ecdh { +func (this ECDH) CreatePrivateKey() ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -52,9 +52,9 @@ func (this Ecdh) CreatePrivateKey() Ecdh { // 生成 PKCS8 私钥带密码 pem 数据 // CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256") -func (this Ecdh) CreatePrivateKeyWithPassword(password string, opts ...any) Ecdh { +func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -87,9 +87,9 @@ func (this Ecdh) CreatePrivateKeyWithPassword(password string, opts ...any) Ecdh } // 生成公钥 pem 数据 -func (this Ecdh) CreatePublicKey() Ecdh { +func (this ECDH) CreatePublicKey() ECDH { if this.publicKey == nil { - err := errors.New("Ecdh: publicKey error.") + err := errors.New("ecdh: publicKey error.") return this.AppendError(err) } @@ -109,14 +109,14 @@ func (this Ecdh) CreatePublicKey() Ecdh { } // 根据公钥和私钥生成密钥 -func (this Ecdh) CreateSecretKey() Ecdh { +func (this ECDH) CreateSecretKey() ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } if this.publicKey == nil { - err := errors.New("Ecdh: publicKey error.") + err := errors.New("ecdh: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/dh/ecdh/ecdh.go b/cryptobin/dh/ecdh/ecdh.go index 501e5a84..64e5ecd8 100644 --- a/cryptobin/dh/ecdh/ecdh.go +++ b/cryptobin/dh/ecdh/ecdh.go @@ -10,7 +10,7 @@ import ( * @create 2022-8-7 * @author deatil */ -type Ecdh struct { +type ECDH struct { // 私钥 privateKey *ecdh.PrivateKey @@ -31,17 +31,17 @@ type Ecdh struct { } // 构造函数 -func NewEcdh() Ecdh { +func NewEcdh() ECDH { curve := ecdh.P256() - return Ecdh{ + return ECDH{ curve: curve, Errors: make([]error, 0), } } // 构造函数 -func New() Ecdh { +func New() ECDH { return NewEcdh() } diff --git a/cryptobin/dh/ecdh/error.go b/cryptobin/dh/ecdh/error.go index 420597fd..2fcf6ebf 100644 --- a/cryptobin/dh/ecdh/error.go +++ b/cryptobin/dh/ecdh/error.go @@ -5,13 +5,13 @@ import ( ) // 添加错误 -func (this Ecdh) AppendError(err ...error) Ecdh { +func (this ECDH) AppendError(err ...error) ECDH { this.Errors = append(this.Errors, err...) return this } // 获取错误 -func (this Ecdh) Error() error { +func (this ECDH) Error() error { return cryptobin_tool.NewError(this.Errors...) } diff --git a/cryptobin/dh/ecdh/from.go b/cryptobin/dh/ecdh/from.go index 4585545d..d610d0c9 100644 --- a/cryptobin/dh/ecdh/from.go +++ b/cryptobin/dh/ecdh/from.go @@ -9,7 +9,7 @@ import ( ) // 生成密钥 -func (this Ecdh) GenerateKey() Ecdh { +func (this ECDH) GenerateKey() ECDH { privateKey, publicKey, err := ecdh.GenerateKey(this.curve, rand.Reader) this.privateKey = privateKey @@ -20,12 +20,12 @@ func (this Ecdh) GenerateKey() Ecdh { // 生成密钥 // 可用参数 [P521 | P384 | P256 | P224] -func GenerateKey(curve string) Ecdh { +func GenerateKey(curve string) ECDH { return defaultECDH.SetCurve(curve).GenerateKey() } // 生成密钥 -func (this Ecdh) GenerateKeyWithSeed(reader io.Reader) Ecdh { +func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH { privateKey, publicKey, err := ecdh.GenerateKey(this.curve, reader) this.privateKey = privateKey @@ -36,14 +36,14 @@ func (this Ecdh) GenerateKeyWithSeed(reader io.Reader) Ecdh { // 生成密钥 // 可用参数 [P521 | P384 | P256 | P224] -func GenerateKeyWithSeed(reader io.Reader, curve string) Ecdh { +func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH { return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader) } // ========== // 私钥 -func (this Ecdh) FromPrivateKey(key []byte) Ecdh { +func (this ECDH) FromPrivateKey(key []byte) ECDH { parsedKey, err := this.ParsePrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -55,12 +55,12 @@ func (this Ecdh) FromPrivateKey(key []byte) Ecdh { } // 私钥 -func FromPrivateKey(key []byte) Ecdh { +func FromPrivateKey(key []byte) ECDH { return defaultECDH.FromPrivateKey(key) } // 私钥带密码 -func (this Ecdh) FromPrivateKeyWithPassword(key []byte, password string) Ecdh { +func (this ECDH) FromPrivateKeyWithPassword(key []byte, password string) ECDH { parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -72,12 +72,12 @@ func (this Ecdh) FromPrivateKeyWithPassword(key []byte, password string) Ecdh { } // 私钥 -func FromPrivateKeyWithPassword(key []byte, password string) Ecdh { +func FromPrivateKeyWithPassword(key []byte, password string) ECDH { return defaultECDH.FromPrivateKeyWithPassword(key, password) } // 公钥 -func (this Ecdh) FromPublicKey(key []byte) Ecdh { +func (this ECDH) FromPublicKey(key []byte) ECDH { parsedKey, err := this.ParsePublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -89,14 +89,14 @@ func (this Ecdh) FromPublicKey(key []byte) Ecdh { } // 公钥 -func FromPublicKey(key []byte) Ecdh { +func FromPublicKey(key []byte) ECDH { return defaultECDH.FromPublicKey(key) } // ========== // 根据私钥 x, y 生成 -func (this Ecdh) FromKeyXYHexString(xString string, yString string) Ecdh { +func (this ECDH) FromKeyXYHexString(xString string, yString string) ECDH { encoding := tool.NewEncoding() x, _ := encoding.HexDecode(xString) @@ -114,12 +114,12 @@ func (this Ecdh) FromKeyXYHexString(xString string, yString string) Ecdh { } // 根据私钥 x, y 生成 -func FromKeyXYHexString(xString string, yString string) Ecdh { +func FromKeyXYHexString(xString string, yString string) ECDH { return defaultECDH.FromKeyXYHexString(xString, yString) } // 根据私钥 x 生成 -func (this Ecdh) FromPrivateKeyXHexString(xString string) Ecdh { +func (this ECDH) FromPrivateKeyXHexString(xString string) ECDH { encoding := tool.NewEncoding() x, _ := encoding.HexDecode(xString) @@ -137,12 +137,12 @@ func (this Ecdh) FromPrivateKeyXHexString(xString string) Ecdh { } // 根据私钥 x 生成 -func FromPrivateKeyXHexString(xString string) Ecdh { +func FromPrivateKeyXHexString(xString string) ECDH { return defaultECDH.FromPrivateKeyXHexString(xString) } // 根据公钥 y 生成 -func (this Ecdh) FromPublicKeyYHexString(yString string) Ecdh { +func (this ECDH) FromPublicKeyYHexString(yString string) ECDH { encoding := tool.NewEncoding() y, _ := encoding.HexDecode(yString) @@ -157,14 +157,14 @@ func (this Ecdh) FromPublicKeyYHexString(yString string) Ecdh { } // 根据公钥 y 生成 -func FromPublicKeyYHexString(yString string) Ecdh { +func FromPublicKeyYHexString(yString string) ECDH { return defaultECDH.FromPublicKeyYHexString(yString) } // ========== // DER 私钥 -func (this Ecdh) FromPrivateKeyDer(der []byte) Ecdh { +func (this ECDH) FromPrivateKeyDer(der []byte) ECDH { key := tool.EncodeDerToPem(der, "PRIVATE KEY") parsedKey, err := this.ParsePrivateKeyFromPEM(key) @@ -178,7 +178,7 @@ func (this Ecdh) FromPrivateKeyDer(der []byte) Ecdh { } // DER 公钥 -func (this Ecdh) FromPublicKeyDer(der []byte) Ecdh { +func (this ECDH) FromPublicKeyDer(der []byte) ECDH { key := tool.EncodeDerToPem(der, "PUBLIC KEY") parsedKey, err := this.ParsePublicKeyFromPEM(key) diff --git a/cryptobin/dh/ecdh/get.go b/cryptobin/dh/ecdh/get.go index 10b9a39c..c53bc802 100644 --- a/cryptobin/dh/ecdh/get.go +++ b/cryptobin/dh/ecdh/get.go @@ -7,12 +7,12 @@ import ( ) // 获取 PrivateKey -func (this Ecdh) GetPrivateKey() *ecdh.PrivateKey { +func (this ECDH) GetPrivateKey() *ecdh.PrivateKey { return this.privateKey } // 获取 X 16进制字符 -func (this Ecdh) GetPrivateKeyXHexString() string { +func (this ECDH) GetPrivateKeyXHexString() string { data := this.privateKey.X dataHex := cryptobin_tool. @@ -23,12 +23,12 @@ func (this Ecdh) GetPrivateKeyXHexString() string { } // 获取 PublicKey -func (this Ecdh) GetPublicKey() *ecdh.PublicKey { +func (this ECDH) GetPublicKey() *ecdh.PublicKey { return this.publicKey } // 获取 Y 16进制字符 -func (this Ecdh) GetPublicKeyYHexString() string { +func (this ECDH) GetPublicKeyYHexString() string { data := this.publicKey.Y dataHex := cryptobin_tool. @@ -39,21 +39,21 @@ func (this Ecdh) GetPublicKeyYHexString() string { } // 获取散列方式 -func (this Ecdh) GetCurve() ecdh.Curve { +func (this ECDH) GetCurve() ecdh.Curve { return this.curve } // 获取 keyData -func (this Ecdh) GetKeyData() []byte { +func (this ECDH) GetKeyData() []byte { return this.keyData } // 获取 secretData -func (this Ecdh) GetSecretData() []byte { +func (this ECDH) GetSecretData() []byte { return this.secretData } // 获取错误 -func (this Ecdh) GetErrors() []error { +func (this ECDH) GetErrors() []error { return this.Errors } diff --git a/cryptobin/dh/ecdh/make.go b/cryptobin/dh/ecdh/make.go index d06edd04..390ea7a5 100644 --- a/cryptobin/dh/ecdh/make.go +++ b/cryptobin/dh/ecdh/make.go @@ -6,11 +6,11 @@ import ( ) // 生成公钥 -func (this Ecdh) MakePublicKey() Ecdh { +func (this ECDH) MakePublicKey() ECDH { this.publicKey = nil if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -21,10 +21,10 @@ func (this Ecdh) MakePublicKey() Ecdh { } // 生成密钥 der 数据 -func (this Ecdh) MakeKeyDer() Ecdh { +func (this ECDH) MakeKeyDer() ECDH { var block *pem.Block if block, _ = pem.Decode(this.keyData); block == nil { - err := errors.New("Ecdh: keyData error.") + err := errors.New("ecdh: keyData error.") return this.AppendError(err) } diff --git a/cryptobin/dh/ecdh/on.go b/cryptobin/dh/ecdh/on.go index 1d563064..0516eef8 100644 --- a/cryptobin/dh/ecdh/on.go +++ b/cryptobin/dh/ecdh/on.go @@ -6,7 +6,7 @@ type ( ) // 引出错误信息 -func (this Ecdh) OnError(fn ErrorFunc) Ecdh { +func (this ECDH) OnError(fn ErrorFunc) ECDH { fn(this.Errors) return this diff --git a/cryptobin/dh/ecdh/parse.go b/cryptobin/dh/ecdh/parse.go index 4e7acee3..d4a7800d 100644 --- a/cryptobin/dh/ecdh/parse.go +++ b/cryptobin/dh/ecdh/parse.go @@ -17,7 +17,7 @@ var ( ) // 解析私钥 -func (this Ecdh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { +func (this ECDH) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -42,7 +42,7 @@ func (this Ecdh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { } // 解析私钥带密码 -func (this Ecdh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { +func (this ECDH) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -71,7 +71,7 @@ func (this Ecdh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) } // 解析公钥 -func (this Ecdh) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { +func (this ECDH) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { var err error // Parse PEM block diff --git a/cryptobin/dh/ecdh/to.go b/cryptobin/dh/ecdh/to.go index 646bff59..64f6e07a 100644 --- a/cryptobin/dh/ecdh/to.go +++ b/cryptobin/dh/ecdh/to.go @@ -5,33 +5,33 @@ import ( ) // 私钥/公钥 -func (this Ecdh) ToKeyBytes() []byte { +func (this ECDH) ToKeyBytes() []byte { return this.keyData } // 私钥/公钥 -func (this Ecdh) ToKeyString() string { +func (this ECDH) ToKeyString() string { return string(this.keyData) } // ================= // 输出字节 -func (this Ecdh) ToBytes() []byte { +func (this ECDH) ToBytes() []byte { return this.secretData } // 输出字符 -func (this Ecdh) ToString() string { +func (this ECDH) ToString() string { return string(this.secretData) } // 输出Base64 -func (this Ecdh) ToBase64String() string { +func (this ECDH) ToBase64String() string { return cryptobin_tool.NewEncoding().Base64Encode(this.secretData) } // 输出Hex -func (this Ecdh) ToHexString() string { +func (this ECDH) ToHexString() string { return cryptobin_tool.NewEncoding().HexEncode(this.secretData) } diff --git a/cryptobin/dh/ecdh/with.go b/cryptobin/dh/ecdh/with.go index d4937b1f..e4fea339 100644 --- a/cryptobin/dh/ecdh/with.go +++ b/cryptobin/dh/ecdh/with.go @@ -5,21 +5,21 @@ import ( ) // 设置 PrivateKey -func (this Ecdh) WithPrivateKey(data *ecdh.PrivateKey) Ecdh { +func (this ECDH) WithPrivateKey(data *ecdh.PrivateKey) ECDH { this.privateKey = data return this } // 设置 PublicKey -func (this Ecdh) WithPublicKey(data *ecdh.PublicKey) Ecdh { +func (this ECDH) WithPublicKey(data *ecdh.PublicKey) ECDH { this.publicKey = data return this } // 设置散列方式 -func (this Ecdh) WithCurve(data ecdh.Curve) Ecdh { +func (this ECDH) WithCurve(data ecdh.Curve) ECDH { this.curve = data return this @@ -27,7 +27,7 @@ func (this Ecdh) WithCurve(data ecdh.Curve) Ecdh { // 设置散列方式 // 可用参数 [P521 | P384 | P256 | P224] -func (this Ecdh) SetCurve(name string) Ecdh { +func (this ECDH) SetCurve(name string) ECDH { var curve ecdh.Curve switch name { @@ -49,21 +49,21 @@ func (this Ecdh) SetCurve(name string) Ecdh { } // 设置 keyData -func (this Ecdh) WithKeyData(data []byte) Ecdh { +func (this ECDH) WithKeyData(data []byte) ECDH { this.keyData = data return this } // 设置 secretData -func (this Ecdh) WithSecretData(data []byte) Ecdh { +func (this ECDH) WithSecretData(data []byte) ECDH { this.secretData = data return this } // 设置错误 -func (this Ecdh) WithErrors(errs []error) Ecdh { +func (this ECDH) WithErrors(errs []error) ECDH { this.Errors = errs return this diff --git a/cryptobin/ecdh/check.go b/cryptobin/ecdh/check.go index 73c516a5..689c76fe 100644 --- a/cryptobin/ecdh/check.go +++ b/cryptobin/ecdh/check.go @@ -1,7 +1,7 @@ package ecdh // 检测公钥私钥是否匹配 -func (this Ecdh) CheckKeyPair() bool { +func (this ECDH) CheckKeyPair() bool { // 私钥导出的公钥 pubKeyFromPriKey := this.MakePublicKey(). CreatePublicKey(). diff --git a/cryptobin/ecdh/create.go b/cryptobin/ecdh/create.go index ab9267ec..8664795a 100644 --- a/cryptobin/ecdh/create.go +++ b/cryptobin/ecdh/create.go @@ -31,9 +31,9 @@ var ( // 使用: // obj := New().SetCurve("P256").GenerateKey() // priKey := obj.CreatePrivateKey().ToKeyString() -func (this Ecdh) CreatePrivateKey() Ecdh { +func (this ECDH) CreatePrivateKey() ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -54,9 +54,9 @@ func (this Ecdh) CreatePrivateKey() Ecdh { // 生成 PKCS8 私钥带密码 pem 数据 // CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256") -func (this Ecdh) CreatePrivateKeyWithPassword(password string, opts ...any) Ecdh { +func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -89,9 +89,9 @@ func (this Ecdh) CreatePrivateKeyWithPassword(password string, opts ...any) Ecdh } // 生成公钥 pem 数据 -func (this Ecdh) CreatePublicKey() Ecdh { +func (this ECDH) CreatePublicKey() ECDH { if this.publicKey == nil { - err := errors.New("Ecdh: publicKey error.") + err := errors.New("ecdh: publicKey error.") return this.AppendError(err) } @@ -113,9 +113,9 @@ func (this Ecdh) CreatePublicKey() Ecdh { // ======================= // 生成私钥 pem 数据, 库自使用的 asn1 格式 -func (this Ecdh) CreateECDHPrivateKey() Ecdh { +func (this ECDH) CreateECDHPrivateKey() ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -140,9 +140,9 @@ func (this Ecdh) CreateECDHPrivateKey() Ecdh { } // 生成 PKCS8 私钥带密码 pem 数据, 库自使用的 asn1 格式 -func (this Ecdh) CreateECDHPrivateKeyWithPassword(password string, opts ...any) Ecdh { +func (this ECDH) CreateECDHPrivateKeyWithPassword(password string, opts ...any) ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -180,9 +180,9 @@ func (this Ecdh) CreateECDHPrivateKeyWithPassword(password string, opts ...any) } // 生成公钥 pem 数据, 库自使用的 asn1 格式 -func (this Ecdh) CreateECDHPublicKey() Ecdh { +func (this ECDH) CreateECDHPublicKey() ECDH { if this.publicKey == nil { - err := errors.New("Ecdh: publicKey error.") + err := errors.New("ecdh: publicKey error.") return this.AppendError(err) } @@ -209,14 +209,14 @@ func (this Ecdh) CreateECDHPublicKey() Ecdh { // ======================= // 根据公钥和私钥生成对称密钥 -func (this Ecdh) CreateSecretKey() Ecdh { +func (this ECDH) CreateSecretKey() ECDH { if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } if this.publicKey == nil { - err := errors.New("Ecdh: publicKey error.") + err := errors.New("ecdh: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/ecdh/ecdh.go b/cryptobin/ecdh/ecdh.go index 39fe3d13..cb39396f 100644 --- a/cryptobin/ecdh/ecdh.go +++ b/cryptobin/ecdh/ecdh.go @@ -11,7 +11,7 @@ import ( * @create 2022-8-7 * @author deatil */ -type Ecdh struct { +type ECDH struct { // 私钥 privateKey *ecdh.PrivateKey @@ -32,19 +32,19 @@ type Ecdh struct { } // 构造函数 -func NewEcdh() Ecdh { - return Ecdh{ +func NewECDH() ECDH { + return ECDH{ curve: ecdh.P256(), Errors: make([]error, 0), } } // 构造函数 -func New() Ecdh { - return NewEcdh() +func New() ECDH { + return NewECDH() } var ( // 默认 - defaultECDH = NewEcdh() + defaultECDH = NewECDH() ) diff --git a/cryptobin/ecdh/error.go b/cryptobin/ecdh/error.go index 420597fd..2fcf6ebf 100644 --- a/cryptobin/ecdh/error.go +++ b/cryptobin/ecdh/error.go @@ -5,13 +5,13 @@ import ( ) // 添加错误 -func (this Ecdh) AppendError(err ...error) Ecdh { +func (this ECDH) AppendError(err ...error) ECDH { this.Errors = append(this.Errors, err...) return this } // 获取错误 -func (this Ecdh) Error() error { +func (this ECDH) Error() error { return cryptobin_tool.NewError(this.Errors...) } diff --git a/cryptobin/ecdh/from.go b/cryptobin/ecdh/from.go index 72d5b6bf..94456d78 100644 --- a/cryptobin/ecdh/from.go +++ b/cryptobin/ecdh/from.go @@ -9,7 +9,7 @@ import ( ) // 生成密钥 -func (this Ecdh) GenerateKey() Ecdh { +func (this ECDH) GenerateKey() ECDH { privateKey, err := this.curve.GenerateKey(rand.Reader) if err != nil { return this.AppendError(err) @@ -22,12 +22,12 @@ func (this Ecdh) GenerateKey() Ecdh { } // 生成密钥 -func GenerateKey(curve string) Ecdh { +func GenerateKey(curve string) ECDH { return defaultECDH.SetCurve(curve).GenerateKey() } // 生成密钥 -func (this Ecdh) GenerateKeyWithSeed(reader io.Reader) Ecdh { +func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH { privateKey, err := this.curve.GenerateKey(reader) if err != nil { return this.AppendError(err) @@ -40,14 +40,14 @@ func (this Ecdh) GenerateKeyWithSeed(reader io.Reader) Ecdh { } // 生成密钥 -func GenerateKeyWithSeed(reader io.Reader, curve string) Ecdh { +func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH { return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader) } // ========== // 私钥 -func (this Ecdh) FromPrivateKey(key []byte) Ecdh { +func (this ECDH) FromPrivateKey(key []byte) ECDH { parsedKey, err := this.ParsePrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -59,12 +59,12 @@ func (this Ecdh) FromPrivateKey(key []byte) Ecdh { } // 私钥 -func FromPrivateKey(key []byte) Ecdh { +func FromPrivateKey(key []byte) ECDH { return defaultECDH.FromPrivateKey(key) } // 私钥带密码 -func (this Ecdh) FromPrivateKeyWithPassword(key []byte, password string) Ecdh { +func (this ECDH) FromPrivateKeyWithPassword(key []byte, password string) ECDH { parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -76,12 +76,12 @@ func (this Ecdh) FromPrivateKeyWithPassword(key []byte, password string) Ecdh { } // 私钥 -func FromPrivateKeyWithPassword(key []byte, password string) Ecdh { +func FromPrivateKeyWithPassword(key []byte, password string) ECDH { return defaultECDH.FromPrivateKeyWithPassword(key, password) } // 公钥 -func (this Ecdh) FromPublicKey(key []byte) Ecdh { +func (this ECDH) FromPublicKey(key []byte) ECDH { parsedKey, err := this.ParsePublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -93,14 +93,14 @@ func (this Ecdh) FromPublicKey(key []byte) Ecdh { } // 公钥 -func FromPublicKey(key []byte) Ecdh { +func FromPublicKey(key []byte) ECDH { return defaultECDH.FromPublicKey(key) } // ========== // DER 私钥 -func (this Ecdh) FromPrivateKeyDer(der []byte) Ecdh { +func (this ECDH) FromPrivateKeyDer(der []byte) ECDH { key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY") parsedKey, err := this.ParsePrivateKeyFromPEM(key) @@ -114,7 +114,7 @@ func (this Ecdh) FromPrivateKeyDer(der []byte) Ecdh { } // DER 公钥 -func (this Ecdh) FromPublicKeyDer(der []byte) Ecdh { +func (this ECDH) FromPublicKeyDer(der []byte) ECDH { key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY") parsedKey, err := this.ParsePublicKeyFromPEM(key) @@ -130,7 +130,7 @@ func (this Ecdh) FromPublicKeyDer(der []byte) Ecdh { // ========== // 私钥, 库自使用的 asn1 格式 -func (this Ecdh) FromECDHPrivateKey(key []byte) Ecdh { +func (this ECDH) FromECDHPrivateKey(key []byte) ECDH { parsedKey, err := this.ParseECDHPrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -142,12 +142,12 @@ func (this Ecdh) FromECDHPrivateKey(key []byte) Ecdh { } // 私钥, 库自使用的 asn1 格式 -func FromECDHPrivateKey(key []byte) Ecdh { +func FromECDHPrivateKey(key []byte) ECDH { return defaultECDH.FromECDHPrivateKey(key) } // 私钥带密码, 库自使用的 asn1 格式 -func (this Ecdh) FromECDHPrivateKeyWithPassword(key []byte, password string) Ecdh { +func (this ECDH) FromECDHPrivateKeyWithPassword(key []byte, password string) ECDH { parsedKey, err := this.ParseECDHPrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -159,12 +159,12 @@ func (this Ecdh) FromECDHPrivateKeyWithPassword(key []byte, password string) Ecd } // 私钥, 库自使用的 asn1 格式 -func FromECDHPrivateKeyWithPassword(key []byte, password string) Ecdh { +func FromECDHPrivateKeyWithPassword(key []byte, password string) ECDH { return defaultECDH.FromECDHPrivateKeyWithPassword(key, password) } // 公钥, 库自使用的 asn1 格式 -func (this Ecdh) FromECDHPublicKey(key []byte) Ecdh { +func (this ECDH) FromECDHPublicKey(key []byte) ECDH { parsedKey, err := this.ParseECDHPublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -176,14 +176,14 @@ func (this Ecdh) FromECDHPublicKey(key []byte) Ecdh { } // 公钥, 库自使用的 asn1 格式 -func FromECDHPublicKey(key []byte) Ecdh { +func FromECDHPublicKey(key []byte) ECDH { return defaultECDH.FromECDHPublicKey(key) } // ========== // DER 私钥, 库自使用的 asn1 格式 -func (this Ecdh) FromECDHPrivateKeyDer(der []byte) Ecdh { +func (this ECDH) FromECDHPrivateKeyDer(der []byte) ECDH { key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY") parsedKey, err := this.ParseECDHPrivateKeyFromPEM(key) @@ -197,7 +197,7 @@ func (this Ecdh) FromECDHPrivateKeyDer(der []byte) Ecdh { } // DER 公钥, 库自使用的 asn1 格式 -func (this Ecdh) FromECDHPublicKeyDer(der []byte) Ecdh { +func (this ECDH) FromECDHPublicKeyDer(der []byte) ECDH { key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY") parsedKey, err := this.ParseECDHPublicKeyFromPEM(key) diff --git a/cryptobin/ecdh/get.go b/cryptobin/ecdh/get.go index 37f2d50b..6626f0a0 100644 --- a/cryptobin/ecdh/get.go +++ b/cryptobin/ecdh/get.go @@ -5,31 +5,31 @@ import ( ) // 获取 PrivateKey -func (this Ecdh) GetPrivateKey() *ecdh.PrivateKey { +func (this ECDH) GetPrivateKey() *ecdh.PrivateKey { return this.privateKey } // 获取 PublicKey -func (this Ecdh) GetPublicKey() *ecdh.PublicKey { +func (this ECDH) GetPublicKey() *ecdh.PublicKey { return this.publicKey } // 获取散列方式 -func (this Ecdh) GetCurve() ecdh.Curve { +func (this ECDH) GetCurve() ecdh.Curve { return this.curve } // 获取 keyData -func (this Ecdh) GetKeyData() []byte { +func (this ECDH) GetKeyData() []byte { return this.keyData } // 获取 secretData -func (this Ecdh) GetSecretData() []byte { +func (this ECDH) GetSecretData() []byte { return this.secretData } // 获取错误 -func (this Ecdh) GetErrors() []error { +func (this ECDH) GetErrors() []error { return this.Errors } diff --git a/cryptobin/ecdh/make.go b/cryptobin/ecdh/make.go index a680ba89..7a1809f9 100644 --- a/cryptobin/ecdh/make.go +++ b/cryptobin/ecdh/make.go @@ -6,11 +6,11 @@ import ( ) // 生成公钥 -func (this Ecdh) MakePublicKey() Ecdh { +func (this ECDH) MakePublicKey() ECDH { this.publicKey = nil if this.privateKey == nil { - err := errors.New("Ecdh: privateKey error.") + err := errors.New("ecdh: privateKey error.") return this.AppendError(err) } @@ -21,10 +21,10 @@ func (this Ecdh) MakePublicKey() Ecdh { } // 生成密钥 der 数据 -func (this Ecdh) MakeKeyDer() Ecdh { +func (this ECDH) MakeKeyDer() ECDH { var block *pem.Block if block, _ = pem.Decode(this.keyData); block == nil { - err := errors.New("Ecdh: keyData error.") + err := errors.New("ecdh: keyData error.") return this.AppendError(err) } diff --git a/cryptobin/ecdh/on.go b/cryptobin/ecdh/on.go index 1d563064..0516eef8 100644 --- a/cryptobin/ecdh/on.go +++ b/cryptobin/ecdh/on.go @@ -6,7 +6,7 @@ type ( ) // 引出错误信息 -func (this Ecdh) OnError(fn ErrorFunc) Ecdh { +func (this ECDH) OnError(fn ErrorFunc) ECDH { fn(this.Errors) return this diff --git a/cryptobin/ecdh/parse.go b/cryptobin/ecdh/parse.go index 58dc8c19..04d1921f 100644 --- a/cryptobin/ecdh/parse.go +++ b/cryptobin/ecdh/parse.go @@ -20,7 +20,7 @@ var ( ) // 解析私钥 -func (this Ecdh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { +func (this ECDH) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -51,7 +51,7 @@ func (this Ecdh) ParsePrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { } // 解析私钥带密码 -func (this Ecdh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { +func (this ECDH) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -86,7 +86,7 @@ func (this Ecdh) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) } // 解析公钥 -func (this Ecdh) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { +func (this ECDH) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { var err error // Parse PEM block @@ -123,7 +123,7 @@ func (this Ecdh) ParsePublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { // ========================================== // 解析私钥 -func (this Ecdh) ParseECDHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { +func (this ECDH) ParseECDHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -149,7 +149,7 @@ func (this Ecdh) ParseECDHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, erro } // 解析私钥带密码 -func (this Ecdh) ParseECDHPrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { +func (this ECDH) ParseECDHPrivateKeyFromPEMWithPassword(key []byte, password string) (crypto.PrivateKey, error) { var err error // Parse PEM block @@ -179,7 +179,7 @@ func (this Ecdh) ParseECDHPrivateKeyFromPEMWithPassword(key []byte, password str } // 解析公钥 -func (this Ecdh) ParseECDHPublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { +func (this ECDH) ParseECDHPublicKeyFromPEM(key []byte) (crypto.PublicKey, error) { var err error // Parse PEM block diff --git a/cryptobin/ecdh/to.go b/cryptobin/ecdh/to.go index 646bff59..64f6e07a 100644 --- a/cryptobin/ecdh/to.go +++ b/cryptobin/ecdh/to.go @@ -5,33 +5,33 @@ import ( ) // 私钥/公钥 -func (this Ecdh) ToKeyBytes() []byte { +func (this ECDH) ToKeyBytes() []byte { return this.keyData } // 私钥/公钥 -func (this Ecdh) ToKeyString() string { +func (this ECDH) ToKeyString() string { return string(this.keyData) } // ================= // 输出字节 -func (this Ecdh) ToBytes() []byte { +func (this ECDH) ToBytes() []byte { return this.secretData } // 输出字符 -func (this Ecdh) ToString() string { +func (this ECDH) ToString() string { return string(this.secretData) } // 输出Base64 -func (this Ecdh) ToBase64String() string { +func (this ECDH) ToBase64String() string { return cryptobin_tool.NewEncoding().Base64Encode(this.secretData) } // 输出Hex -func (this Ecdh) ToHexString() string { +func (this ECDH) ToHexString() string { return cryptobin_tool.NewEncoding().HexEncode(this.secretData) } diff --git a/cryptobin/ecdh/with.go b/cryptobin/ecdh/with.go index 73579d9b..e6a3c0cc 100644 --- a/cryptobin/ecdh/with.go +++ b/cryptobin/ecdh/with.go @@ -5,21 +5,21 @@ import ( ) // 设置 PrivateKey -func (this Ecdh) WithPrivateKey(data *ecdh.PrivateKey) Ecdh { +func (this ECDH) WithPrivateKey(data *ecdh.PrivateKey) ECDH { this.privateKey = data return this } // 设置 PublicKey -func (this Ecdh) WithPublicKey(data *ecdh.PublicKey) Ecdh { +func (this ECDH) WithPublicKey(data *ecdh.PublicKey) ECDH { this.publicKey = data return this } // 设置散列方式 -func (this Ecdh) WithCurve(data ecdh.Curve) Ecdh { +func (this ECDH) WithCurve(data ecdh.Curve) ECDH { this.curve = data return this @@ -27,7 +27,7 @@ func (this Ecdh) WithCurve(data ecdh.Curve) Ecdh { // 设置散列方式 // 可用参数 [P521 | P384 | P256 | X25519] -func (this Ecdh) SetCurve(curve string) Ecdh { +func (this ECDH) SetCurve(curve string) ECDH { switch curve { case "P521": this.curve = ecdh.P521() @@ -43,21 +43,21 @@ func (this Ecdh) SetCurve(curve string) Ecdh { } // 设置 keyData -func (this Ecdh) WithKeyData(data []byte) Ecdh { +func (this ECDH) WithKeyData(data []byte) ECDH { this.keyData = data return this } // 设置 secretData -func (this Ecdh) WithSecretData(data []byte) Ecdh { +func (this ECDH) WithSecretData(data []byte) ECDH { this.secretData = data return this } // 设置错误 -func (this Ecdh) WithErrors(errs []error) Ecdh { +func (this ECDH) WithErrors(errs []error) ECDH { this.Errors = errs return this diff --git a/cryptobin/ecdsa/check.go b/cryptobin/ecdsa/check.go index ec3aae24..ba54457d 100644 --- a/cryptobin/ecdsa/check.go +++ b/cryptobin/ecdsa/check.go @@ -1,7 +1,7 @@ package ecdsa // 检测公钥私钥是否匹配 -func (this Ecdsa) CheckKeyPair() bool { +func (this ECDSA) CheckKeyPair() bool { // 私钥导出的公钥 pubKeyFromPriKey := this.MakePublicKey(). CreatePublicKey(). diff --git a/cryptobin/ecdsa/create.go b/cryptobin/ecdsa/create.go index b84132c9..98ee5dd5 100644 --- a/cryptobin/ecdsa/create.go +++ b/cryptobin/ecdsa/create.go @@ -30,23 +30,23 @@ var ( // 使用: // obj := New().WithCurve("P521").GenerateKey() // priKey := obj.CreatePrivateKey().ToKeyString() -func (this Ecdsa) CreatePrivateKey() Ecdsa { +func (this ECDSA) CreatePrivateKey() ECDSA { return this.CreatePKCS1PrivateKey() } // 生成私钥带密码 pem 数据, PKCS1 别名 // CreatePrivateKeyWithPassword("123", "AES256CBC") // PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC -func (this Ecdsa) CreatePrivateKeyWithPassword(password string, opts ...string) Ecdsa { +func (this ECDSA) CreatePrivateKeyWithPassword(password string, opts ...string) ECDSA { return this.CreatePKCS1PrivateKeyWithPassword(password, opts...) } // ==================== // 生成私钥 pem 数据 -func (this Ecdsa) CreatePKCS1PrivateKey() Ecdsa { +func (this ECDSA) CreatePKCS1PrivateKey() ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -66,9 +66,9 @@ func (this Ecdsa) CreatePKCS1PrivateKey() Ecdsa { } // 生成私钥带密码 pem 数据 -func (this Ecdsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) Ecdsa { +func (this ECDSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -80,7 +80,7 @@ func (this Ecdsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str // 加密方式 cipher := cryptobin_pkcs1.GetPEMCipher(opt) if cipher == nil { - err := errors.New("Ecdsa: PEMCipher not exists.") + err := errors.New("ecdsa: PEMCipher not exists.") return this.AppendError(err) } @@ -110,9 +110,9 @@ func (this Ecdsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str // ==================== // 生成 PKCS8 私钥 pem 数据 -func (this Ecdsa) CreatePKCS8PrivateKey() Ecdsa { +func (this ECDSA) CreatePKCS8PrivateKey() ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -133,9 +133,9 @@ func (this Ecdsa) CreatePKCS8PrivateKey() Ecdsa { // 生成 PKCS8 私钥带密码 pem 数据 // CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256") -func (this Ecdsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) Ecdsa { +func (this ECDSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -170,9 +170,9 @@ func (this Ecdsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any // ==================== // 生成公钥 pem 数据 -func (this Ecdsa) CreatePublicKey() Ecdsa { +func (this ECDSA) CreatePublicKey() ECDSA { if this.publicKey == nil { - err := errors.New("Ecdsa: publicKey error.") + err := errors.New("ecdsa: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/ecdsa/ecdsa.go b/cryptobin/ecdsa/ecdsa.go index 0314a822..8d6a0f68 100644 --- a/cryptobin/ecdsa/ecdsa.go +++ b/cryptobin/ecdsa/ecdsa.go @@ -13,12 +13,12 @@ type ( ) /** - * Ecdsa + * ECDSA * * @create 2022-4-3 * @author deatil */ -type Ecdsa struct { +type ECDSA struct { // 私钥 privateKey *ecdsa.PrivateKey @@ -48,8 +48,8 @@ type Ecdsa struct { } // 构造函数 -func NewEcdsa() Ecdsa { - return Ecdsa{ +func NewECDSA() ECDSA { + return ECDSA{ curve: elliptic.P256(), signHash: sha256.New, verify: false, @@ -58,11 +58,11 @@ func NewEcdsa() Ecdsa { } // 构造函数 -func New() Ecdsa { - return NewEcdsa() +func New() ECDSA { + return NewECDSA() } var ( // 默认 - defaultECDSA = NewEcdsa() + defaultECDSA = NewECDSA() ) diff --git a/cryptobin/ecdsa/ecdsa_test.go b/cryptobin/ecdsa/ecdsa_test.go index 81c6d22d..59bd9544 100644 --- a/cryptobin/ecdsa/ecdsa_test.go +++ b/cryptobin/ecdsa/ecdsa_test.go @@ -80,7 +80,7 @@ func Test_SignASN1_RC2_40En(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC2_40En), "123"). SignASN1() @@ -94,7 +94,7 @@ func Test_SignASN1_RC2_64En(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC2_64En), "123"). SignASN1() @@ -108,7 +108,7 @@ func Test_SignASN1_RC2_128En(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC2_128En), "123"). SignASN1() @@ -122,7 +122,7 @@ func Test_SignASN1_RC5_256En(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC5_256En), "123"). SignASN1() @@ -136,7 +136,7 @@ func Test_SignASN1_RC5_192En(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC5_192En), "123"). SignASN1() @@ -151,7 +151,7 @@ func Test_SignASN1_RC5_128En(t *testing.T) { assertBool := cryptobin_test.AssertBoolT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKeyWithPassword([]byte(prikeyRC5_128En), "123"). SignASN1() @@ -160,7 +160,7 @@ func Test_SignASN1_RC5_128En(t *testing.T) { assertError(objSign.Error(), "SignASN1_RC5_128En-SignASN1") assertNotEmpty(signedData, "SignASN1_RC5_128En-SignASN1") - objVerify := NewEcdsa(). + objVerify := NewECDSA(). FromBase64String(signedData). FromPrivateKeyWithPassword([]byte(prikeyRC5_128En), "123"). MakePublicKey(). @@ -176,7 +176,7 @@ func Test_VerifyASN1En(t *testing.T) { data := "test-pass" sig := "MEUCIBhAZzrS6jM4MfwibzA+j0vBkTEQGvkiDWhx7E6/ePUmAiEAt1uTZXUPGNU9nY8ZS3UxcJCRqwh/G8eeyrAVwM3qen4=" - objVerify := NewEcdsa(). + objVerify := NewECDSA(). FromBase64String(sig). FromPublicKey([]byte(pubkeyEn)). VerifyASN1([]byte(data)) diff --git a/cryptobin/ecdsa/encryption.go b/cryptobin/ecdsa/encryption.go index b528caf2..b476a281 100644 --- a/cryptobin/ecdsa/encryption.go +++ b/cryptobin/ecdsa/encryption.go @@ -8,10 +8,10 @@ import ( ) // 公钥加密 -// Ecdsa 核心为对称加密 -func (this Ecdsa) Encrypt() Ecdsa { +// ECDSA 核心为对称加密 +func (this ECDSA) Encrypt() ECDSA { if this.publicKey == nil { - err := errors.New("Ecdsa: publicKey error.") + err := errors.New("ecdsa: publicKey error.") return this.AppendError(err) } @@ -28,10 +28,10 @@ func (this Ecdsa) Encrypt() Ecdsa { } // 私钥解密 -// Ecdsa 核心为对称加密 -func (this Ecdsa) Decrypt() Ecdsa { +// ECDSA 核心为对称加密 +func (this ECDSA) Decrypt() ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } diff --git a/cryptobin/ecdsa/encryption_test.go b/cryptobin/ecdsa/encryption_test.go index 0799a9c2..1008546d 100644 --- a/cryptobin/ecdsa/encryption_test.go +++ b/cryptobin/ecdsa/encryption_test.go @@ -28,7 +28,7 @@ func Test_Encrypt(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - obj := NewEcdsa(). + obj := NewECDSA(). FromString(data). FromPublicKey([]byte(enpubkey)). Encrypt() @@ -43,7 +43,7 @@ func Test_Decrypt(t *testing.T) { data := "test-pass" endata := "BA6UmWJHLf/XOhge8ASuz11cMpX3YCu6Pfmp5tQ/OPK7rV27paYGB6V5vL/KhjVGznedvhGe0F3CNzoyxfp+r+41m+ehtIC0isWnDc8ZyZrmNVioOeaO5i6yEwiEwhTB8QzUSDE5JJB6ta0vObhBvFRVvgzv1VD0C4Y=" - obj := NewEcdsa(). + obj := NewECDSA(). FromBase64String(endata). FromPrivateKey([]byte(enprikey)). Decrypt() diff --git a/cryptobin/ecdsa/error.go b/cryptobin/ecdsa/error.go index 3e966030..6bac6acb 100644 --- a/cryptobin/ecdsa/error.go +++ b/cryptobin/ecdsa/error.go @@ -5,13 +5,13 @@ import ( ) // 添加错误 -func (this Ecdsa) AppendError(err ...error) Ecdsa { +func (this ECDSA) AppendError(err ...error) ECDSA { this.Errors = append(this.Errors, err...) return this } // 获取错误 -func (this Ecdsa) Error() error { +func (this ECDSA) Error() error { return cryptobin_tool.NewError(this.Errors...) } diff --git a/cryptobin/ecdsa/from.go b/cryptobin/ecdsa/from.go index 5e27c0ee..b592bea8 100644 --- a/cryptobin/ecdsa/from.go +++ b/cryptobin/ecdsa/from.go @@ -12,7 +12,7 @@ import ( ) // 生成密钥 -func (this Ecdsa) GenerateKey() Ecdsa { +func (this ECDSA) GenerateKey() ECDSA { privateKey, err := ecdsa.GenerateKey(this.curve, rand.Reader) if err != nil { return this.AppendError(err) @@ -28,12 +28,12 @@ func (this Ecdsa) GenerateKey() Ecdsa { // 生成密钥 // 可选 [P521 | P384 | P256 | P224] -func GenerateKey(curve string) Ecdsa { +func GenerateKey(curve string) ECDSA { return defaultECDSA.SetCurve(curve).GenerateKey() } // 生成密钥 -func (this Ecdsa) GenerateKeyWithSeed(reader io.Reader) Ecdsa { +func (this ECDSA) GenerateKeyWithSeed(reader io.Reader) ECDSA { privateKey, err := ecdsa.GenerateKey(this.curve, reader) if err != nil { return this.AppendError(err) @@ -49,14 +49,14 @@ func (this Ecdsa) GenerateKeyWithSeed(reader io.Reader) Ecdsa { // 生成密钥 // 可选 [P521 | P384 | P256 | P224] -func GenerateKeyWithSeed(reader io.Reader, curve string) Ecdsa { +func GenerateKeyWithSeed(reader io.Reader, curve string) ECDSA { return defaultECDSA.SetCurve(curve).GenerateKeyWithSeed(reader) } // ========== // 私钥 -func (this Ecdsa) FromPrivateKey(key []byte) Ecdsa { +func (this ECDSA) FromPrivateKey(key []byte) ECDSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) if err == nil { this.privateKey = privateKey @@ -75,12 +75,12 @@ func (this Ecdsa) FromPrivateKey(key []byte) Ecdsa { } // 私钥 -func FromPrivateKey(key []byte) Ecdsa { +func FromPrivateKey(key []byte) ECDSA { return defaultECDSA.FromPrivateKey(key) } // 私钥带密码 -func (this Ecdsa) FromPrivateKeyWithPassword(key []byte, password string) Ecdsa { +func (this ECDSA) FromPrivateKeyWithPassword(key []byte, password string) ECDSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password) if err == nil { this.privateKey = privateKey @@ -99,14 +99,14 @@ func (this Ecdsa) FromPrivateKeyWithPassword(key []byte, password string) Ecdsa } // 私钥 -func FromPrivateKeyWithPassword(key []byte, password string) Ecdsa { +func FromPrivateKeyWithPassword(key []byte, password string) ECDSA { return defaultECDSA.FromPrivateKeyWithPassword(key, password) } // ========== // 私钥 -func (this Ecdsa) FromPKCS1PrivateKey(key []byte) Ecdsa { +func (this ECDSA) FromPKCS1PrivateKey(key []byte) ECDSA { privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -118,12 +118,12 @@ func (this Ecdsa) FromPKCS1PrivateKey(key []byte) Ecdsa { } // 私钥 -func FromPKCS1PrivateKey(key []byte) Ecdsa { +func FromPKCS1PrivateKey(key []byte) ECDSA { return defaultECDSA.FromPKCS1PrivateKey(key) } // 私钥带密码 -func (this Ecdsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) Ecdsa { +func (this ECDSA) FromPKCS1PrivateKeyWithPassword(key []byte, password string) ECDSA { privateKey, err := this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -135,14 +135,14 @@ func (this Ecdsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) E } // 私钥带密码 -func FromPKCS1PrivateKeyWithPassword(key []byte, password string) Ecdsa { +func FromPKCS1PrivateKeyWithPassword(key []byte, password string) ECDSA { return defaultECDSA.FromPKCS1PrivateKeyWithPassword(key, password) } // ========== // PKCS8 私钥 -func (this Ecdsa) FromPKCS8PrivateKey(key []byte) Ecdsa { +func (this ECDSA) FromPKCS8PrivateKey(key []byte) ECDSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -154,12 +154,12 @@ func (this Ecdsa) FromPKCS8PrivateKey(key []byte) Ecdsa { } // PKCS8 私钥 -func FromPKCS8PrivateKey(key []byte) Ecdsa { +func FromPKCS8PrivateKey(key []byte) ECDSA { return defaultECDSA.FromPKCS8PrivateKey(key) } // Pkcs8WithPassword -func (this Ecdsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) Ecdsa { +func (this ECDSA) FromPKCS8PrivateKeyWithPassword(key []byte, password string) ECDSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -171,14 +171,14 @@ func (this Ecdsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) E } // PKCS8 私钥带密码 -func FromPKCS8PrivateKeyWithPassword(key []byte, password string) Ecdsa { +func FromPKCS8PrivateKeyWithPassword(key []byte, password string) ECDSA { return defaultECDSA.FromPKCS8PrivateKeyWithPassword(key, password) } // ========== // 公钥 -func (this Ecdsa) FromPublicKey(key []byte) Ecdsa { +func (this ECDSA) FromPublicKey(key []byte) ECDSA { publicKey, err := this.ParsePublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -190,14 +190,14 @@ func (this Ecdsa) FromPublicKey(key []byte) Ecdsa { } // 公钥 -func FromPublicKey(key []byte) Ecdsa { +func FromPublicKey(key []byte) ECDSA { return defaultECDSA.FromPublicKey(key) } // ========== // DER 私钥 -func (this Ecdsa) FromPKCS1PrivateKeyDer(der []byte) Ecdsa { +func (this ECDSA) FromPKCS1PrivateKeyDer(der []byte) ECDSA { key := cryptobin_tool.EncodeDerToPem(der, "EC PRIVATE KEY") privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key) @@ -211,7 +211,7 @@ func (this Ecdsa) FromPKCS1PrivateKeyDer(der []byte) Ecdsa { } // DER 私钥 -func (this Ecdsa) FromPKCS8PrivateKeyDer(der []byte) Ecdsa { +func (this ECDSA) FromPKCS8PrivateKeyDer(der []byte) ECDSA { key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY") privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) @@ -225,7 +225,7 @@ func (this Ecdsa) FromPKCS8PrivateKeyDer(der []byte) Ecdsa { } // DER 公钥 -func (this Ecdsa) FromPublicKeyDer(der []byte) Ecdsa { +func (this ECDSA) FromPublicKeyDer(der []byte) ECDSA { key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY") publicKey, err := this.ParsePublicKeyFromPEM(key) @@ -243,7 +243,7 @@ func (this Ecdsa) FromPublicKeyDer(der []byte) Ecdsa { // 公钥 x,y 16进制字符对 // 需要设置对应的 curve // [xString: xHexString, yString: yHexString] -func (this Ecdsa) FromPublicKeyXYString(xString string, yString string) Ecdsa { +func (this ECDSA) FromPublicKeyXYString(xString string, yString string) ECDSA { x, _ := new(big.Int).SetString(xString[:], 16) y, _ := new(big.Int).SetString(yString[:], 16) @@ -257,7 +257,7 @@ func (this Ecdsa) FromPublicKeyXYString(xString string, yString string) Ecdsa { } // 公钥字符对,需要设置对应的 curve -func (this Ecdsa) FromPublicKeyXYBytes(xBytes, yBytes []byte) Ecdsa { +func (this ECDSA) FromPublicKeyXYBytes(xBytes, yBytes []byte) ECDSA { x := new(big.Int).SetBytes(xBytes) y := new(big.Int).SetBytes(yBytes) @@ -275,12 +275,12 @@ func (this Ecdsa) FromPublicKeyXYBytes(xBytes, yBytes []byte) Ecdsa { // 公钥明文未压缩 // 需要设置对应的 curve // public-key hex: 047c********. -func (this Ecdsa) FromPublicKeyUncompressString(key string) Ecdsa { +func (this ECDSA) FromPublicKeyUncompressString(key string) ECDSA { k, _ := cryptobin_tool.HexDecode(key) x, y := elliptic.Unmarshal(this.curve, k) if x == nil || y == nil { - err := errors.New("Ecdsa: publicKey uncompress string error") + err := errors.New("ecdsa: publicKey uncompress string error") return this.AppendError(err) } @@ -297,12 +297,12 @@ func (this Ecdsa) FromPublicKeyUncompressString(key string) Ecdsa { // 公钥明文压缩 // 需要设置对应的 curve // public-key hex: 027c******** || 036c******** -func (this Ecdsa) FromPublicKeyCompressString(key string) Ecdsa { +func (this ECDSA) FromPublicKeyCompressString(key string) ECDSA { k, _ := cryptobin_tool.HexDecode(key) x, y := elliptic.UnmarshalCompressed(this.curve, k) if x == nil || y == nil { - err := errors.New("Ecdsa: publicKey compress string error") + err := errors.New("ecdsa: publicKey compress string error") return this.AppendError(err) } @@ -317,7 +317,7 @@ func (this Ecdsa) FromPublicKeyCompressString(key string) Ecdsa { } // 公钥明文,需要设置对应的 curve -func (this Ecdsa) FromPublicKeyString(key string) Ecdsa { +func (this ECDSA) FromPublicKeyString(key string) ECDSA { byteLen := (this.curve.Params().BitSize + 7) / 8 k, _ := cryptobin_tool.HexDecode(key) @@ -331,7 +331,7 @@ func (this Ecdsa) FromPublicKeyString(key string) Ecdsa { // 私钥明文,需要设置对应的 curve // private-key: 07e4********; -func (this Ecdsa) FromPrivateKeyString(keyString string) Ecdsa { +func (this ECDSA) FromPrivateKeyString(keyString string) ECDSA { c := this.curve k, _ := new(big.Int).SetString(keyString[:], 16) @@ -349,7 +349,7 @@ func (this Ecdsa) FromPrivateKeyString(keyString string) Ecdsa { // 公钥明文, hex 或者 base64 解码后 // 需要设置对应的 curve -func (this Ecdsa) FromPublicKeyBytes(pub []byte) Ecdsa { +func (this ECDSA) FromPublicKeyBytes(pub []byte) ECDSA { key := cryptobin_tool.HexEncode(pub) return this.FromPublicKeyString(key) @@ -357,7 +357,7 @@ func (this Ecdsa) FromPublicKeyBytes(pub []byte) Ecdsa { // 明文私钥生成私钥结构体 // 需要设置对应的 curve -func (this Ecdsa) FromPrivateKeyBytes(priByte []byte) Ecdsa { +func (this ECDSA) FromPrivateKeyBytes(priByte []byte) ECDSA { c := this.curve k := new(big.Int).SetBytes(priByte) @@ -374,31 +374,31 @@ func (this Ecdsa) FromPrivateKeyBytes(priByte []byte) Ecdsa { // ========== // 字节 -func (this Ecdsa) FromBytes(data []byte) Ecdsa { +func (this ECDSA) FromBytes(data []byte) ECDSA { this.data = data return this } // 字节 -func FromBytes(data []byte) Ecdsa { +func FromBytes(data []byte) ECDSA { return defaultECDSA.FromBytes(data) } // 字符 -func (this Ecdsa) FromString(data string) Ecdsa { +func (this ECDSA) FromString(data string) ECDSA { this.data = []byte(data) return this } // 字符 -func FromString(data string) Ecdsa { +func FromString(data string) ECDSA { return defaultECDSA.FromString(data) } // Base64 -func (this Ecdsa) FromBase64String(data string) Ecdsa { +func (this ECDSA) FromBase64String(data string) ECDSA { newData, err := cryptobin_tool.NewEncoding().Base64Decode(data) this.data = newData @@ -407,12 +407,12 @@ func (this Ecdsa) FromBase64String(data string) Ecdsa { } // Base64 -func FromBase64String(data string) Ecdsa { +func FromBase64String(data string) ECDSA { return defaultECDSA.FromBase64String(data) } // Hex -func (this Ecdsa) FromHexString(data string) Ecdsa { +func (this ECDSA) FromHexString(data string) ECDSA { newData, err := cryptobin_tool.NewEncoding().HexDecode(data) this.data = newData @@ -421,6 +421,6 @@ func (this Ecdsa) FromHexString(data string) Ecdsa { } // Hex -func FromHexString(data string) Ecdsa { +func FromHexString(data string) ECDSA { return defaultECDSA.FromHexString(data) } diff --git a/cryptobin/ecdsa/get.go b/cryptobin/ecdsa/get.go index d3f99aa3..7f0f4c00 100644 --- a/cryptobin/ecdsa/get.go +++ b/cryptobin/ecdsa/get.go @@ -8,17 +8,17 @@ import ( ) // 获取 PrivateKey -func (this Ecdsa) GetPrivateKey() *ecdsa.PrivateKey { +func (this ECDSA) GetPrivateKey() *ecdsa.PrivateKey { return this.privateKey } // 获取 PrivateKeyCurve -func (this Ecdsa) GetPrivateKeyCurve() elliptic.Curve { +func (this ECDSA) GetPrivateKeyCurve() elliptic.Curve { return this.privateKey.Curve } // 获取 PrivateKeyD -func (this Ecdsa) GetPrivateKeyDHexString() string { +func (this ECDSA) GetPrivateKeyDHexString() string { data := this.privateKey.D dataHex := tool.HexEncode(data.Bytes()) @@ -27,22 +27,22 @@ func (this Ecdsa) GetPrivateKeyDHexString() string { } // 获取私钥明文 -func (this Ecdsa) GetPrivateKeyString() string { +func (this ECDSA) GetPrivateKeyString() string { return this.GetPrivateKeyDHexString() } // 获取 PublicKey -func (this Ecdsa) GetPublicKey() *ecdsa.PublicKey { +func (this ECDSA) GetPublicKey() *ecdsa.PublicKey { return this.publicKey } // 获取 PublicKeyCurve -func (this Ecdsa) GetPublicKeyCurve() elliptic.Curve { +func (this ECDSA) GetPublicKeyCurve() elliptic.Curve { return this.publicKey.Curve } // 获取 PublicKeyX -func (this Ecdsa) GetPublicKeyXHexString() string { +func (this ECDSA) GetPublicKeyXHexString() string { data := this.publicKey.X dataHex := tool.HexEncode(data.Bytes()) @@ -51,7 +51,7 @@ func (this Ecdsa) GetPublicKeyXHexString() string { } // 获取 PublicKeyY -func (this Ecdsa) GetPublicKeyYHexString() string { +func (this ECDSA) GetPublicKeyYHexString() string { data := this.publicKey.Y dataHex := tool.HexEncode(data.Bytes()) @@ -60,52 +60,52 @@ func (this Ecdsa) GetPublicKeyYHexString() string { } // 获取 PublicKeyXYHex -func (this Ecdsa) GetPublicKeyXYHexString() string { +func (this ECDSA) GetPublicKeyXYHexString() string { dataHex := this.GetPublicKeyXHexString() + this.GetPublicKeyYHexString() return dataHex } // 获取未压缩公钥 -func (this Ecdsa) GetPublicKeyUncompressString() string { +func (this ECDSA) GetPublicKeyUncompressString() string { key := elliptic.Marshal(this.publicKey.Curve, this.publicKey.X, this.publicKey.Y) return tool.HexEncode(key) } // 获取压缩公钥 -func (this Ecdsa) GetPublicKeyCompressString() string { +func (this ECDSA) GetPublicKeyCompressString() string { key := elliptic.MarshalCompressed(this.publicKey.Curve, this.publicKey.X, this.publicKey.Y) return tool.HexEncode(key) } // 获取 hash 类型 -func (this Ecdsa) GetSignHash() HashFunc { +func (this ECDSA) GetSignHash() HashFunc { return this.signHash } // 获取 keyData -func (this Ecdsa) GetKeyData() []byte { +func (this ECDSA) GetKeyData() []byte { return this.keyData } // 获取 data -func (this Ecdsa) GetData() []byte { +func (this ECDSA) GetData() []byte { return this.data } // 获取 parsedData -func (this Ecdsa) GetParedData() []byte { +func (this ECDSA) GetParedData() []byte { return this.parsedData } // 获取验证后情况 -func (this Ecdsa) GetVerify() bool { +func (this ECDSA) GetVerify() bool { return this.verify } // 获取错误 -func (this Ecdsa) GetErrors() []error { +func (this ECDSA) GetErrors() []error { return this.Errors } diff --git a/cryptobin/ecdsa/make.go b/cryptobin/ecdsa/make.go index 7ca6978f..dd768051 100644 --- a/cryptobin/ecdsa/make.go +++ b/cryptobin/ecdsa/make.go @@ -6,11 +6,11 @@ import( ) // 生成公钥 -func (this Ecdsa) MakePublicKey() Ecdsa { +func (this ECDSA) MakePublicKey() ECDSA { this.publicKey = nil if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -20,10 +20,10 @@ func (this Ecdsa) MakePublicKey() Ecdsa { } // 生成密钥 der 数据 -func (this Ecdsa) MakeKeyDer() Ecdsa { +func (this ECDSA) MakeKeyDer() ECDSA { var block *pem.Block if block, _ = pem.Decode(this.keyData); block == nil { - err := errors.New("Ecdsa: keyData error.") + err := errors.New("ecdsa: keyData error.") return this.AppendError(err) } diff --git a/cryptobin/ecdsa/on.go b/cryptobin/ecdsa/on.go index c9ddd9a5..bb884c5a 100644 --- a/cryptobin/ecdsa/on.go +++ b/cryptobin/ecdsa/on.go @@ -6,7 +6,7 @@ type ( ) // 引出错误信息 -func (this Ecdsa) OnError(fn EcdsaErrorFunc) Ecdsa { +func (this ECDSA) OnError(fn EcdsaErrorFunc) ECDSA { fn(this.Errors) return this diff --git a/cryptobin/ecdsa/parse.go b/cryptobin/ecdsa/parse.go index 65a33dae..572ef130 100644 --- a/cryptobin/ecdsa/parse.go +++ b/cryptobin/ecdsa/parse.go @@ -17,7 +17,7 @@ var ( ) // 解析私钥 -func (this Ecdsa) ParsePKCS1PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) { +func (this ECDSA) ParsePKCS1PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) { var err error // Parse PEM block @@ -43,7 +43,7 @@ func (this Ecdsa) ParsePKCS1PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, er } // 解析私钥带密码 -func (this Ecdsa) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*ecdsa.PrivateKey, error) { +func (this ECDSA) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*ecdsa.PrivateKey, error) { var err error // Parse PEM block @@ -76,7 +76,7 @@ func (this Ecdsa) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password s // ========== // 解析私钥 -func (this Ecdsa) ParsePKCS8PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) { +func (this ECDSA) ParsePKCS8PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) { var err error // Parse PEM block @@ -102,7 +102,7 @@ func (this Ecdsa) ParsePKCS8PrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, er } // 解析 PKCS8 带密码的私钥 -func (this Ecdsa) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password string) (*ecdsa.PrivateKey, error) { +func (this ECDSA) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password string) (*ecdsa.PrivateKey, error) { var err error // Parse PEM block @@ -134,7 +134,7 @@ func (this Ecdsa) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password s // ========== // 解析公钥 -func (this Ecdsa) ParsePublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) { +func (this ECDSA) ParsePublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) { var err error // Parse PEM block diff --git a/cryptobin/ecdsa/sign.go b/cryptobin/ecdsa/sign.go index d04ef28e..ec30360f 100644 --- a/cryptobin/ecdsa/sign.go +++ b/cryptobin/ecdsa/sign.go @@ -11,9 +11,9 @@ import ( ) // 私钥签名 -func (this Ecdsa) Sign(separator ...string) Ecdsa { +func (this ECDSA) Sign(separator ...string) ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -50,9 +50,9 @@ func (this Ecdsa) Sign(separator ...string) Ecdsa { } // 公钥验证 -func (this Ecdsa) Verify(data []byte, separator ...string) Ecdsa { +func (this ECDSA) Verify(data []byte, separator ...string) ECDSA { if this.publicKey == nil { - err := errors.New("Ecdsa: publicKey error.") + err := errors.New("ecdsa: publicKey error.") return this.AppendError(err) } @@ -68,7 +68,7 @@ func (this Ecdsa) Verify(data []byte, separator ...string) Ecdsa { split := strings.Split(string(this.data), sep) if len(split) != 2 { - err := errors.New("Ecdsa: sign data is error.") + err := errors.New("ecdsa: sign data is error.") return this.AppendError(err) } @@ -95,9 +95,9 @@ func (this Ecdsa) Verify(data []byte, separator ...string) Ecdsa { // =============== // 私钥签名 -func (this Ecdsa) SignASN1() Ecdsa { +func (this ECDSA) SignASN1() ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -118,9 +118,9 @@ func (this Ecdsa) SignASN1() Ecdsa { // 公钥验证, 官方默认 // 使用原始数据[data]对比签名后数据 -func (this Ecdsa) VerifyASN1(data []byte) Ecdsa { +func (this ECDSA) VerifyASN1(data []byte) ECDSA { if this.publicKey == nil { - err := errors.New("Ecdsa: publicKey error.") + err := errors.New("ecdsa: publicKey error.") return this.AppendError(err) } @@ -137,9 +137,9 @@ func (this Ecdsa) VerifyASN1(data []byte) Ecdsa { // =============== // 私钥签名 -func (this Ecdsa) SignBytes() Ecdsa { +func (this ECDSA) SignBytes() ECDSA { if this.privateKey == nil { - err := errors.New("Ecdsa: privateKey error.") + err := errors.New("ecdsa: privateKey error.") return this.AppendError(err) } @@ -169,9 +169,9 @@ func (this Ecdsa) SignBytes() Ecdsa { // 公钥验证 // 使用原始数据[data]对比签名后数据 -func (this Ecdsa) VerifyBytes(data []byte) Ecdsa { +func (this ECDSA) VerifyBytes(data []byte) ECDSA { if this.publicKey == nil { - err := errors.New("Ecdsa: publicKey error.") + err := errors.New("ecdsa: publicKey error.") return this.AppendError(err) } @@ -193,7 +193,7 @@ func (this Ecdsa) VerifyBytes(data []byte) Ecdsa { // =============== // 签名后数据 -func (this Ecdsa) DataHash(fn HashFunc, data []byte) ([]byte, error) { +func (this ECDSA) DataHash(fn HashFunc, data []byte) ([]byte, error) { h := fn() h.Write(data) diff --git a/cryptobin/ecdsa/sign_test.go b/cryptobin/ecdsa/sign_test.go index 3ef34092..7e347315 100644 --- a/cryptobin/ecdsa/sign_test.go +++ b/cryptobin/ecdsa/sign_test.go @@ -28,7 +28,7 @@ func Test_SignASN1(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) data := "test-pass" - objSign := NewEcdsa(). + objSign := NewECDSA(). FromString(data). FromPrivateKey([]byte(prikey)). SignASN1() @@ -43,7 +43,7 @@ func Test_VerifyASN1(t *testing.T) { data := "test-pass" sig := "MEUCIBhAZzrS6jM4MfwibzA+j0vBkTEQGvkiDWhx7E6/ePUmAiEAt1uTZXUPGNU9nY8ZS3UxcJCRqwh/G8eeyrAVwM3qen4=" - objVerify := NewEcdsa(). + objVerify := NewECDSA(). FromBase64String(sig). FromPublicKey([]byte(pubkey)). VerifyASN1([]byte(data)) diff --git a/cryptobin/ecdsa/to.go b/cryptobin/ecdsa/to.go index 14025651..d44f2147 100644 --- a/cryptobin/ecdsa/to.go +++ b/cryptobin/ecdsa/to.go @@ -5,46 +5,46 @@ import ( ) // 私钥/公钥 -func (this Ecdsa) ToKeyBytes() []byte { +func (this ECDSA) ToKeyBytes() []byte { return this.keyData } // 私钥/公钥 -func (this Ecdsa) ToKeyString() string { +func (this ECDSA) ToKeyString() string { return string(this.keyData) } // ========== // 输出字节 -func (this Ecdsa) ToBytes() []byte { +func (this ECDSA) ToBytes() []byte { return this.parsedData } // 输出字符 -func (this Ecdsa) ToString() string { +func (this ECDSA) ToString() string { return string(this.parsedData) } // 输出Base64 -func (this Ecdsa) ToBase64String() string { +func (this ECDSA) ToBase64String() string { return cryptobin_tool.NewEncoding().Base64Encode(this.parsedData) } // 输出Hex -func (this Ecdsa) ToHexString() string { +func (this ECDSA) ToHexString() string { return cryptobin_tool.NewEncoding().HexEncode(this.parsedData) } // ========== // 验证结果 -func (this Ecdsa) ToVerify() bool { +func (this ECDSA) ToVerify() bool { return this.verify } // 验证结果,返回 int 类型 -func (this Ecdsa) ToVerifyInt() int { +func (this ECDSA) ToVerifyInt() int { if this.verify { return 1 } diff --git a/cryptobin/ecdsa/with.go b/cryptobin/ecdsa/with.go index e9e3f11d..a4184a0b 100644 --- a/cryptobin/ecdsa/with.go +++ b/cryptobin/ecdsa/with.go @@ -8,21 +8,21 @@ import ( ) // 设置 PrivateKey -func (this Ecdsa) WithPrivateKey(data *ecdsa.PrivateKey) Ecdsa { +func (this ECDSA) WithPrivateKey(data *ecdsa.PrivateKey) ECDSA { this.privateKey = data return this } // 设置 PublicKey -func (this Ecdsa) WithPublicKey(data *ecdsa.PublicKey) Ecdsa { +func (this ECDSA) WithPublicKey(data *ecdsa.PublicKey) ECDSA { this.publicKey = data return this } // 设置曲线类型 -func (this Ecdsa) WithCurve(curve elliptic.Curve) Ecdsa { +func (this ECDSA) WithCurve(curve elliptic.Curve) ECDSA { this.curve = curve return this @@ -30,7 +30,7 @@ func (this Ecdsa) WithCurve(curve elliptic.Curve) Ecdsa { // 设置曲线类型 // 可选参数 [P521 | P384 | P256 | P224] -func (this Ecdsa) SetCurve(curve string) Ecdsa { +func (this ECDSA) SetCurve(curve string) ECDSA { switch curve { case "P521": this.curve = elliptic.P521() @@ -46,14 +46,14 @@ func (this Ecdsa) SetCurve(curve string) Ecdsa { } // 设置 hash 类型 -func (this Ecdsa) WithSignHash(hash HashFunc) Ecdsa { +func (this ECDSA) WithSignHash(hash HashFunc) ECDSA { this.signHash = hash return this } // 设置 hash 类型 -func (this Ecdsa) SetSignHash(hash string) Ecdsa { +func (this ECDSA) SetSignHash(hash string) ECDSA { h, err := tool.GetHash(hash) if err != nil { return this.AppendError(err) @@ -65,28 +65,28 @@ func (this Ecdsa) SetSignHash(hash string) Ecdsa { } // 设置 data -func (this Ecdsa) WithData(data []byte) Ecdsa { +func (this ECDSA) WithData(data []byte) ECDSA { this.data = data return this } // 设置 parsedData -func (this Ecdsa) WithParedData(data []byte) Ecdsa { +func (this ECDSA) WithParedData(data []byte) ECDSA { this.parsedData = data return this } // 设置验证结果 -func (this Ecdsa) WithVerify(data bool) Ecdsa { +func (this ECDSA) WithVerify(data bool) ECDSA { this.verify = data return this } // 设置错误 -func (this Ecdsa) WithErrors(errs []error) Ecdsa { +func (this ECDSA) WithErrors(errs []error) ECDSA { this.Errors = errs return this diff --git a/cryptobin/rsa/check.go b/cryptobin/rsa/check.go index a66caf3f..683fcd85 100644 --- a/cryptobin/rsa/check.go +++ b/cryptobin/rsa/check.go @@ -1,7 +1,7 @@ package rsa // 检测公钥私钥是否匹配 -func (this Rsa) CheckKeyPair() bool { +func (this RSA) CheckKeyPair() bool { // 私钥导出的公钥 pubKeyFromPriKey := this.MakePublicKey(). CreatePKCS8PublicKey(). diff --git a/cryptobin/rsa/create.go b/cryptobin/rsa/create.go index be1a6a54..f72473ef 100644 --- a/cryptobin/rsa/create.go +++ b/cryptobin/rsa/create.go @@ -31,26 +31,26 @@ var ( // 使用: // obj := New().GenerateKey(2048) // priKey := obj.CreatePrivateKey().ToKeyString() -func (this Rsa) CreatePrivateKey() Rsa { +func (this RSA) CreatePrivateKey() RSA { return this.CreatePKCS1PrivateKey() } // 生成私钥带密码 pem 数据, PKCS1 别名 -func (this Rsa) CreatePrivateKeyWithPassword(password string, opts ...string) Rsa { +func (this RSA) CreatePrivateKeyWithPassword(password string, opts ...string) RSA { return this.CreatePKCS1PrivateKeyWithPassword(password, opts...) } // 生成公钥 pem 数据 -func (this Rsa) CreatePublicKey() Rsa { +func (this RSA) CreatePublicKey() RSA { return this.CreatePKCS1PublicKey() } // ==================== // 生成 PKCS1 私钥 -func (this Rsa) CreatePKCS1PrivateKey() Rsa { +func (this RSA) CreatePKCS1PrivateKey() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -69,9 +69,9 @@ func (this Rsa) CreatePKCS1PrivateKey() Rsa { // 生成 PKCS1 私钥带密码 pem 数据 // CreatePKCS1PrivateKeyWithPassword("123", "AES256CBC") // PEMCipher: DESCBC | DESEDE3CBC | AES128CBC | AES192CBC | AES256CBC -func (this Rsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) Rsa { +func (this RSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -83,7 +83,7 @@ func (this Rsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...strin // 加密方式 cipher := cryptobin_pkcs1.GetPEMCipher(opt) if cipher == nil { - err := errors.New("Rsa: PEMCipher not exists.") + err := errors.New("rsa: PEMCipher not exists.") return this.AppendError(err) } @@ -108,9 +108,9 @@ func (this Rsa) CreatePKCS1PrivateKeyWithPassword(password string, opts ...strin } // 生成 pcks1 公钥 pem 数据 -func (this Rsa) CreatePKCS1PublicKey() Rsa { +func (this RSA) CreatePKCS1PublicKey() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -129,9 +129,9 @@ func (this Rsa) CreatePKCS1PublicKey() Rsa { // ==================== // 生成 PKCS8 私钥 pem 数据 -func (this Rsa) CreatePKCS8PrivateKey() Rsa { +func (this RSA) CreatePKCS8PrivateKey() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -152,9 +152,9 @@ func (this Rsa) CreatePKCS8PrivateKey() Rsa { // 生成 PKCS8 私钥带密码 pem 数据 // CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256") -func (this Rsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) Rsa { +func (this RSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -187,9 +187,9 @@ func (this Rsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) } // 生成公钥 pem 数据 -func (this Rsa) CreatePKCS8PublicKey() Rsa { +func (this RSA) CreatePKCS8PublicKey() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -211,9 +211,9 @@ func (this Rsa) CreatePKCS8PublicKey() Rsa { // ==================== // 生成私钥 xml 数据 -func (this Rsa) CreateXMLPrivateKey() Rsa { +func (this RSA) CreateXMLPrivateKey() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -228,9 +228,9 @@ func (this Rsa) CreateXMLPrivateKey() Rsa { } // 生成公钥 xml 数据 -func (this Rsa) CreateXMLPublicKey() Rsa { +func (this RSA) CreateXMLPublicKey() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/rsa/encryption.go b/cryptobin/rsa/encryption.go index c13beb02..60b11da0 100644 --- a/cryptobin/rsa/encryption.go +++ b/cryptobin/rsa/encryption.go @@ -10,9 +10,9 @@ import ( ) // 公钥加密 -func (this Rsa) Encrypt() Rsa { +func (this RSA) Encrypt() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -27,9 +27,9 @@ func (this Rsa) Encrypt() Rsa { } // 私钥解密 -func (this Rsa) Decrypt() Rsa { +func (this RSA) Decrypt() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -46,9 +46,9 @@ func (this Rsa) Decrypt() Rsa { // ==================== // 私钥加密 -func (this Rsa) PrivateKeyEncrypt() Rsa { +func (this RSA) PrivateKeyEncrypt() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -63,9 +63,9 @@ func (this Rsa) PrivateKeyEncrypt() Rsa { } // 公钥解密 -func (this Rsa) PublicKeyDecrypt() Rsa { +func (this RSA) PublicKeyDecrypt() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -82,9 +82,9 @@ func (this Rsa) PublicKeyDecrypt() Rsa { // ==================== // OAEP公钥加密 -func (this Rsa) EncryptOAEP(typ ...string) Rsa { +func (this RSA) EncryptOAEP(typ ...string) RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -109,9 +109,9 @@ func (this Rsa) EncryptOAEP(typ ...string) Rsa { } // OAEP私钥解密 -func (this Rsa) DecryptOAEP(typ ...string) Rsa { +func (this RSA) DecryptOAEP(typ ...string) RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -138,9 +138,9 @@ func (this Rsa) DecryptOAEP(typ ...string) Rsa { // ==================== // 公钥加密, ECB 模式 -func (this Rsa) EncryptECB() Rsa { +func (this RSA) EncryptECB() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -177,9 +177,9 @@ func (this Rsa) EncryptECB() Rsa { } // 私钥解密, ECB 模式 -func (this Rsa) DecryptECB() Rsa { +func (this RSA) DecryptECB() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -218,9 +218,9 @@ func (this Rsa) DecryptECB() Rsa { // ==================== // 私钥加密, ECB 模式 -func (this Rsa) PrivateKeyEncryptECB() Rsa { +func (this RSA) PrivateKeyEncryptECB() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -256,9 +256,9 @@ func (this Rsa) PrivateKeyEncryptECB() Rsa { return this} // 公钥解密, ECB 模式 -func (this Rsa) PublicKeyDecryptECB() Rsa { +func (this RSA) PublicKeyDecryptECB() RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -297,9 +297,9 @@ func (this Rsa) PublicKeyDecryptECB() Rsa { // ==================== // OAEP公钥加密, ECB 模式 -func (this Rsa) EncryptOAEPECB(typ ...string) Rsa { +func (this RSA) EncryptOAEPECB(typ ...string) RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } @@ -346,9 +346,9 @@ func (this Rsa) EncryptOAEPECB(typ ...string) Rsa { } // OAEP私钥解密, ECB 模式 -func (this Rsa) DecryptOAEPECB(typ ...string) Rsa { +func (this RSA) DecryptOAEPECB(typ ...string) RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } diff --git a/cryptobin/rsa/error.go b/cryptobin/rsa/error.go index 266e07a3..693295b8 100644 --- a/cryptobin/rsa/error.go +++ b/cryptobin/rsa/error.go @@ -5,13 +5,13 @@ import ( ) // 添加错误 -func (this Rsa) AppendError(err ...error) Rsa { +func (this RSA) AppendError(err ...error) RSA { this.Errors = append(this.Errors, err...) return this } // 获取错误 -func (this Rsa) Error() error { +func (this RSA) Error() error { return cryptobin_tool.NewError(this.Errors...) } diff --git a/cryptobin/rsa/from.go b/cryptobin/rsa/from.go index 97e1ca47..7fe0d26f 100644 --- a/cryptobin/rsa/from.go +++ b/cryptobin/rsa/from.go @@ -12,7 +12,7 @@ import ( // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func (this Rsa) GenerateKey(bits int) Rsa { +func (this RSA) GenerateKey(bits int) RSA { privateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return this.AppendError(err) @@ -28,12 +28,12 @@ func (this Rsa) GenerateKey(bits int) Rsa { // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func GenerateKey(bits int) Rsa { +func GenerateKey(bits int) RSA { return defaultRSA.GenerateKey(bits) } // 生成密钥 -func (this Rsa) GenerateMultiPrimeKey(nprimes int, bits int) Rsa { +func (this RSA) GenerateMultiPrimeKey(nprimes int, bits int) RSA { privateKey, err := rsa.GenerateMultiPrimeKey(rand.Reader, nprimes, bits) if err != nil { return this.AppendError(err) @@ -48,7 +48,7 @@ func (this Rsa) GenerateMultiPrimeKey(nprimes int, bits int) Rsa { } // 生成密钥 -func GenerateMultiPrimeKey(nprimes int, bits int) Rsa { +func GenerateMultiPrimeKey(nprimes int, bits int) RSA { return defaultRSA.GenerateMultiPrimeKey(nprimes, bits) } @@ -56,7 +56,7 @@ func GenerateMultiPrimeKey(nprimes int, bits int) Rsa { // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func (this Rsa) GenerateKeyWithSeed(reader io.Reader, bits int) Rsa { +func (this RSA) GenerateKeyWithSeed(reader io.Reader, bits int) RSA { privateKey, err := rsa.GenerateKey(reader, bits) if err != nil { return this.AppendError(err) @@ -72,12 +72,12 @@ func (this Rsa) GenerateKeyWithSeed(reader io.Reader, bits int) Rsa { // 生成密钥 // bits = 512 | 1024 | 2048 | 4096 -func GenerateKeyWithSeed(reader io.Reader, bits int) Rsa { +func GenerateKeyWithSeed(reader io.Reader, bits int) RSA { return defaultRSA.GenerateKeyWithSeed(reader, bits) } // 生成密钥 -func (this Rsa) GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) Rsa { +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) @@ -92,14 +92,14 @@ func (this Rsa) GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bit } // 生成密钥 -func GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) Rsa { +func GenerateMultiPrimeKeyWithSeed(reader io.Reader, nprimes int, bits int) RSA { return defaultRSA.GenerateMultiPrimeKeyWithSeed(reader, nprimes, bits) } // ========== // 私钥 -func (this Rsa) FromPrivateKey(key []byte) Rsa { +func (this RSA) FromPrivateKey(key []byte) RSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) if err == nil { this.privateKey = privateKey @@ -118,12 +118,12 @@ func (this Rsa) FromPrivateKey(key []byte) Rsa { } // 私钥 -func FromPrivateKey(key []byte) Rsa { +func FromPrivateKey(key []byte) RSA { return defaultRSA.FromPrivateKey(key) } // 私钥带密码 -func (this Rsa) FromPrivateKeyWithPassword(key []byte, password string) Rsa { +func (this RSA) FromPrivateKeyWithPassword(key []byte, password string) RSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password) if err == nil { this.privateKey = privateKey @@ -142,12 +142,12 @@ func (this Rsa) FromPrivateKeyWithPassword(key []byte, password string) Rsa { } // 私钥带密码 -func FromPrivateKeyWithPassword(key []byte, password string) Rsa { +func FromPrivateKeyWithPassword(key []byte, password string) RSA { return defaultRSA.FromPrivateKeyWithPassword(key, password) } // 公钥 -func (this Rsa) FromPublicKey(key []byte) Rsa { +func (this RSA) FromPublicKey(key []byte) RSA { var publicKey *rsa.PublicKey var err error @@ -165,7 +165,7 @@ func (this Rsa) FromPublicKey(key []byte) Rsa { } // 公钥 -func FromPublicKey(key []byte) Rsa { +func FromPublicKey(key []byte) RSA { return defaultRSA.FromPublicKey(key) } @@ -173,16 +173,16 @@ func FromPublicKey(key []byte) Rsa { // 模数、指数生成公钥 // 指数默认为 0x10001(65537) -func (this Rsa) FromPublicKeyNE(nString string, eString string) Rsa { +func (this RSA) FromPublicKeyNE(nString string, eString string) RSA { n, ok := new(big.Int).SetString(nString[:], 16) if !ok { - err := errors.New("RSA: n is error") + err := errors.New("rsa: n is error") return this.AppendError(err) } e, ok := new(big.Int).SetString(eString[:], 16) if !ok { - err := errors.New("RSA: e is error") + err := errors.New("rsa: e is error") return this.AppendError(err) } @@ -195,14 +195,14 @@ func (this Rsa) FromPublicKeyNE(nString string, eString string) Rsa { } // 公钥 -func FromPublicKeyNE(nString string, eString string) Rsa { +func FromPublicKeyNE(nString string, eString string) RSA { return defaultRSA.FromPublicKeyNE(nString, eString) } // ========== // Pkcs1 -func (this Rsa) FromPKCS1PrivateKey(key []byte) Rsa { +func (this RSA) FromPKCS1PrivateKey(key []byte) RSA { privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -214,12 +214,12 @@ func (this Rsa) FromPKCS1PrivateKey(key []byte) Rsa { } // PKCS1 私钥 -func FromPKCS1PrivateKey(key []byte) Rsa { +func FromPKCS1PrivateKey(key []byte) RSA { return defaultRSA.FromPKCS1PrivateKey(key) } // Pkcs1WithPassword -func (this Rsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) Rsa { +func (this RSA) FromPKCS1PrivateKeyWithPassword(key []byte, password string) RSA { privateKey, err := this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -231,12 +231,12 @@ func (this Rsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) Rsa } // PKCS1 私钥带密码 -func FromPKCS1PrivateKeyWithPassword(key []byte, password string) Rsa { +func FromPKCS1PrivateKeyWithPassword(key []byte, password string) RSA { return defaultRSA.FromPKCS1PrivateKeyWithPassword(key, password) } // PKCS1 公钥 -func (this Rsa) FromPKCS1PublicKey(key []byte) Rsa { +func (this RSA) FromPKCS1PublicKey(key []byte) RSA { publicKey, err := this.ParsePKCS1PublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -248,14 +248,14 @@ func (this Rsa) FromPKCS1PublicKey(key []byte) Rsa { } // PKCS1 公钥 -func FromPKCS1PublicKey(key []byte) Rsa { +func FromPKCS1PublicKey(key []byte) RSA { return defaultRSA.FromPKCS1PublicKey(key) } // ========== // Pkcs8 -func (this Rsa) FromPKCS8PrivateKey(key []byte) Rsa { +func (this RSA) FromPKCS8PrivateKey(key []byte) RSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -267,12 +267,12 @@ func (this Rsa) FromPKCS8PrivateKey(key []byte) Rsa { } // PKCS8 私钥 -func FromPKCS8PrivateKey(key []byte) Rsa { +func FromPKCS8PrivateKey(key []byte) RSA { return defaultRSA.FromPKCS8PrivateKey(key) } // Pkcs8WithPassword -func (this Rsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) Rsa { +func (this RSA) FromPKCS8PrivateKeyWithPassword(key []byte, password string) RSA { privateKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -284,12 +284,12 @@ func (this Rsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) Rsa } // PKCS8 私钥带密码 -func FromPKCS8PrivateKeyWithPassword(key []byte, password string) Rsa { +func FromPKCS8PrivateKeyWithPassword(key []byte, password string) RSA { return defaultRSA.FromPKCS8PrivateKeyWithPassword(key, password) } // PKCS8 公钥 -func (this Rsa) FromPKCS8PublicKey(key []byte) Rsa { +func (this RSA) FromPKCS8PublicKey(key []byte) RSA { publicKey, err := this.ParsePKCS8PublicKeyFromPEM(key) if err != nil { return this.AppendError(err) @@ -301,14 +301,14 @@ func (this Rsa) FromPKCS8PublicKey(key []byte) Rsa { } // PKCS8 公钥 -func FromPKCS8PublicKey(key []byte) Rsa { +func FromPKCS8PublicKey(key []byte) RSA { return defaultRSA.FromPKCS8PublicKey(key) } // ========== // Pkcs1 DER -func (this Rsa) FromPKCS1PrivateKeyDer(der []byte) Rsa { +func (this RSA) FromPKCS1PrivateKeyDer(der []byte) RSA { key := cryptobin_tool.EncodeDerToPem(der, "RSA PRIVATE KEY") privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key) @@ -322,7 +322,7 @@ func (this Rsa) FromPKCS1PrivateKeyDer(der []byte) Rsa { } // PKCS1 DER 公钥 -func (this Rsa) FromPKCS1PublicKeyDer(der []byte) Rsa { +func (this RSA) FromPKCS1PublicKeyDer(der []byte) RSA { key := cryptobin_tool.EncodeDerToPem(der, "RSA PUBLIC KEY") publicKey, err := this.ParsePKCS1PublicKeyFromPEM(key) @@ -338,7 +338,7 @@ func (this Rsa) FromPKCS1PublicKeyDer(der []byte) Rsa { // ========== // Pkcs8 DER -func (this Rsa) FromPKCS8PrivateKeyDer(der []byte) Rsa { +func (this RSA) FromPKCS8PrivateKeyDer(der []byte) RSA { key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY") privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key) @@ -352,7 +352,7 @@ func (this Rsa) FromPKCS8PrivateKeyDer(der []byte) Rsa { } // PKCS8 DER 公钥 -func (this Rsa) FromPKCS8PublicKeyDer(der []byte) Rsa { +func (this RSA) FromPKCS8PublicKeyDer(der []byte) RSA { key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY") publicKey, err := this.ParsePKCS8PublicKeyFromPEM(key) @@ -368,7 +368,7 @@ func (this Rsa) FromPKCS8PublicKeyDer(der []byte) Rsa { // ========== // XML 私钥 -func (this Rsa) FromXMLPrivateKey(key []byte) Rsa { +func (this RSA) FromXMLPrivateKey(key []byte) RSA { privateKey, err := this.ParsePrivateKeyFromXML(key) if err != nil { return this.AppendError(err) @@ -380,12 +380,12 @@ func (this Rsa) FromXMLPrivateKey(key []byte) Rsa { } // XML 私钥 -func FromXMLPrivateKey(key []byte) Rsa { +func FromXMLPrivateKey(key []byte) RSA { return defaultRSA.FromXMLPrivateKey(key) } // XML 公钥 -func (this Rsa) FromXMLPublicKey(key []byte) Rsa { +func (this RSA) FromXMLPublicKey(key []byte) RSA { publicKey, err := this.ParsePublicKeyFromXML(key) if err != nil { return this.AppendError(err) @@ -397,14 +397,14 @@ func (this Rsa) FromXMLPublicKey(key []byte) Rsa { } // XML 公钥 -func FromXMLPublicKey(key []byte) Rsa { +func FromXMLPublicKey(key []byte) RSA { return defaultRSA.FromXMLPublicKey(key) } // ========== // Pkcs12 Cert -func (this Rsa) FromPKCS12Cert(key []byte) Rsa { +func (this RSA) FromPKCS12Cert(key []byte) RSA { privateKey, err := this.ParsePKCS12CertFromPEMWithPassword(key, "") if err != nil { return this.AppendError(err) @@ -416,12 +416,12 @@ func (this Rsa) FromPKCS12Cert(key []byte) Rsa { } // Pkcs12Cert -func FromPKCS12Cert(key []byte) Rsa { +func FromPKCS12Cert(key []byte) RSA { return defaultRSA.FromPKCS12Cert(key) } // Pkcs12CertWithPassword -func (this Rsa) FromPKCS12CertWithPassword(key []byte, password string) Rsa { +func (this RSA) FromPKCS12CertWithPassword(key []byte, password string) RSA { privateKey, err := this.ParsePKCS12CertFromPEMWithPassword(key, password) if err != nil { return this.AppendError(err) @@ -433,38 +433,38 @@ func (this Rsa) FromPKCS12CertWithPassword(key []byte, password string) Rsa { } // Pkcs12Cert 带密码 -func FromPKCS12CertWithPassword(key []byte, password string) Rsa { +func FromPKCS12CertWithPassword(key []byte, password string) RSA { return defaultRSA.FromPKCS12CertWithPassword(key, password) } // ========== // 字节 -func (this Rsa) FromBytes(data []byte) Rsa { +func (this RSA) FromBytes(data []byte) RSA { this.data = data return this } // 字节 -func FromBytes(data []byte) Rsa { +func FromBytes(data []byte) RSA { return defaultRSA.FromBytes(data) } // 字符 -func (this Rsa) FromString(data string) Rsa { +func (this RSA) FromString(data string) RSA { this.data = []byte(data) return this } // 字符 -func FromString(data string) Rsa { +func FromString(data string) RSA { return defaultRSA.FromString(data) } // Base64 -func (this Rsa) FromBase64String(data string) Rsa { +func (this RSA) FromBase64String(data string) RSA { newData, err := cryptobin_tool.NewEncoding().Base64Decode(data) if err != nil { return this.AppendError(err) @@ -476,12 +476,12 @@ func (this Rsa) FromBase64String(data string) Rsa { } // Base64 -func FromBase64String(data string) Rsa { +func FromBase64String(data string) RSA { return defaultRSA.FromBase64String(data) } // Hex -func (this Rsa) FromHexString(data string) Rsa { +func (this RSA) FromHexString(data string) RSA { newData, err := cryptobin_tool.NewEncoding().HexDecode(data) if err != nil { return this.AppendError(err) @@ -493,6 +493,6 @@ func (this Rsa) FromHexString(data string) Rsa { } // Hex -func FromHexString(data string) Rsa { +func FromHexString(data string) RSA { return defaultRSA.FromHexString(data) } diff --git a/cryptobin/rsa/get.go b/cryptobin/rsa/get.go index 76ee7239..b069f9c9 100644 --- a/cryptobin/rsa/get.go +++ b/cryptobin/rsa/get.go @@ -9,55 +9,55 @@ import ( ) // 获取 PrivateKey -func (this Rsa) GetPrivateKey() *rsa.PrivateKey { +func (this RSA) GetPrivateKey() *rsa.PrivateKey { return this.privateKey } // 获取 PublicKey -func (this Rsa) GetPublicKey() *rsa.PublicKey { +func (this RSA) GetPublicKey() *rsa.PublicKey { return this.publicKey } // 获取 PublicKeyN -func (this Rsa) GetPublicKeyNHexString() string { +func (this RSA) GetPublicKeyNHexString() string { data := this.publicKey.N return cryptobin_tool.HexEncode(data.Bytes()) } // 获取 PublicKeyE -func (this Rsa) GetPublicKeyEHexString() string { +func (this RSA) GetPublicKeyEHexString() string { e := big.NewInt(int64(this.publicKey.E)) return cryptobin_tool.HexEncode(e.Bytes()) } // 获取 hash 类型 -func (this Rsa) GetSignHash() crypto.Hash { +func (this RSA) GetSignHash() crypto.Hash { return this.signHash } // 获取 keyData -func (this Rsa) GetKeyData() []byte { +func (this RSA) GetKeyData() []byte { return this.keyData } // 获取 data -func (this Rsa) GetData() []byte { +func (this RSA) GetData() []byte { return this.data } // 获取 parsedData -func (this Rsa) GetParedData() []byte { +func (this RSA) GetParedData() []byte { return this.parsedData } // 获取验证后情况 -func (this Rsa) GetVerify() bool { +func (this RSA) GetVerify() bool { return this.verify } // 获取错误 -func (this Rsa) GetErrors() []error { +func (this RSA) GetErrors() []error { return this.Errors } diff --git a/cryptobin/rsa/make.go b/cryptobin/rsa/make.go index ac006b07..a0c6e9f7 100644 --- a/cryptobin/rsa/make.go +++ b/cryptobin/rsa/make.go @@ -6,11 +6,11 @@ import( ) // 生成公钥 -func (this Rsa) MakePublicKey() Rsa { +func (this RSA) MakePublicKey() RSA { this.publicKey = nil if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -20,10 +20,10 @@ func (this Rsa) MakePublicKey() Rsa { } // 生成密钥 der 数据 -func (this Rsa) MakeKeyDer() Rsa { +func (this RSA) MakeKeyDer() RSA { var block *pem.Block if block, _ = pem.Decode(this.keyData); block == nil { - err := errors.New("Rsa: keyData error.") + err := errors.New("rsa: keyData error.") return this.AppendError(err) } diff --git a/cryptobin/rsa/on.go b/cryptobin/rsa/on.go index d080895c..ce0908f6 100644 --- a/cryptobin/rsa/on.go +++ b/cryptobin/rsa/on.go @@ -6,7 +6,7 @@ type ( ) // 引出错误信息 -func (this Rsa) OnError(fn RsaErrorFunc) Rsa { +func (this RSA) OnError(fn RsaErrorFunc) RSA { fn(this.Errors) return this diff --git a/cryptobin/rsa/parse.go b/cryptobin/rsa/parse.go index 2bf79470..93509616 100644 --- a/cryptobin/rsa/parse.go +++ b/cryptobin/rsa/parse.go @@ -19,7 +19,7 @@ var ( ) // 解析 PKCS1 私钥 -func (this Rsa) ParsePKCS1PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) { +func (this RSA) ParsePKCS1PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) { var err error // Parse PEM block @@ -44,7 +44,7 @@ func (this Rsa) ParsePKCS1PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) } // 解析 PKCS1 带密码的私钥 -func (this Rsa) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) { +func (this RSA) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) { var err error // Parse PEM block @@ -74,7 +74,7 @@ func (this Rsa) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password str } // 解析 PKCS1 公钥 -func (this Rsa) ParsePKCS1PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { +func (this RSA) ParsePKCS1PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { var err error // Parse PEM block @@ -101,7 +101,7 @@ func (this Rsa) ParsePKCS1PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { // ==================== // 解析 PKCS8 私钥 -func (this Rsa) ParsePKCS8PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) { +func (this RSA) ParsePKCS8PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) { var err error // Parse PEM block @@ -126,7 +126,7 @@ func (this Rsa) ParsePKCS8PrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) } // 解析 PKCS8 带密码的私钥 -func (this Rsa) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) { +func (this RSA) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) { var err error // Parse PEM block @@ -157,7 +157,7 @@ func (this Rsa) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password str } // 解析 PKCS8 公钥 -func (this Rsa) ParsePKCS8PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { +func (this RSA) ParsePKCS8PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { var err error // Parse PEM block @@ -189,7 +189,7 @@ func (this Rsa) ParsePKCS8PublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { // ============ // 解析 pkf 证书 -func (this Rsa) ParsePKCS12CertFromPEMWithPassword(pfxData []byte, password string) (*rsa.PrivateKey, error) { +func (this RSA) ParsePKCS12CertFromPEMWithPassword(pfxData []byte, password string) (*rsa.PrivateKey, error) { privateKey, _, err := pkcs12.Decode(pfxData, password) if err != nil { return nil, err @@ -206,11 +206,11 @@ func (this Rsa) ParsePKCS12CertFromPEMWithPassword(pfxData []byte, password stri // ============ // 解析 xml 私钥 -func (this Rsa) ParsePrivateKeyFromXML(key []byte) (*rsa.PrivateKey, error) { +func (this RSA) ParsePrivateKeyFromXML(key []byte) (*rsa.PrivateKey, error) { return cryptobin_rsa.ParseXMLPrivateKey(key) } // 解析 xml 公钥 -func (this Rsa) ParsePublicKeyFromXML(key []byte) (*rsa.PublicKey, error) { +func (this RSA) ParsePublicKeyFromXML(key []byte) (*rsa.PublicKey, error) { return cryptobin_rsa.ParseXMLPublicKey(key) } diff --git a/cryptobin/rsa/rsa.go b/cryptobin/rsa/rsa.go index c7cda23a..0da8901b 100644 --- a/cryptobin/rsa/rsa.go +++ b/cryptobin/rsa/rsa.go @@ -6,12 +6,12 @@ import ( ) /** - * Rsa 加密 + * RSA 加密 * * @create 2021-8-28 * @author deatil */ -type Rsa struct { +type RSA struct { // 私钥 privateKey *rsa.PrivateKey @@ -38,8 +38,8 @@ type Rsa struct { } // 构造函数 -func NewRsa() Rsa { - return Rsa{ +func NewRSA() RSA { + return RSA{ signHash: crypto.SHA256, verify: false, Errors: make([]error, 0), @@ -47,11 +47,11 @@ func NewRsa() Rsa { } // 构造函数 -func New() Rsa { - return NewRsa() +func New() RSA { + return NewRSA() } var ( // 默认 - defaultRSA = NewRsa() + defaultRSA = NewRSA() ) diff --git a/cryptobin/rsa/rsa_test.go b/cryptobin/rsa/rsa_test.go index 041235bb..c1ff6101 100644 --- a/cryptobin/rsa/rsa_test.go +++ b/cryptobin/rsa/rsa_test.go @@ -17,7 +17,7 @@ func Test_PrimeKeyGeneration(t *testing.T) { size = 256 } - obj := NewRsa().GenerateMultiPrimeKey(3, size) + obj := NewRSA().GenerateMultiPrimeKey(3, size) objPriKey := obj.CreatePKCS1PrivateKey() @@ -90,7 +90,7 @@ func Test_RSAPkcs1Sign(t *testing.T) { data := "test-pass" - obj := NewRsa() + obj := NewRSA() sign := obj. FromString(data). @@ -120,7 +120,7 @@ func Test_RSAPkcs8Sign(t *testing.T) { data := "test-pass22222" - obj := NewRsa() + obj := NewRSA() sign := obj. FromString(data). @@ -160,7 +160,7 @@ func Test_PubNE(t *testing.T) { assertError := cryptobin_test.AssertErrorT(t) assertEqual := cryptobin_test.AssertEqualT(t) - en := NewRsa(). + en := NewRSA(). FromPublicKeyNE(testPubN, testPubE). CreatePKCS8PublicKey() enData := en.ToKeyString() diff --git a/cryptobin/rsa/sign.go b/cryptobin/rsa/sign.go index d9e3c9b5..c9c0110b 100644 --- a/cryptobin/rsa/sign.go +++ b/cryptobin/rsa/sign.go @@ -7,9 +7,9 @@ import ( ) // 私钥签名 -func (this Rsa) Sign() Rsa { +func (this RSA) Sign() RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -26,9 +26,9 @@ func (this Rsa) Sign() Rsa { // 公钥验证 // 使用原始数据[data]对比签名后数据 -func (this Rsa) Verify(data []byte) Rsa { +func (this RSA) Verify(data []byte) RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/rsa/sign_pss.go b/cryptobin/rsa/sign_pss.go index 6feac899..da5aa7d1 100644 --- a/cryptobin/rsa/sign_pss.go +++ b/cryptobin/rsa/sign_pss.go @@ -8,9 +8,9 @@ import ( // 私钥签名 // 常用为: PS256[SHA256] | PS384[SHA384] | PS512[SHA512] -func (this Rsa) SignPSS(opts ...rsa.PSSOptions) Rsa { +func (this RSA) SignPSS(opts ...rsa.PSSOptions) RSA { if this.privateKey == nil { - err := errors.New("Rsa: privateKey error.") + err := errors.New("rsa: privateKey error.") return this.AppendError(err) } @@ -35,9 +35,9 @@ func (this Rsa) SignPSS(opts ...rsa.PSSOptions) Rsa { // 公钥验证 // 使用原始数据[data]对比签名后数据 -func (this Rsa) VerifyPSS(data []byte, opts ...rsa.PSSOptions) Rsa { +func (this RSA) VerifyPSS(data []byte, opts ...rsa.PSSOptions) RSA { if this.publicKey == nil { - err := errors.New("Rsa: publicKey error.") + err := errors.New("rsa: publicKey error.") return this.AppendError(err) } diff --git a/cryptobin/rsa/to.go b/cryptobin/rsa/to.go index 1677f1ad..d308e658 100644 --- a/cryptobin/rsa/to.go +++ b/cryptobin/rsa/to.go @@ -5,46 +5,46 @@ import ( ) // 私钥/公钥 -func (this Rsa) ToKeyBytes() []byte { +func (this RSA) ToKeyBytes() []byte { return this.keyData } // 私钥/公钥 -func (this Rsa) ToKeyString() string { +func (this RSA) ToKeyString() string { return string(this.keyData) } // ========== // 输出字节 -func (this Rsa) ToBytes() []byte { +func (this RSA) ToBytes() []byte { return this.parsedData } // 输出字符 -func (this Rsa) ToString() string { +func (this RSA) ToString() string { return string(this.parsedData) } // 输出Base64 -func (this Rsa) ToBase64String() string { +func (this RSA) ToBase64String() string { return cryptobin_tool.NewEncoding().Base64Encode(this.parsedData) } // 输出Hex -func (this Rsa) ToHexString() string { +func (this RSA) ToHexString() string { return cryptobin_tool.NewEncoding().HexEncode(this.parsedData) } // ========== // 验证结果 -func (this Rsa) ToVerify() bool { +func (this RSA) ToVerify() bool { return this.verify } // 验证结果,返回 int 类型 -func (this Rsa) ToVerifyInt() int { +func (this RSA) ToVerifyInt() int { if this.verify { return 1 } diff --git a/cryptobin/rsa/with.go b/cryptobin/rsa/with.go index c6bad321..ca665c5c 100644 --- a/cryptobin/rsa/with.go +++ b/cryptobin/rsa/with.go @@ -8,28 +8,28 @@ import ( ) // 设置 PrivateKey -func (this Rsa) WithPrivateKey(data *rsa.PrivateKey) Rsa { +func (this RSA) WithPrivateKey(data *rsa.PrivateKey) RSA { this.privateKey = data return this } // 设置 PublicKey -func (this Rsa) WithPublicKey(data *rsa.PublicKey) Rsa { +func (this RSA) WithPublicKey(data *rsa.PublicKey) RSA { this.publicKey = data return this } // 设置 hash 类型 -func (this Rsa) WithSignHash(h crypto.Hash) Rsa { +func (this RSA) WithSignHash(h crypto.Hash) RSA { this.signHash = h return this } // 设置 hash 类型 -func (this Rsa) SetSignHash(name string) Rsa { +func (this RSA) SetSignHash(name string) RSA { hash, err := tool.GetCryptoHash(name) if err != nil { return this.AppendError(err) @@ -41,28 +41,28 @@ func (this Rsa) SetSignHash(name string) Rsa { } // 设置 data -func (this Rsa) WithData(data []byte) Rsa { +func (this RSA) WithData(data []byte) RSA { this.data = data return this } // 设置 parsedData -func (this Rsa) WithParedData(data []byte) Rsa { +func (this RSA) WithParedData(data []byte) RSA { this.parsedData = data return this } // 设置 verify -func (this Rsa) WithVerify(data bool) Rsa { +func (this RSA) WithVerify(data bool) RSA { this.verify = data return this } // 设置错误 -func (this Rsa) WithError(errs []error) Rsa { +func (this RSA) WithError(errs []error) RSA { this.Errors = errs return this