Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 22, 2024
1 parent 49da360 commit 157fe8e
Show file tree
Hide file tree
Showing 46 changed files with 404 additions and 365 deletions.
26 changes: 13 additions & 13 deletions cryptobin/dh/curve25519/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"crypto/rand"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/curve25519"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

type (
// 配置
Opts = cryptobin_pkcs8.Opts
Opts = pkcs8.Opts
// PBKDF2 配置
PBKDF2Opts = cryptobin_pkcs8.PBKDF2Opts
PBKDF2Opts = pkcs8.PBKDF2Opts
// Scrypt 配置
ScryptOpts = cryptobin_pkcs8.ScryptOpts
ScryptOpts = pkcs8.ScryptOpts
)

var (
// 获取 Cipher 类型
GetCipherFromName = cryptobin_pkcs8.GetCipherFromName
GetCipherFromName = pkcs8.GetCipherFromName
// 获取 hash 类型
GetHashFromName = cryptobin_pkcs8.GetHashFromName
GetHashFromName = pkcs8.GetHashFromName
)

// 生成私钥 pem 数据
Expand All @@ -31,7 +31,7 @@ var (
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this Curve25519) CreatePrivateKey() Curve25519 {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand All @@ -54,11 +54,11 @@ func (this Curve25519) CreatePrivateKey() Curve25519 {
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any) Curve25519 {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

opt, err := cryptobin_pkcs8.ParseOpts(opts...)
opt, err := pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -70,7 +70,7 @@ func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any
}

// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPEMBlock(
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
Expand All @@ -89,7 +89,7 @@ func (this Curve25519) CreatePrivateKeyWithPassword(password string, opts ...any
// 生成公钥 pem 数据
func (this Curve25519) CreatePublicKey() Curve25519 {
if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand All @@ -111,12 +111,12 @@ func (this Curve25519) CreatePublicKey() Curve25519 {
// 根据公钥和私钥生成密钥
func (this Curve25519) CreateSecretKey() Curve25519 {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cryptobin/dh/curve25519/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (this Curve25519) MakePublicKey() Curve25519 {
this.publicKey = nil

if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand Down
5 changes: 2 additions & 3 deletions cryptobin/dh/curve25519/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"crypto"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/curve25519"

cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

var (
Expand Down Expand Up @@ -52,7 +51,7 @@ func (this Curve25519) ParsePrivateKeyFromPEMWithPassword(key []byte, password s
}

var blockDecrypted []byte
if blockDecrypted, err = cryptobin_pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
if blockDecrypted, err = pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
return nil, err
}

Expand Down
26 changes: 13 additions & 13 deletions cryptobin/dh/dh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"crypto/rand"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/dh"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

type (
// 配置
Opts = cryptobin_pkcs8.Opts
Opts = pkcs8.Opts
// PBKDF2 配置
PBKDF2Opts = cryptobin_pkcs8.PBKDF2Opts
PBKDF2Opts = pkcs8.PBKDF2Opts
// Scrypt 配置
ScryptOpts = cryptobin_pkcs8.ScryptOpts
ScryptOpts = pkcs8.ScryptOpts
)

var (
// 获取 Cipher 类型
GetCipherFromName = cryptobin_pkcs8.GetCipherFromName
GetCipherFromName = pkcs8.GetCipherFromName
// 获取 hash 类型
GetHashFromName = cryptobin_pkcs8.GetHashFromName
GetHashFromName = pkcs8.GetHashFromName
)

// 生成私钥 pem 数据
Expand All @@ -31,7 +31,7 @@ var (
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this DH) CreatePrivateKey() DH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand All @@ -54,11 +54,11 @@ func (this DH) CreatePrivateKey() DH {
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

opt, err := cryptobin_pkcs8.ParseOpts(opts...)
opt, err := pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -70,7 +70,7 @@ func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH {
}

// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPEMBlock(
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
Expand All @@ -89,7 +89,7 @@ func (this DH) CreatePrivateKeyWithPassword(password string, opts ...any) DH {
// 生成公钥 pem 数据
func (this DH) CreatePublicKey() DH {
if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand All @@ -111,12 +111,12 @@ func (this DH) CreatePublicKey() DH {
// 根据公钥和私钥生成密钥
func (this DH) CreateSecretKey() DH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cryptobin/dh/dh/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (this DH) MakePublicKey() DH {
this.publicKey = nil

if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand Down
5 changes: 2 additions & 3 deletions cryptobin/dh/dh/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"crypto"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/dh"

cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

var (
Expand Down Expand Up @@ -52,7 +51,7 @@ func (this DH) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (
}

var blockDecrypted []byte
if blockDecrypted, err = cryptobin_pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
if blockDecrypted, err = pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
return nil, err
}

Expand Down
26 changes: 13 additions & 13 deletions cryptobin/dh/ecdh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"crypto/rand"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/ecdh"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

type (
// 配置
Opts = cryptobin_pkcs8.Opts
Opts = pkcs8.Opts
// PBKDF2 配置
PBKDF2Opts = cryptobin_pkcs8.PBKDF2Opts
PBKDF2Opts = pkcs8.PBKDF2Opts
// Scrypt 配置
ScryptOpts = cryptobin_pkcs8.ScryptOpts
ScryptOpts = pkcs8.ScryptOpts
)

var (
// 获取 Cipher 类型
GetCipherFromName = cryptobin_pkcs8.GetCipherFromName
GetCipherFromName = pkcs8.GetCipherFromName
// 获取 hash 类型
GetHashFromName = cryptobin_pkcs8.GetHashFromName
GetHashFromName = pkcs8.GetHashFromName
)

// 生成私钥 pem 数据
Expand All @@ -31,7 +31,7 @@ var (
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this ECDH) CreatePrivateKey() ECDH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand All @@ -54,11 +54,11 @@ func (this ECDH) CreatePrivateKey() ECDH {
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

opt, err := cryptobin_pkcs8.ParseOpts(opts...)
opt, err := pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -70,7 +70,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
}

// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPEMBlock(
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
Expand All @@ -89,7 +89,7 @@ func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH
// 生成公钥 pem 数据
func (this ECDH) CreatePublicKey() ECDH {
if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand All @@ -111,12 +111,12 @@ func (this ECDH) CreatePublicKey() ECDH {
// 根据公钥和私钥生成密钥
func (this ECDH) CreateSecretKey() ECDH {
if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

if this.publicKey == nil {
err := errors.New("publicKey error.")
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cryptobin/dh/ecdh/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (this ECDH) MakePublicKey() ECDH {
this.publicKey = nil

if this.privateKey == nil {
err := errors.New("privateKey error.")
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

Expand Down
5 changes: 2 additions & 3 deletions cryptobin/dh/ecdh/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"crypto"
"encoding/pem"

"github.com/deatil/go-cryptobin/pkcs8"
"github.com/deatil/go-cryptobin/dh/ecdh"

cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)

var (
Expand Down Expand Up @@ -52,7 +51,7 @@ func (this ECDH) ParsePrivateKeyFromPEMWithPassword(key []byte, password string)
}

var blockDecrypted []byte
if blockDecrypted, err = cryptobin_pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
if blockDecrypted, err = pkcs8.DecryptPEMBlock(block, []byte(password)); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 157fe8e

Please sign in to comment.