Skip to content

Commit

Permalink
优化更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Aug 25, 2024
1 parent 6d272cf commit 5bc24a0
Show file tree
Hide file tree
Showing 20 changed files with 3,042 additions and 86 deletions.
38 changes: 20 additions & 18 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"github.com/deatil/go-cryptobin/tool"
)

var usePadding = tool.NewPadding()

type ZeroPaddinger struct {}

// Padding 补码模式 / padding type
func (this ZeroPaddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ZeroPadding(plainText, blockSize)
return usePadding.ZeroPadding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this ZeroPaddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ZeroUnPadding(cipherText)
return usePadding.ZeroUnPadding(cipherText)
}

// ===================
Expand All @@ -22,12 +24,12 @@ type PKCS5Paddinger struct {}

// Padding 补码模式 / padding type
func (this PKCS5Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PKCS5Padding(plainText)
return usePadding.PKCS5Padding(plainText)
}

// UnPadding 补码模式 / unpadding type
func (this PKCS5Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PKCS5UnPadding(cipherText)
return usePadding.PKCS5UnPadding(cipherText)
}

// ===================
Expand All @@ -36,12 +38,12 @@ type PKCS7Paddinger struct {}

// Padding 补码模式 / padding type
func (this PKCS7Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PKCS7Padding(plainText, blockSize)
return usePadding.PKCS7Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this PKCS7Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PKCS7UnPadding(cipherText)
return usePadding.PKCS7UnPadding(cipherText)
}

// ===================
Expand All @@ -50,12 +52,12 @@ type X923Paddinger struct {}

// Padding 补码模式 / padding type
func (this X923Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().X923Padding(plainText, blockSize)
return usePadding.X923Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this X923Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().X923UnPadding(cipherText)
return usePadding.X923UnPadding(cipherText)
}

// ===================
Expand All @@ -64,12 +66,12 @@ type ISO10126Paddinger struct {}

// Padding 补码模式 / padding type
func (this ISO10126Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO10126Padding(plainText, blockSize)
return usePadding.ISO10126Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this ISO10126Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO10126UnPadding(cipherText)
return usePadding.ISO10126UnPadding(cipherText)
}

// ===================
Expand All @@ -78,12 +80,12 @@ type ISO7816_4Paddinger struct {}

// Padding 补码模式 / padding type
func (this ISO7816_4Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO7816_4Padding(plainText, blockSize)
return usePadding.ISO7816_4Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this ISO7816_4Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO7816_4UnPadding(cipherText)
return usePadding.ISO7816_4UnPadding(cipherText)
}

// ===================
Expand All @@ -92,12 +94,12 @@ type ISO97971Paddinger struct {}

// Padding 补码模式 / padding type
func (this ISO97971Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().ISO97971Padding(plainText, blockSize)
return usePadding.ISO97971Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this ISO97971Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().ISO97971UnPadding(cipherText)
return usePadding.ISO97971UnPadding(cipherText)
}

// ===================
Expand All @@ -106,12 +108,12 @@ type PBOC2Paddinger struct {}

// Padding 补码模式 / padding type
func (this PBOC2Paddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().PBOC2Padding(plainText, blockSize)
return usePadding.PBOC2Padding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this PBOC2Paddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().PBOC2UnPadding(cipherText)
return usePadding.PBOC2UnPadding(cipherText)
}

// ===================
Expand All @@ -120,12 +122,12 @@ type TBCPaddinger struct {}

// Padding 补码模式 / padding type
func (this TBCPaddinger) Padding(plainText []byte, blockSize int, opt IOption) []byte {
return tool.NewPadding().TBCPadding(plainText, blockSize)
return usePadding.TBCPadding(plainText, blockSize)
}

// UnPadding 补码模式 / unpadding type
func (this TBCPaddinger) UnPadding(cipherText []byte, opt IOption) ([]byte, error) {
return tool.NewPadding().TBCUnPadding(cipherText)
return usePadding.TBCUnPadding(cipherText)
}

