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 8049de6 commit 1d10bd1
Show file tree
Hide file tree
Showing 92 changed files with 2,733 additions and 1,530 deletions.
26 changes: 13 additions & 13 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/ca/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// 证书请求
func (this CA) CreateCSR() CA {
if this.privateKey == nil {
err := errors.New("CA: privateKey error.")
err := errors.New("privateKey error.")
return this.AppendError(err)
}

Expand All @@ -29,7 +29,7 @@ func (this CA) CreateCSR() CA {
case *sm2.PrivateKey:
certRequest, ok := this.certRequest.(*cryptobin_x509.CertificateRequest)
if !ok {
err := errors.New("CA: sm2 certRequest error.")
err := errors.New("sm2 certRequest error.")
return this.AppendError(err)
}

Expand All @@ -38,7 +38,7 @@ func (this CA) CreateCSR() CA {
default:
certRequest, ok := this.certRequest.(*x509.CertificateRequest)
if !ok {
err := errors.New("CA: certRequest error.")
err := errors.New("certRequest error.")
return this.AppendError(err)
}

Expand All @@ -62,7 +62,7 @@ func (this CA) CreateCSR() CA {
// CA 证书
func (this CA) CreateCA() CA {
if this.publicKey == nil || this.privateKey == nil {
err := errors.New("CA: publicKey or privateKey error.")
err := errors.New("publicKey or privateKey error.")
return this.AppendError(err)
}

Expand All @@ -73,7 +73,7 @@ func (this CA) CreateCA() CA {
case *sm2.PrivateKey:
cert, ok := this.cert.(*cryptobin_x509.Certificate)
if !ok {
err := errors.New("CA: sm2 cert error.")
err := errors.New("sm2 cert error.")
return this.AppendError(err)
}

Expand All @@ -84,7 +84,7 @@ func (this CA) CreateCA() CA {
default:
cert, ok := this.cert.(*x509.Certificate)
if !ok {
err := errors.New("CA: cert error.")
err := errors.New("cert error.")
return this.AppendError(err)
}

Expand All @@ -108,7 +108,7 @@ func (this CA) CreateCA() CA {
// 自签名证书
func (this CA) CreateCert(ca any) CA {
if this.publicKey == nil || this.privateKey == nil {
err := errors.New("CA: publicKey or privateKey error.")
err := errors.New("publicKey or privateKey error.")
return this.AppendError(err)
}

Expand All @@ -119,13 +119,13 @@ func (this CA) CreateCert(ca any) CA {
case *sm2.PrivateKey:
newCert, certOk := this.cert.(*cryptobin_x509.Certificate)
if !certOk {
err := errors.New("CA: sm2 cert error.")
err := errors.New("sm2 cert error.")
return this.AppendError(err)
}

newCa, caOk := ca.(*cryptobin_x509.Certificate)
if !caOk {
err := errors.New("CA: sm2 ca error.")
err := errors.New("sm2 ca error.")
return this.AppendError(err)
}

Expand All @@ -136,13 +136,13 @@ func (this CA) CreateCert(ca any) CA {
default:
newCert, certOk := this.cert.(*x509.Certificate)
if !certOk {
err := errors.New("CA: cert error.")
err := errors.New("cert error.")
return this.AppendError(err)
}

newCa, caOk := ca.(*x509.Certificate)
if !caOk {
err := errors.New("CA: ca error.")
err := errors.New("ca error.")
return this.AppendError(err)
}

Expand All @@ -166,7 +166,7 @@ func (this CA) CreateCert(ca any) CA {
// 私钥
func (this CA) CreatePrivateKey() CA {
if this.privateKey == nil {
err := errors.New("CA: privateKey error.")
err := errors.New("privateKey error.")
return this.AppendError(err)
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (this CA) CreatePrivateKey() CA {
}

default:
err := fmt.Errorf("CA: unsupported private key type: %T", privateKey)
err := fmt.Errorf("unsupported private key type: %T", privateKey)
return this.AppendError(err)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/ca/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ func (this CA) Verify(rootPEM string, certPEM string, opts x509.VerifyOptions) (
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
return false, errors.New("CA: failed to parse root certificate")
return false, errors.New("failed to parse root certificate")
}

block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return false, errors.New("CA: failed to parse certificate PEM")
return false, errors.New("failed to parse certificate PEM")
}

cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return false, errors.New("CA: failed to parse certificate: " + err.Error())
return false, errors.New("failed to parse certificate: " + err.Error())
}

// 重设
opts.Roots = roots

if _, err := cert.Verify(opts); err != nil {
return false, errors.New("CA: failed to verify certificate: " + err.Error())
return false, errors.New("failed to verify certificate: " + err.Error())
}

return true, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var UsePadding = NewDataSet[Padding, IPadding]()
// 获取加密解密方式
func getEncrypt(m Multiple) (IEncrypt, error) {
if !UseEncrypt.Has(m) {
err := fmt.Errorf("Cryptobin: Multiple [%s] is error.", m)
err := fmt.Errorf("Multiple [%s] is error.", m)
return nil, err
}

Expand Down
24 changes: 8 additions & 16 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,12 @@ type ModeGCM struct {}
func (this ModeGCM) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]byte, error) {
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := cipher.NewGCMWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -346,13 +345,12 @@ func (this ModeGCM) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]by
func (this ModeGCM) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byte, error) {
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := cipher.NewGCMWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -371,13 +369,12 @@ type ModeCCM struct {}
func (this ModeCCM) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]byte, error) {
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := ccm.NewCCMWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -393,13 +390,12 @@ func (this ModeCCM) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byt
// ccm nounce size, should be in [7,13]
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := ccm.NewCCMWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand Down Expand Up @@ -530,13 +526,12 @@ type ModeOCB struct {}
func (this ModeOCB) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]byte, error) {
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := ocb.NewOCBWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -552,13 +547,12 @@ func (this ModeOCB) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byt
// ocb nounce size, should be in [0, cipher.block.BlockSize]
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := ocb.NewOCBWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -583,13 +577,12 @@ type ModeEAX struct {}
func (this ModeEAX) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]byte, error) {
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := eax.NewEAXWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand All @@ -605,13 +598,12 @@ func (this ModeEAX) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byt
// eax nounce size, should be in > 0
nonceBytes := opt.Config().GetBytes("nonce")
if nonceBytes == nil {
err := fmt.Errorf("Cryptobin: nonce is empty.")
err := fmt.Errorf("nonce is empty.")
return nil, err
}

aead, err := eax.NewEAXWithNonceSize(block, len(nonceBytes))
if err != nil {
err = fmt.Errorf("Cryptobin: %w", err)
return nil, err
}

Expand Down
Loading

0 comments on commit 1d10bd1

Please sign in to comment.