Skip to content

Commit

Permalink
优化更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 5, 2024
1 parent 5f2a83b commit 35ad6cb
Show file tree
Hide file tree
Showing 65 changed files with 502 additions and 499 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
2 changes: 1 addition & 1 deletion cryptobin/dh/dh/check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dh

// 检测公钥私钥是否匹配
func (this Dh) CheckKeyPair() bool {
func (this DH) CheckKeyPair() bool {
// 私钥导出的公钥
pubKeyFromPriKey := this.MakePublicKey().
CreatePublicKey().
Expand Down
18 changes: 9 additions & 9 deletions cryptobin/dh/dh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions cryptobin/dh/dh/dh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type (
* @create 2022-8-7
* @author deatil
*/
type Dh struct {
type DH struct {
// 私钥
privateKey *cryptobin_dh.PrivateKey

Expand All @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions cryptobin/dh/dh/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
36 changes: 18 additions & 18 deletions cryptobin/dh/dh/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions cryptobin/dh/dh/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand All @@ -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 ""
}
Expand All @@ -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 ""
}
Expand All @@ -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 ""
}
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions cryptobin/dh/dh/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cryptobin/dh/dh/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type (
)

// 引出错误信息
func (this Dh) OnError(fn ErrorFunc) Dh {
func (this DH) OnError(fn ErrorFunc) DH {
fn(this.Errors)

return this
Expand Down
6 changes: 3 additions & 3 deletions cryptobin/dh/dh/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 35ad6cb

Please sign in to comment.