// ===================
Expand Down
9 changes: 9 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/docs/sm2.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ func main() {
// 设置 UID 值
// set uid data
var uid []byte = []byte("")

// 设置 hash
// set hash func
var hash = md5.New

obj := sm2.New()

// 私钥签名
// private key sign data
// 比如: SM2withSM3 => ... SetSignHash("SM3").Sign() ...
var priKeyPem string = ""
sigBase64String = obj.
FromString(data).
Expand All @@ -104,6 +109,8 @@ func main() {
// FromPKCS8PrivateKey([]byte(priKeyPem)).
// FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword).
// WithUID(uid).
// SetSignHash("SM3").
// WithSignHash(hash).
Sign().
// SignASN1().
// SignBytes().
Expand All @@ -116,6 +123,8 @@ func main() {
FromBase64String(sigBase64String).
FromPublicKey([]byte(pubKeyPem)).
// WithUID(uid).
// SetSignHash("SM3").
// WithSignHash(hash).
Verify([]byte(data)).
// VerifyASN1([]byte(data)).
// VerifyBytes([]byte(data)).
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-cryptobin/ecgdsa/ecgdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

type Hasher = func() hash.Hash

// SignerOpts contains options for creating and verifying EC-KCDSA signatures.
// SignerOpts contains options for creating and verifying EC-GDSA signatures.
type SignerOpts struct {
Hash Hasher
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/lakego-pkg/go-cryptobin/eckcdsa/eckcdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func SignUsingK(k *big.Int, priv *PrivateKey, hashFunc Hasher, msg []byte) (r, s
h.Reset()
h.Write(x1Bytes)
rBytes := h.Sum(nil)

r = new(big.Int).SetBytes(rBytes)
if Lh > w {
r = r.Mod(r, two_8w)
Expand All @@ -281,14 +282,15 @@ func SignUsingK(k *big.Int, priv *PrivateKey, hashFunc Hasher, msg []byte) (r, s
h.Write(cQ)
h.Write(msg)
vBytes := h.Sum(nil)

v := new(big.Int).SetBytes(vBytes)
if Lh > w {
v = v.Mod(v, two_8w)
}

// 6: e ← (r ⊕ v) mod n
e := new(big.Int)
e.Mod(e.Xor(r, v), n)
e := new(big.Int).Xor(r, v)
e.Mod(e, n)

// 7: t ← x(k - e) mod n
t := new(big.Int)
Expand Down Expand Up @@ -350,15 +352,6 @@ func VerifyWithRS(pub *PublicKey, hashFunc Hasher, data []byte, r, s *big.Int) b

t := s

var two_8w *big.Int
if Lh > w {
two_8w = big.NewInt(256)
two_8w.Exp(two_8w, big.NewInt(int64(w)), nil)
}

if r.Sign() <= 0 {
return false
}
if Lh > w {
if (r.BitLen()+7)/8 > w {
return false
Expand All @@ -368,10 +361,16 @@ func VerifyWithRS(pub *PublicKey, hashFunc Hasher, data []byte, r, s *big.Int) b
return false
}
}
if t.Sign() <= 0 || t.Cmp(n) >= 0 {
if t.Cmp(n) >= 0 {
return false
}

var two_8w *big.Int
if Lh > w {
two_8w = big.NewInt(256)
two_8w.Exp(two_8w, big.NewInt(int64(w)), nil)
}

// 2: cQ ← MSB(xQ ‖ yQ, L)
cQ := append(
padLeft(xQ.Bytes(), K),
Expand All @@ -384,6 +383,7 @@ func VerifyWithRS(pub *PublicKey, hashFunc Hasher, data []byte, r, s *big.Int) b
h.Write(cQ)
h.Write(data)
vBytes := h.Sum(nil)

v := new(big.Int).SetBytes(vBytes)
if Lh > w {
v.Mod(v, two_8w)
Expand All @@ -403,6 +403,7 @@ func VerifyWithRS(pub *PublicKey, hashFunc Hasher, data []byte, r, s *big.Int) b
h.Reset()
h.Write(x2Bytes)
rBytes := h.Sum(nil)

r2 := new(big.Int).SetBytes(rBytes)
if Lh > w {
r2.Mod(r2, two_8w)
Expand Down
Loading

0 comments on commit 5bc24a0

Please sign in to comment.