Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 7, 2024
1 parent da45c31 commit 24dd419
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions gm/sm2/sm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,14 @@ func encrypt(random io.Reader, pub *PublicKey, data []byte) ([]byte, error) {
c = append(c, x1Buf...) // x分量
c = append(c, y1Buf...) // y分量

tm := []byte{}
tm = append(tm, x2Buf...)
tm = append(tm, data...)
tm = append(tm, y2Buf...)
md := sm3.New()
md.Write(x2Buf)
md.Write(data)
md.Write(y2Buf)

h := sm3.Sum(tm)
c = append(c, h[:]...)
h := md.Sum(nil)

c = append(c, h...)

// 生成密钥 / make key
ct, ok := kdf(length, x2Buf, y2Buf)
Expand Down Expand Up @@ -392,14 +393,14 @@ func decrypt(priv *PrivateKey, data []byte) ([]byte, error) {
// 解密密文 / decrypt data
subtle.XORBytes(c, c, data)

tm := []byte{}
tm = append(tm, x2Buf...)
tm = append(tm, c...)
tm = append(tm, y2Buf...)
md := sm3.New()
md.Write(x2Buf)
md.Write(c)
md.Write(y2Buf)

h := sm3.Sum(tm)
h := md.Sum(nil)

if bytes.Compare(h[:], hash) != 0 {
if bytes.Compare(h, hash) != 0 {
return c, errors.New("cryptobin/sm2: failed to decrypt")
}

Expand Down

0 comments on commit 24dd419

Please sign in to comment.