Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 31, 2024
1 parent a758b88 commit c48d76f
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 37 deletions.
155 changes: 155 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/encryption.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sm2

import (
"bytes"
"errors"
"crypto/rand"

Expand Down Expand Up @@ -88,3 +89,157 @@ func (this SM2) DecryptASN1() SM2 {

return this
}

// ====================

const ecbSize = 256

// 公钥加密, ECB 模式
func (this SM2) EncryptECB() SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

plainText := this.data
pubSize, plainTextSize := ecbSize, len(plainText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < plainTextSize {
endIndex := offSet + pubSize
if endIndex > plainTextSize {
endIndex = plainTextSize
}

enc := this.FromBytes(plainText[offSet:endIndex]).Encrypt()

err := enc.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := enc.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

// 私钥解密, ECB 模式
func (this SM2) DecryptECB() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

cipherText := this.data
priSize, cipherTextSize := ecbSize, len(cipherText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < cipherTextSize {
endIndex := offSet + priSize
if endIndex > cipherTextSize {
endIndex = cipherTextSize
}

dec := this.FromBytes(cipherText[offSet:endIndex]).Decrypt()

err := dec.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := dec.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

// ====================

// 公钥加密,返回 asn.1 编码格式的密文内容, ECB 模式
func (this SM2) EncryptASN1ECB() SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

plainText := this.data
pubSize, plainTextSize := ecbSize, len(plainText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < plainTextSize {
endIndex := offSet + pubSize
if endIndex > plainTextSize {
endIndex = plainTextSize
}

enc := this.FromBytes(plainText[offSet:endIndex]).EncryptASN1()

err := enc.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := enc.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

// 私钥解密,返回 asn.1 编码格式的密文内容, ECB 模式
func (this SM2) DecryptASN1ECB() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

cipherText := this.data
priSize, cipherTextSize := ecbSize, len(cipherText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < cipherTextSize {
endIndex := offSet + priSize
if endIndex > cipherTextSize {
endIndex = cipherTextSize
}

dec := this.FromBytes(cipherText[offSet:endIndex]).DecryptASN1()

err := dec.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := dec.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}
73 changes: 73 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/sm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sm2
import (
"errors"
"testing"
"strings"
"crypto/md5"
"crypto/rand"
"encoding/hex"
Expand Down Expand Up @@ -1091,3 +1092,75 @@ jGAJaWDORkhDVOkYH2Rt`
assertError(de.Error(), "Test_SignAndDecrypt_Check22-DecryptASN1")
assertEqual(deData, check, "Test_SignAndDecrypt_Check22-DecryptASN1")
}

func Test_SM2_EncryptECB(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)
assertError := cryptobin_test.AssertErrorT(t)
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

sm2key := "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E="
sm2keyBytes, err2 := base64.StdEncoding.DecodeString(sm2key)

assertError(err2, "Test_SM2_EncryptECB-sm2keyDecode")

data := strings.Repeat("test-pass", 1 << 12)

sm2 := NewSM2()

en := sm2.
FromString(data).
FromPrivateKeyBytes(sm2keyBytes).
MakePublicKey().
Encrypt()
enData := en.ToBase64String()

assertError(en.Error(), "Test_SM2_EncryptECB-Encrypt")
assertNotEmpty(enData, "Test_SM2_EncryptECB-Encrypt")

de := sm2.
FromBase64String(enData).
FromPrivateKeyBytes(sm2keyBytes).
Decrypt()
deData := de.ToString()

assertError(de.Error(), "Test_SM2_EncryptECB-Decrypt")
assertNotEmpty(deData, "Test_SM2_EncryptECB-Decrypt")

assertEqual(data, deData, "Test_SM2_EncryptECB-Dedata")
}

func Test_SM2_EncryptASN1ECB(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)
assertError := cryptobin_test.AssertErrorT(t)
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

sm2key := "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E="
sm2keyBytes, err2 := base64.StdEncoding.DecodeString(sm2key)

assertError(err2, "Test_SM2_EncryptASN1ECB-sm2keyDecode")

data := strings.Repeat("test-pass", 1 << 12)

sm2 := NewSM2()

en := sm2.
FromString(data).
FromPrivateKeyBytes(sm2keyBytes).
MakePublicKey().
EncryptASN1()
enData := en.ToBase64String()

assertError(en.Error(), "Test_SM2_EncryptASN1ECB-Encrypt")
assertNotEmpty(enData, "Test_SM2_EncryptASN1ECB-Encrypt")

de := sm2.
FromBase64String(enData).
FromPrivateKeyBytes(sm2keyBytes).
DecryptASN1()
deData := de.ToString()

assertError(de.Error(), "Test_SM2_EncryptASN1ECB-Decrypt")
assertNotEmpty(deData, "Test_SM2_EncryptASN1ECB-Decrypt")

assertEqual(data, deData, "Test_SM2_EncryptASN1ECB-Dedata")
}
6 changes: 6 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/gm/sm2/sm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ type PublicKey struct {
X, Y *big.Int
}

// Size returns the maximum length of the shared key the
// public key can produce.
func (pub *PublicKey) Size() int {
return (pub.Curve.Params().BitSize + 7) / 8
}

// Equal reports whether pub and x have the same value.
func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
xx, ok := x.(*PublicKey)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lakego-pkg/go-cryptobin/kdf/smkdf/smkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func Key(h func() hash.Hash, z []byte, size int) []byte {
mdSize := md.Size()

limit := (uint64(size) + uint64(mdSize) - 1) / uint64(mdSize)
if limit >= (1 << uint64(mdSize) - 1) {
if limit >= uint64(1 << 32) - 1 {
panic("kdf: key length too long")
}

var countBytes [4]byte
var count uint32 = 1
var k []byte

for i := 0; i < int(limit); i++ {
for i := uint64(0); i < limit; i++ {
binary.BigEndian.PutUint32(countBytes[:], count)

md.Reset()
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-datebin/datebin/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
EST = "EST"
GMT = "GMT"
UTC = "UTC"
UCT = "UCT"
CTT = "CTT"
MST = "MST"

Cuba = "Cuba" // 古巴
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-hash/hamsi/digest256.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
Size224 = 28
Size256 = 32

BlockSize256 = 4
BlockSize256 = 32
)

// digest256 represents the partial evaluation of a checksum.
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-hash/hamsi/digest512.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
Size384 = 48
Size512 = 64

BlockSize512 = 8
BlockSize512 = 32
)

// digest512 represents the partial evaluation of a checksum.
Expand Down
Loading

0 comments on commit c48d76f

Please sign in to comment.