diff --git a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/cryptobin_test.go b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/cryptobin_test.go index 098f882d..db6539c6 100644 --- a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/cryptobin_test.go +++ b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/cryptobin_test.go @@ -615,7 +615,7 @@ func Test_AesCCMPKCS7Padding(t *testing.T) { SetKey("dfertf12dfertf12"). SetIv("dfertf12dfe"). Aes(). - CCM(). + CCMWithNonceSize(11). PKCS7Padding(). Encrypt() cyptStr := cypt.ToBase64String() @@ -626,7 +626,7 @@ func Test_AesCCMPKCS7Padding(t *testing.T) { SetKey("dfertf12dfertf12"). SetIv("dfertf12dfe"). Aes(). - CCM(). + CCMWithNonceSize(11). PKCS7Padding(). Decrypt() cyptdeStr := cyptde.ToString() @@ -1493,7 +1493,8 @@ func Test_Weapp_AES256_GCM_Check(t *testing.T) { authtag := "5qeM/2vZv+6KtScN94IpMg==" real_authTag, _ := base64.StdEncoding.DecodeString(authtag) - cypt := FromString(real_plaintext). + cypt := New(). + FromString(real_plaintext). WithKey(real_key). WithIv(real_iv). Aes(). @@ -1763,3 +1764,213 @@ func Test_Chacha20poly1305(t *testing.T) { assert(data, cyptdeStr, "Test_Chacha20poly1305") } + +func Test_Aes_CCM_PKCS7Padding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + CCM(). + PKCS7Padding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_CCM_PKCS7Padding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + CCM(). + PKCS7Padding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_CCM_PKCS7Padding-Decode") + + assert(data, cyptdeStr, "Test_Aes_CCM_PKCS7Padding") +} + +func Test_Aes_CCMWithNonceSize_PKCS7Padding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + CCMWithNonceSize(13). + PKCS7Padding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_CCMWithNonceSize_PKCS7Padding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + CCMWithNonceSize(13). + PKCS7Padding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_CCMWithNonceSize_PKCS7Padding-Decode") + + assert(data, cyptdeStr, "Test_Aes_CCMWithNonceSize_PKCS7Padding") +} + +func Test_Aes_CCMWithTagSize_NoPadding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + CCMWithTagSize(12). + NoPadding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_CCMWithTagSize_NoPadding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + CCMWithTagSize(12). + NoPadding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_CCMWithTagSize_NoPadding-Decode") + + assert(data, cyptdeStr, "Test_Aes_CCMWithTagSize_NoPadding") +} + +func Test_Aes_CCMWithNonceAndTagSize_PKCS7Padding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + CCMWithNonceAndTagSize(13, 16). + PKCS7Padding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_CCMWithNonceAndTagSize_PKCS7Padding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + CCMWithNonceAndTagSize(13, 16). + PKCS7Padding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_CCMWithNonceAndTagSize_PKCS7Padding-Decode") + + assert(data, cyptdeStr, "Test_Aes_CCMWithNonceAndTagSize_PKCS7Padding") +} + +func Test_Aes_GCM_PKCS7Padding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + GCM(). + PKCS7Padding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_GCM_PKCS7Padding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + GCM(). + PKCS7Padding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_GCM_PKCS7Padding-Decode") + + assert(data, cyptdeStr, "Test_Aes_GCM_PKCS7Padding") +} + +func Test_Aes_GCMWithNonceSize_PKCS7Padding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + GCMWithNonceSize(13). + PKCS7Padding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_GCMWithNonceSize_PKCS7Padding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe12"). + Aes(). + GCMWithNonceSize(13). + PKCS7Padding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_GCMWithNonceSize_PKCS7Padding-Decode") + + assert(data, cyptdeStr, "Test_Aes_GCMWithNonceSize_PKCS7Padding") +} + +func Test_Aes_GCMWithTagSize_NoPadding(t *testing.T) { + assert := cryptobin_test.AssertEqualT(t) + assertError := cryptobin_test.AssertErrorT(t) + + data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass" + cypt := New().FromString(data). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + GCMWithTagSize(15). + NoPadding(). + Encrypt() + cyptStr := cypt.ToBase64String() + + assertError(cypt.Error(), "Test_Aes_GCMWithTagSize_NoPadding-Encode") + + cyptde := New().FromBase64String(cyptStr). + SetKey("dfertf12dfertf12"). + SetIv("dfertf12dfe1"). + Aes(). + GCMWithTagSize(15). + NoPadding(). + Decrypt() + cyptdeStr := cyptde.ToString() + + assertError(cyptde.Error(), "Test_Aes_GCMWithTagSize_NoPadding-Decode") + + assert(data, cyptdeStr, "Test_Aes_GCMWithTagSize_NoPadding") +} diff --git a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_mode.go b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_mode.go index dd7e081a..c4f9e292 100644 --- a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_mode.go +++ b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_mode.go @@ -245,19 +245,22 @@ func (this ModeGCM) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]by var aead cipher.AEAD var err error - iv := opt.Iv() - + nonceSize := opt.Config().GetInt("nonce_size") tagSize := opt.Config().GetInt("tag_size") + if tagSize > 0 { aead, err = cipher.NewGCMWithTagSize(block, tagSize) + } else if nonceSize > 0 { + aead, err = cipher.NewGCMWithNonceSize(block, nonceSize) } else { - aead, err = cipher.NewGCMWithNonceSize(block, len(iv)) + aead, err = cipher.NewGCM(block) } if err != nil { return nil, err } + iv := opt.Iv() additional := opt.Config().GetBytes("additional") cryptText := aead.Seal(nil, iv, plain, additional) @@ -270,19 +273,22 @@ func (this ModeGCM) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byt var aead cipher.AEAD var err error - iv := opt.Iv() - + nonceSize := opt.Config().GetInt("nonce_size") tagSize := opt.Config().GetInt("tag_size") + if tagSize > 0 { aead, err = cipher.NewGCMWithTagSize(block, tagSize) + } else if nonceSize > 0 { + aead, err = cipher.NewGCMWithNonceSize(block, nonceSize) } else { - aead, err = cipher.NewGCMWithNonceSize(block, len(iv)) + aead, err = cipher.NewGCM(block) } if err != nil { return nil, err } + iv := opt.Iv() additional := opt.Config().GetBytes("additional") dst, err := aead.Open(nil, iv, data, additional) @@ -299,19 +305,24 @@ func (this ModeCCM) Encrypt(plain []byte, block cipher.Block, opt IOption) ([]by var aead cipher.AEAD var err error - iv := opt.Iv() - + nonceSize := opt.Config().GetInt("nonce_size") tagSize := opt.Config().GetInt("tag_size") - if tagSize > 0 { + + if nonceSize > 0 && tagSize > 0 { + aead, err = ccm.NewCCMWithNonceAndTagSize(block, nonceSize, tagSize) + } else if tagSize > 0 { aead, err = ccm.NewCCMWithTagSize(block, tagSize) + } else if nonceSize > 0 { + aead, err = ccm.NewCCMWithNonceSize(block, nonceSize) } else { - aead, err = ccm.NewCCMWithNonceSize(block, len(iv)) + aead, err = ccm.NewCCM(block) } if err != nil { return nil, err } + iv := opt.Iv() additional := opt.Config().GetBytes("additional") cryptText := aead.Seal(nil, iv, plain, additional) @@ -325,15 +336,20 @@ func (this ModeCCM) Decrypt(data []byte, block cipher.Block, opt IOption) ([]byt var aead cipher.AEAD var err error - iv := opt.Iv() - + nonceSize := opt.Config().GetInt("nonce_size") tagSize := opt.Config().GetInt("tag_size") - if tagSize > 0 { + + if nonceSize > 0 && tagSize > 0 { + aead, err = ccm.NewCCMWithNonceAndTagSize(block, nonceSize, tagSize) + } else if tagSize > 0 { aead, err = ccm.NewCCMWithTagSize(block, tagSize) + } else if nonceSize > 0 { + aead, err = ccm.NewCCMWithNonceSize(block, nonceSize) } else { - aead, err = ccm.NewCCMWithNonceSize(block, len(iv)) + aead, err = ccm.NewCCM(block) } + iv := opt.Iv() additional := opt.Config().GetBytes("additional") dst, err := aead.Open(nil, iv, data, additional) diff --git a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/use.go b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/use.go index 8af7d42e..4b2686ff 100644 --- a/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/use.go +++ b/pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/use.go @@ -470,6 +470,21 @@ func (this Cryptobin) CTR() Cryptobin { func (this Cryptobin) GCM(additional ...[]byte) Cryptobin { this.mode = GCM + this.config.Set("nonce_size", 0) + this.config.Set("tag_size", 0) + + if len(additional) > 0 { + this.config.Set("additional", additional[0]) + } + + return this +} + +// GCMWithNonceSize +func (this Cryptobin) GCMWithNonceSize(nonceSize int, additional ...[]byte) Cryptobin { + this.mode = GCM + + this.config.Set("nonce_size", nonceSize) this.config.Set("tag_size", 0) if len(additional) > 0 { @@ -483,6 +498,7 @@ func (this Cryptobin) GCM(additional ...[]byte) Cryptobin { func (this Cryptobin) GCMWithTagSize(tagSize int, additional ...[]byte) Cryptobin { this.mode = GCM + this.config.Set("nonce_size", 0) this.config.Set("tag_size", tagSize) if len(additional) > 0 { @@ -493,10 +509,25 @@ func (this Cryptobin) GCMWithTagSize(tagSize int, additional ...[]byte) Cryptobi } // CCM -// ccm nounce size, should be in [7,13] func (this Cryptobin) CCM(additional ...[]byte) Cryptobin { this.mode = CCM + this.config.Set("nonce_size", 0) + this.config.Set("tag_size", 0) + + if len(additional) > 0 { + this.config.Set("additional", additional[0]) + } + + return this +} + +// CCMWithNonceSize +// ccm nonce size, should be in [7,13] +func (this Cryptobin) CCMWithNonceSize(nonceSize int, additional ...[]byte) Cryptobin { + this.mode = CCM + + this.config.Set("nonce_size", nonceSize) this.config.Set("tag_size", 0) if len(additional) > 0 { @@ -507,10 +538,25 @@ func (this Cryptobin) CCM(additional ...[]byte) Cryptobin { } // CCMWithTagSize -// ccm nounce size, should be in [7,13] +// Tag sizes between 8 and 16 bytes are allowed. func (this Cryptobin) CCMWithTagSize(tagSize int, additional ...[]byte) Cryptobin { this.mode = CCM + this.config.Set("nonce_size", 0) + this.config.Set("tag_size", tagSize) + + if len(additional) > 0 { + this.config.Set("additional", additional[0]) + } + + return this +} + +// CCMWithNonceSize +func (this Cryptobin) CCMWithNonceAndTagSize(nonceSize, tagSize int, additional ...[]byte) Cryptobin { + this.mode = CCM + + this.config.Set("nonce_size", nonceSize) this.config.Set("tag_size", tagSize) if len(additional) > 0 { diff --git a/pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/sm2_test.go b/pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/sm2_test.go index 79a5a1f7..fa58481e 100644 --- a/pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/sm2_test.go +++ b/pkg/lakego-pkg/go-cryptobin/cryptobin/sm2/sm2_test.go @@ -413,7 +413,11 @@ func test_CreatePKCS1PrivateKeyWithPassword(t *testing.T, cipher string) { }) } -// 招商银行签名会因为业务不同用的签名方法也会不同,签名方法默认有 SignBytes 和 SignASN1 两种,可根据招商银行给的 demo 选择对应的方法使用 +// 招商银行签名会因为业务不同用的签名方法也会不同, +// 签名方法默认有 SignBytes 和 SignASN1 两种, +// 可根据招商银行给的 demo 选择对应的方法使用 +// SignBytes 为签名数据明文拼接 +// SignASN1 为签名数据做 ASN.1 编码 func Test_ZhaoshangBank_Check(t *testing.T) { assertBool := cryptobin_test.AssertBoolT(t) @@ -490,7 +494,7 @@ func Test_ZhaoshangBank_Sign2(t *testing.T) { func Test_ZhaoshangBank_Verify(t *testing.T) { assertBool := cryptobin_test.AssertBoolT(t) - // 未压缩公钥明文,16进制 + // 未压缩公钥明文, 16进制 sm2pubkey := "046374f8947b208b3f28a2dfaec78510f858bc1bad37f038b95903975c9636beb859653fb145727d02d65cd68f202abc2ff93eecea477b1dc81f4f650621b89e9d" sm2data := `{"request":{"body":{"TEST":"中文","TEST2":"!@#$%^&*()","TEST3":12345,"TEST4":[{"arrItem1":"qaz","arrItem2":123,"arrItem3":true,"arrItem4":"中文"}],"buscod":"N02030"},"head":{"funcode":"DCLISMOD","userid":"N003261207"}},"signature":{"sigdat":"__signature_sigdat__"}}` sm2userid := "N0032612070000000000000000" @@ -507,13 +511,13 @@ func Test_ZhaoshangBank_Verify(t *testing.T) { // VerifyASN1([]byte(sm2data)). ToVerify() - assertBool(sm2Verify, "ZhaoshangBank_Verify") + assertBool(sm2Verify, "Test_ZhaoshangBank_Verify") } func Test_ZhaoshangBank_Verify2(t *testing.T) { assertBool := cryptobin_test.AssertBoolT(t) - // 压缩公钥明文,16进制 + // 压缩公钥明文, 16进制 sm2pubkey := "036374f8947b208b3f28a2dfaec78510f858bc1bad37f038b95903975c9636beb8" sm2data := `{"request":{"body":{"TEST":"中文","TEST2":"!@#$%^&*()","TEST3":12345,"TEST4":[{"arrItem1":"qaz","arrItem2":123,"arrItem3":true,"arrItem4":"中文"}],"buscod":"N02030"},"head":{"funcode":"DCLISMOD","userid":"N003261207"}},"signature":{"sigdat":"__signature_sigdat__"}}` sm2userid := "N0032612070000000000000000" @@ -530,7 +534,7 @@ func Test_ZhaoshangBank_Verify2(t *testing.T) { // VerifyASN1([]byte(sm2data)). ToVerify() - assertBool(sm2Verify, "ZhaoshangBank_Verify") + assertBool(sm2Verify, "Test_ZhaoshangBank_Verify2") } func Test_PKCS1SignWithHash(t *testing.T) { diff --git a/pkg/lakego-pkg/go-cryptobin/docs/bencode.md b/pkg/lakego-pkg/go-cryptobin/docs/bencode.md index 7da15b78..91de9f77 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/bencode.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/bencode.md @@ -38,7 +38,8 @@ var data map[string]any // 数据结果 var torrentData []byte +var err error // 生成操作 -torrentData, err := bencode.Marshal(data) +torrentData, err = bencode.Marshal(data) ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/bign.md b/pkg/lakego-pkg/go-cryptobin/docs/bign.md index 4caf5fb3..64ba8d2e 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/bign.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/bign.md @@ -33,24 +33,24 @@ Error() func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成证书 // 可选 [Bign256v1 | Bign384v1 | Bign512v1] gen := bign.GenerateKey("Bign256v1") // 生成私钥 PEM 证书 - privateKeyString := gen. + privateKeyPEM := gen. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey() - // CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC") // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). ToKeyString() // 生成公钥 PEM 证书 - publicKeyString := gen. + publicKeyPEM := gen. CreatePublicKey(). ToKeyString() } @@ -75,18 +75,18 @@ SignBytes() / VerifyBytes(data []byte) func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 私钥签名 - var pri []byte = []byte("...") + var prikeyPem []byte = []byte("...") var base64signedString string = bign. FromString("test-pass"). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + // FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). // WithEncodingASN1(). // WithEncodingBytes(). WithAdata(adata). // 设置 adata 参数 @@ -94,11 +94,11 @@ func main() { ToBase64String() // 公钥验证 - var pub []byte = []byte("...") + var pubkeyPem []byte = []byte("...") var base64signedString string = "..." var verify bool = bign. FromBase64String(base64signedString). - FromPublicKey(pub). + FromPublicKey(pubkeyPem). // WithEncodingASN1(). // WithEncodingBytes(). WithAdata(adata). // 设置 adata 参数 @@ -111,21 +111,21 @@ func main() { #### 检测私钥公钥是否匹配 ~~~go func main() { - // 私钥密码 - // privatekey password - var psssword string = "" - var prikeyPem []byte = []byte("...") var pubkeyPem []byte = []byte("...") + // 私钥密码 + // privatekey password + var password string = "" + var res bool = bign.New(). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). - FromPublicKey(pubkey). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + // FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/dsa.md b/pkg/lakego-pkg/go-cryptobin/docs/dsa.md index b4a2fffc..410ee1e3 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/dsa.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/dsa.md @@ -73,8 +73,6 @@ func main() { #### 签名验证 / sign data ~~~go func main() { - obj := dsa.New() - // 待签名数据 // no sign data var data string = "..." @@ -83,18 +81,25 @@ func main() { // sign data var sigBase64String string = "..." + var priKeyPem []byte = []byte("...") + var pubKeyPem []byte = []byte("...") + + var priKeyXML []byte = []byte("...") + var pubKeyXML []byte = []byte("...") + + obj := dsa.New() + // 私钥签名 // private key sign data - var priKeyPem string = "" sigBase64String = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), "123"). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), "123"). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), "123"). - // FromXMLPrivateKey([]byte(priKeyXML)). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, "123"). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, "123"). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, "123"). + // FromXMLPrivateKey(priKeyXML). SetSignHash("SHA256"). Sign(). // SignASN1(). @@ -104,13 +109,12 @@ func main() { // 公钥验证 // public key verify signed data - var pubKeyPem string = "" var res bool = obj. FromBase64String(sigBase64String). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). - // FromXMLPublicKey([]byte(pubKeyXML)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). + // FromXMLPublicKey(pubKeyXML). SetSignHash("SHA256"). Verify([]byte(data)). // VerifyASN1([]byte(data)). @@ -123,19 +127,19 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var prikeyPem string = "..." - var pubkeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") var res bool = dsa.New(). - // FromPrivateKey([]byte(prikeyPem)). - // FromPrivateKeyWithPassword([]byte(prikeyPem), "123"). - // FromPKCS1PrivateKey([]byte(prikeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(prikeyPem), "123"). - FromPKCS8PrivateKey([]byte(prikeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(prikeyPem), "123"). - // FromPublicKey([]byte(pubkeyPem)). - // FromPKCS1PublicKey([]byte(pubkeyPem)). - FromPKCS8PublicKey([]byte(pubkeyPem)). + // FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, "123"). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, "123"). + FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, "123"). + // FromPublicKey(pubkeyPem). + // FromPKCS1PublicKey(pubkeyPem). + FromPKCS8PublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/ecdh.md b/pkg/lakego-pkg/go-cryptobin/docs/ecdh.md index 5df1a49d..5af50091 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/ecdh.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/ecdh.md @@ -38,19 +38,19 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "DESEDE3CBC"). + // CreatePrivateKeyWithPassword(password, "DESEDE3CBC"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePrivateKeyWithPassword(psssword, sm2.Opts{ + CreatePrivateKeyWithPassword(password, sm2.Opts{ Cipher: sm2.GetCipherFromName("AES256CBC"), KDFOpts: sm2.ScryptOpts{ CostParameter: 1 << 15, @@ -72,27 +72,27 @@ func main() { #### 生成对称加密密钥 ~~~go func main() { - var prikeyPem1 string = "..." - var pubkeyPem1 string = "..." + var prikeyPem1 []byte = []byte("...") + var pubkeyPem1 []byte = []byte("...") - var prikeyPem2 string = "..." - var pubkeyPem2 string = "..." + var prikeyPem2 []byte = []byte("...") + var pubkeyPem2 []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var secret1 string = obj. - FromPrivateKey([]byte(prikeyPem1)). - // FromPrivateKeyWithPassword([]byte(prikeyPem1), psssword). - FromPublicKey([]byte(pubkeyPem2)). + FromPrivateKey(prikeyPem1). + // FromPrivateKeyWithPassword(prikeyPem1, password). + FromPublicKey(pubkeyPem2). CreateSecretKey(). ToHexString() var secret2 string = obj. - FromPrivateKey([]byte(prikeyPem2)). - // FromPrivateKeyWithPassword([]byte(prikeyPem2), psssword). - FromPublicKey([]byte(pubkeyPem1)). + FromPrivateKey(prikeyPem2). + // FromPrivateKeyWithPassword(prikeyPem2, password). + FromPublicKey(pubkeyPem1). CreateSecretKey(). ToHexString() diff --git a/pkg/lakego-pkg/go-cryptobin/docs/ecdsa.md b/pkg/lakego-pkg/go-cryptobin/docs/ecdsa.md index a4e05d10..08364db0 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/ecdsa.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/ecdsa.md @@ -33,24 +33,24 @@ Error() func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成证书 // 可选参数 [P521 | P384 | P256 | P224] ec := ecdsa.GenerateKey("P521") // 生成私钥 PEM 证书 - privateKeyString := ec. + privateKeyPEM := ec. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey() - // CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC") // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). ToKeyString() // 生成公钥 PEM 证书 - publicKeyString := ec. + publicKeyPEM := ec. CreatePublicKey(). ToKeyString() } @@ -75,28 +75,30 @@ SignBytes() / VerifyBytes(data []byte) func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" + + var data string = "test-pass" // 私钥签名 - var pri []byte = []byte("...") + var priPEM []byte = []byte("...") var base64signedString string = ecdsa. - FromString("test-pass"). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). + FromString(data). + FromPrivateKey(priPEM). + // FromPrivateKeyWithPassword(priPEM, password). + // FromPKCS1PrivateKey(priPEM). + // FromPKCS1PrivateKeyWithPassword(priPEM, password). + // FromPKCS8PrivateKey(priPEM). + // FromPKCS8PrivateKeyWithPassword(priPEM, password). Sign(). ToBase64String() // 公钥验证 - var pub []byte = []byte("...") + var pubEM []byte = []byte("...") var base64signedString string = "..." var verify bool = ecdsa. FromBase64String(base64signedString). - FromPublicKey(pub). - Verify([]byte("test-pass")). + FromPublicKey(pubEM). + Verify([]byte(data)). ToVerify() } ~~~ @@ -146,19 +148,19 @@ Vu0zCh5hkl/0r9vPzPeqGpHJv3eJw/zF+gZWxn2LvLcKkQTcGutSwVdVRQ== func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var prikeyPem []byte = []byte("...") var pubkeyPem []byte = []byte("...") var res bool = ecdsa.New(). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). - FromPublicKey(pubkey). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + // FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/ecgdsa.md b/pkg/lakego-pkg/go-cryptobin/docs/ecgdsa.md index 75a60a73..2e23dce9 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/ecgdsa.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/ecgdsa.md @@ -33,24 +33,24 @@ Error() func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成证书 // 可选参数 [P521 | P384 | P256 | P224] ec := ecgdsa.GenerateKey("P521") // 生成私钥 PEM 证书 - privateKeyString := ec. + privateKeyPEM := ec. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey() - // CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC") // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). ToKeyString() // 生成公钥 PEM 证书 - publicKeyString := ec. + publicKeyPEM := ec. CreatePublicKey(). ToKeyString() } @@ -75,28 +75,30 @@ SignBytes() / VerifyBytes(data []byte) func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" + + var data string = "test-pass" // 私钥签名 - var pri []byte = []byte("...") + var priPEM []byte = []byte("...") var base64signedString string = ecgdsa. - FromString("test-pass"). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). + FromString(data). + FromPrivateKey(priPEM). + // FromPrivateKeyWithPassword(priPEM, password). + // FromPKCS1PrivateKey(priPEM). + // FromPKCS1PrivateKeyWithPassword(priPEM, password). + // FromPKCS8PrivateKey(priPEM). + // FromPKCS8PrivateKeyWithPassword(priPEM, password). Sign(). ToBase64String() // 公钥验证 - var pub []byte = []byte("...") + var pubPEM []byte = []byte("...") var base64signedString string = "..." var verify bool = ecgdsa. FromBase64String(base64signedString). - FromPublicKey(pub). - Verify([]byte("test-pass")). + FromPublicKey(pubPEM). + Verify([]byte(data)). ToVerify() } ~~~ @@ -107,19 +109,19 @@ func main() { func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var prikeyPem []byte = []byte("...") var pubkeyPem []byte = []byte("...") var res bool = ecgdsa.New(). - FromPrivateKey(pri). - // FromPrivateKeyWithPassword(pri, psssword). - // FromPKCS1PrivateKey(pri). - // FromPKCS1PrivateKeyWithPassword(pri, psssword). - // FromPKCS8PrivateKey(pri). - // FromPKCS8PrivateKeyWithPassword(pri, psssword). - FromPublicKey(pubkey). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + // FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/ed448.md b/pkg/lakego-pkg/go-cryptobin/docs/ed448.md index a750eca8..4f0a9ea6 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/ed448.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/ed448.md @@ -35,19 +35,19 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "DESEDE3CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePrivateKeyWithPassword(psssword, sm2.Opts{ + CreatePrivateKeyWithPassword(password, sm2.Opts{ Cipher: sm2.GetCipherFromName("AES256CBC"), KDFOpts: sm2.ScryptOpts{ CostParameter: 1 << 15, @@ -79,7 +79,7 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // ctx 数据 var ctx string = "" @@ -92,7 +92,7 @@ func main() { sigBase64String = obj. FromString(data). FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). + // FromPrivateKeyWithPassword([]byte(priKeyPem), password). // 其他设置, 默认为 ED448 模式, ctx 为空 // SetOptions("ED448", ""). // SetOptions("ED448", ctx). @@ -118,17 +118,17 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var prikeyPem string = "..." - var pubkeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var res bool = ed448.New(). - FromPrivateKey([]byte(prikeyPem)). - // FromPrivateKeyWithPassword([]byte(prikeyPem), psssword). - FromPublicKey([]byte(pubkeyPem)). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/eddsa.md b/pkg/lakego-pkg/go-cryptobin/docs/eddsa.md index e0e1414b..e67bad87 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/eddsa.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/eddsa.md @@ -35,19 +35,19 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "DESEDE3CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePrivateKeyWithPassword(psssword, sm2.Opts{ + CreatePrivateKeyWithPassword(password, sm2.Opts{ Cipher: sm2.GetCipherFromName("AES256CBC"), KDFOpts: sm2.ScryptOpts{ CostParameter: 1 << 15, @@ -79,7 +79,7 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // ctx 数据 var ctx string = "" @@ -92,11 +92,11 @@ func main() { sigBase64String = obj. FromString(data). FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). + // FromPrivateKeyWithPassword([]byte(priKeyPem), password). // 其他设置, 默认为 Ed25519 模式 + // SetOptions("Ed25519"). // SetOptions("Ed25519ph", ctx). // SetOptions("Ed25519ctx", ctx). - // SetOptions("Ed25519"). Sign(). ToBase64String() @@ -107,9 +107,9 @@ func main() { FromBase64String(sigBase64String). FromPublicKey([]byte(pubKeyPem)). // 其他设置, 默认为 Ed25519 模式 + // SetOptions("Ed25519"). // SetOptions("Ed25519ph", ctx). // SetOptions("Ed25519ctx", ctx). - // SetOptions("Ed25519"). Verify([]byte(data)). ToVerify() } @@ -118,17 +118,17 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var prikeyPem string = "..." - var pubkeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var res bool = eddsa.New(). - FromPrivateKey([]byte(prikeyPem)). - // FromPrivateKeyWithPassword([]byte(prikeyPem), psssword). - FromPublicKey([]byte(pubkeyPem)). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/elgamal.md b/pkg/lakego-pkg/go-cryptobin/docs/elgamal.md index ec19194b..a485eec9 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/elgamal.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/elgamal.md @@ -35,23 +35,23 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "DESEDE3CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey(). - // CreatePKCS1PrivateKeyWithPassword(psssword, "DESEDE3CBC"). + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePKCS8PrivateKeyWithPassword(psssword, sm2.Opts{ + CreatePKCS8PrivateKeyWithPassword(password, sm2.Opts{ Cipher: sm2.GetCipherFromName("AES256CBC"), KDFOpts: sm2.ScryptOpts{ CostParameter: 1 << 15, @@ -85,32 +85,32 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" obj := elgamal.New() // 私钥签名 // private key sign data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") sigBase64String = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). Sign(). ToBase64String() // 公钥验证 // public key verify signed data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") var res bool = obj. FromBase64String(sigBase64String). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). Verify([]byte(data)). ToVerify() } @@ -127,30 +127,30 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 公钥加密 // public key Encrypt data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") var enData string = obj. FromString(data). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). Encrypt(). ToBase64String() // 私钥解密 // private key Decrypt data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") var deData string = obj. FromBase64String(enData). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). Decrypt(). ToString() } @@ -159,23 +159,23 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var prikeyPem string = "..." - var pubkeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var res bool = elgamal.New(). - // FromPrivateKey([]byte(prikeyPem)). - // FromPrivateKeyWithPassword([]byte(prikeyPem), psssword). - // FromPKCS1PrivateKey([]byte(prikeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(prikeyPem), psssword). - FromPKCS8PrivateKey([]byte(prikeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(prikeyPem), psssword). - // FromPublicKey([]byte(pubkeyPem)). - // FromPKCS1PublicKey([]byte(pubkeyPem)). - FromPKCS8PublicKey([]byte(pubkeyPem)). + // FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). + // FromPublicKey(pubkeyPem). + // FromPKCS1PublicKey(pubkeyPem). + FromPKCS8PublicKey(pubkeyPem). CheckKeyPair() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/gost.md b/pkg/lakego-pkg/go-cryptobin/docs/gost.md index d2563492..57a857ee 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/gost.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/gost.md @@ -45,20 +45,20 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePrivateKeyWithPassword(psssword, gost.Opts{ + CreatePrivateKeyWithPassword(password, gost.Opts{ Cipher: gost.GetCipherFromName("AES256CBC"), KDFOpts: gost.ScryptOpts{ CostParameter: 1 << 15, @@ -90,7 +90,7 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 数据签名类型 // hash name type @@ -101,11 +101,11 @@ func main() { // 私钥签名 // private key sign data - var priKeyPem string = "" + var prikeyPem []byte = []byte("...") sigBase64String = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). // SetSignHash(hashName). Sign(). // SignASN1(). @@ -113,10 +113,10 @@ func main() { // 公钥验证 // public key verify signed data - var pubKeyPem string = "" + var pubkeyPem []byte = []byte("...") var res bool = obj. FromBase64String(sigBase64String). - FromPublicKey([]byte(pubKeyPem)). + FromPublicKey(pubkeyPem). // SetSignHash(hashName). Verify([]byte(data)). // VerifyASN1([]byte(data)). @@ -127,13 +127,13 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var priKeyPem string = "..." - var pubKeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") var res bool = gost.New(). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - FromPublicKey([]byte(pubKeyPem)). + FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + FromPublicKey(pubkeyPem). CheckKeyPair() } ~~~ @@ -141,24 +141,24 @@ func main() { #### 生成 VKO 密钥 ~~~go func main() { - var prikeyPem1 string = "..." - var pubkeyPem1 string = "..." + var prikeyPem1 []byte = []byte("...") + var pubkeyPem1 []byte = []byte("...") - var prikeyPem2 string = "..." - var pubkeyPem2 string = "..." + var prikeyPem2 []byte = []byte("...") + var pubkeyPem2 []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // ukm 数据 // ukm data var ukm []byte = []byte("...") var secret1 string = obj. - FromPrivateKey([]byte(prikeyPem1)). - // FromPrivateKeyWithPassword([]byte(prikeyPem1), psssword). - FromPublicKey([]byte(pubkeyPem2)). + FromPrivateKey(prikeyPem1). + // FromPrivateKeyWithPassword(prikeyPem1, password). + FromPublicKey(pubkeyPem2). KEK(ukm). // KEK2001(ukm). // KEK2012256(ukm). @@ -166,9 +166,9 @@ func main() { ToSecretString() var secret2 string = obj. - FromPrivateKey([]byte(prikeyPem2)). - // FromPrivateKeyWithPassword([]byte(prikeyPem2), psssword). - FromPublicKey([]byte(pubkeyPem1)). + FromPrivateKey(prikeyPem2). + // FromPrivateKeyWithPassword(prikeyPem2, password). + FromPublicKey(pubkeyPem1). KEK(ukm). // KEK2001(ukm). // KEK2012256(ukm). diff --git a/pkg/lakego-pkg/go-cryptobin/docs/rsa.md b/pkg/lakego-pkg/go-cryptobin/docs/rsa.md index d205102f..8ae13524 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/rsa.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/rsa.md @@ -37,7 +37,7 @@ Error() func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // bits = 512 | 1024 | 2048 | 4096 obj := rsa.New(). @@ -48,18 +48,18 @@ func main() { // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey(). - // CreatePKCS1PrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). // CreateXMLPrivateKey(). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePKCS8PrivateKeyWithPassword(psssword, rsa.Opts{ + CreatePKCS8PrivateKeyWithPassword(password, rsa.Opts{ Cipher: rsa.GetCipherFromName("AES256CBC"), KDFOpts: rsa.ScryptOpts{ CostParameter: 1 << 15, @@ -95,20 +95,21 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 私钥签名 // private key sign data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") + var priKeyXML []byte = []byte("...") sigBase64String = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromXMLPrivateKey([]byte(priKeyXML)). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). + // FromXMLPrivateKey(priKeyXML). SetSignHash("SHA256"). Sign(). // SignPSS(). @@ -116,13 +117,14 @@ func main() { // 公钥验证 // public key verify signed data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") + var pubKeyXML []byte = []byte("...") var res bool = obj. FromBase64String(sigBase64String). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). - // FromXMLPublicKey([]byte(pubKeyXML)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). + // FromXMLPublicKey(pubKeyXML). SetSignHash("SHA256"). Verify([]byte(data)). // VerifyPSS([]byte(data)). @@ -141,17 +143,18 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 公钥加密 // public key Encrypt data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") + var pubKeyXML []byte = []byte("...") var enData string = obj. FromString(data). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). - // FromXMLPublicKey([]byte(pubKeyXML)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). + // FromXMLPublicKey(pubKeyXML). Encrypt(). // SetOAEPHash("SHA256"). // OAEP 可选 // SetOAEPLabel("test-label"). // OAEP 可选 @@ -160,16 +163,17 @@ func main() { // 私钥解密 // private key Decrypt data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") + var priKeyXML []byte = []byte("...") var deData string = obj. FromBase64String(enData). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromXMLPrivateKey([]byte(priKeyXML)). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). + // FromXMLPrivateKey(priKeyXML). Decrypt(). // SetOAEPHash("SHA256"). // OAEP 可选 // SetOAEPLabel("test-label"). // OAEP 可选 @@ -189,32 +193,34 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 私钥加密 // private key Decrypt data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") + var priKeyXML []byte = []byte("...") var enData string = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromXMLPrivateKey([]byte(priKeyXML)). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). + // FromXMLPrivateKey(priKeyXML). PrivateKeyEncrypt(). ToBase64String() // 公钥解密 // public key Encrypt data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") + var pubKeyXML []byte = []byte("...") var deData string = obj. FromBase64String(enData). - FromPublicKey([]byte(pubKeyPem)). - // FromPKCS1PublicKey([]byte(pubKeyPem)). - // FromPKCS8PublicKey([]byte(pubKeyPem)). - // FromXMLPublicKey([]byte(pubKeyXML)). + FromPublicKey(pubKeyPem). + // FromPKCS1PublicKey(pubKeyPem). + // FromPKCS8PublicKey(pubKeyPem). + // FromXMLPublicKey(pubKeyXML). PublicKeyDecrypt(). ToString() } @@ -223,23 +229,28 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var prikeyPem string = "..." - var pubkeyPem string = "..." + var prikeyPem []byte = []byte("...") + var pubkeyPem []byte = []byte("...") + + var priKeyXML []byte = []byte("...") + var pubKeyXML []byte = []byte("...") // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var res bool = rsa.New(). - // FromPrivateKey([]byte(prikeyPem)). - // FromPrivateKeyWithPassword([]byte(prikeyPem), psssword). - // FromPKCS1PrivateKey([]byte(prikeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(prikeyPem), psssword). - FromPKCS8PrivateKey([]byte(prikeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(prikeyPem), psssword). - // FromPublicKey([]byte(pubkeyPem)). - // FromPKCS1PublicKey([]byte(pubkeyPem)). - FromPKCS8PublicKey([]byte(pubkeyPem)). + // FromPrivateKey(prikeyPem). + // FromPrivateKeyWithPassword(prikeyPem, password). + // FromPKCS1PrivateKey(prikeyPem). + // FromPKCS1PrivateKeyWithPassword(prikeyPem, password). + FromPKCS8PrivateKey(prikeyPem). + // FromPKCS8PrivateKeyWithPassword(prikeyPem, password). + // FromXMLPrivateKey(priKeyXML). + // FromPublicKey(pubkeyPem). + // FromPKCS1PublicKey(pubkeyPem). + FromPKCS8PublicKey(pubkeyPem). + // FromXMLPublicKey(pubKeyXML). CheckKeyPair() } ~~~ @@ -258,15 +269,15 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var parsedPrivateKey *go_rsa.PrivateKey = rsa.New(). FromPrivateKey(priKeyPem). - // FromPrivateKeyWithPassword(priKeyPem, psssword). + // FromPrivateKeyWithPassword(priKeyPem, password). // FromPKCS1PrivateKey(priKeyPem). - // FromPKCS1PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). // FromPKCS8PrivateKey(priKeyPem). - // FromPKCS8PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). // FromXMLPrivateKey(priKeyXML). GetPrivateKey() @@ -294,22 +305,22 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var newPrivateKey string = rsa.New(). // FromPrivateKey(priKeyPem). - // FromPrivateKeyWithPassword(priKeyPem, psssword). + // FromPrivateKeyWithPassword(priKeyPem, password). // FromPKCS1PrivateKey(priKeyPem). - FromPKCS1PrivateKeyWithPassword(priKeyPem, psssword). + FromPKCS1PrivateKeyWithPassword(priKeyPem, password). // FromPKCS8PrivateKey(priKeyPem). - // FromPKCS8PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). // FromXMLPrivateKey(priKeyXML). // CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey(). - // CreatePKCS1PrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC"). CreatePKCS8PrivateKey(). // 转为 PKCS8 编码 - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). // CreateXMLPrivateKey(). ToKeyString() diff --git a/pkg/lakego-pkg/go-cryptobin/docs/sm2.md b/pkg/lakego-pkg/go-cryptobin/docs/sm2.md index 9b3225e6..95ccb004 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/sm2.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/sm2.md @@ -35,24 +35,24 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 生成私钥 // create private key var PriKeyPem string = obj. CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey(). - // CreatePKCS1PrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS8PrivateKey(). - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). ToKeyString() // 自定义私钥加密类型 // use custom encrypt options var PriKeyPem string = obj. - CreatePKCS8PrivateKeyWithPassword(psssword, sm2.Opts{ + CreatePKCS8PrivateKeyWithPassword(password, sm2.Opts{ Cipher: sm2.GetCipherFromName("AES256CBC"), KDFOpts: sm2.ScryptOpts{ CostParameter: 1 << 15, @@ -84,33 +84,33 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 设置 UID 值 // set uid data var uid []byte = []byte("") - + // 设置 hash // set hash func - var hash = md5.New + var md5Hash = md5.New obj := sm2.New() // 私钥签名 // private key sign data // 比如: SM2withSM3 => ... SetSignHash("SM3").Sign() ... - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") sigBase64String = obj. FromString(data). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). // WithUID(uid). // SetSignHash("SM3"). - // WithSignHash(hash). + // WithSignHash(md5Hash). Sign(). // SignASN1(). // SignBytes(). @@ -118,13 +118,13 @@ func main() { // 公钥验证 // public key verify signed data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") var res bool = obj. FromBase64String(sigBase64String). - FromPublicKey([]byte(pubKeyPem)). + FromPublicKey(pubKeyPem). // WithUID(uid). // SetSignHash("SM3"). - // WithSignHash(hash). + // WithSignHash(md5Hash). Verify([]byte(data)). // VerifyASN1([]byte(data)). // VerifyBytes([]byte(data)). @@ -143,14 +143,14 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" // 公钥加密 // public key Encrypt data - var pubKeyPem string = "" + var pubKeyPem []byte = []byte("...") var enData string = obj. FromString(data). - FromPublicKey([]byte(pubKeyPem)). + FromPublicKey(pubKeyPem). // SetMode 为可选,默认为 C1C3C2 // SetMode("C1C3C2"). // C1C3C2 | C1C2C3 Encrypt(). @@ -158,15 +158,15 @@ func main() { // 私钥解密 // private key Decrypt data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") var deData string = obj. FromBase64String(enData). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). // SetMode 为可选,默认为 C1C3C2 // SetMode("C1C3C2"). // C1C3C2 | C1C2C3 Decrypt(). @@ -174,23 +174,23 @@ func main() { } ~~~ -#### SM2 获取 x, y, d 16进制数据 / get x, y, d data +#### SM2 获取 x, y, d 16进制(Hex)数据 / get x, y, d hex data ~~~go func main() { obj := sm2.New() // 获取私钥明文 D // get private key D data - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") d := sm2. - FromPrivateKey([]byte(priKeyPem)). + FromPrivateKey(priKeyPem). GetPrivateKeyDString() // 获取公钥 X, Y 明文数据, 从私钥 // get public key x data and y data from private key - var priKeyPem string = "" + var priKeyPem []byte = []byte("...") public := sm2. - FromPrivateKey([]byte(priKeyPem)). + FromPrivateKey(priKeyPem). MakePublicKey() x := public.GetPublicKeyXString() @@ -198,8 +198,8 @@ func main() { // 获取公钥 X, Y 明文数据, 从公钥 // get public key x data and y data from public key - var pubKeyPem string = "" - public := sm2.FromPublicKey([]byte(pubKeyPem)) + var pubKeyPem []byte = []byte("...") + public := sm2.FromPublicKey(pubKeyPem) x := public.GetPublicKeyXString() y := public.GetPublicKeyYString() @@ -228,17 +228,17 @@ func main() { #### 检测私钥公钥是否匹配 / Check KeyPair ~~~go func main() { - var priKeyPem string = "..." - var pubKeyPem string = "..." + var priKeyPem []byte = []byte("...") + var pubKeyPem []byte = []byte("...") var res bool = sm2.New(). - FromPrivateKey([]byte(priKeyPem)). - // FromPrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS1PrivateKey([]byte(priKeyPem)). - // FromPKCS1PrivateKeyWithPassword([]byte(priKeyPem), psssword). - // FromPKCS8PrivateKey([]byte(priKeyPem)). - // FromPKCS8PrivateKeyWithPassword([]byte(priKeyPem), psssword). - FromPublicKey([]byte(pubKeyPem)). + FromPrivateKey(priKeyPem). + // FromPrivateKeyWithPassword(priKeyPem, password). + // FromPKCS1PrivateKey(priKeyPem). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). + // FromPKCS8PrivateKey(priKeyPem). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). + FromPublicKey(pubKeyPem). CheckKeyPair() } ~~~ @@ -257,15 +257,15 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var parsedPrivateKey *gmsm2.PrivateKey = sm2.New(). FromPrivateKey(priKeyPem). - // FromPrivateKeyWithPassword(priKeyPem, psssword). + // FromPrivateKeyWithPassword(priKeyPem, password). // FromPKCS1PrivateKey(priKeyPem). - // FromPKCS1PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS1PrivateKeyWithPassword(priKeyPem, password). // FromPKCS8PrivateKey(priKeyPem). - // FromPKCS8PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). GetPrivateKey() // 公钥解析 @@ -278,7 +278,7 @@ func main() { } ~~~ -#### 私钥证书编码格式转换 / Change PrivateKey type +#### 私钥证书格式编码转换 / Change PrivateKey type ~~~go import ( "github.com/deatil/go-cryptobin/cryptobin/sm2" @@ -291,21 +291,21 @@ func main() { // 私钥密码 // privatekey password - var psssword string = "" + var password string = "" var newPrivateKey string = sm2.New(). // FromPrivateKey(priKeyPem). - // FromPrivateKeyWithPassword(priKeyPem, psssword). + // FromPrivateKeyWithPassword(priKeyPem, password). // FromPKCS1PrivateKey(priKeyPem). - FromPKCS1PrivateKeyWithPassword(priKeyPem, psssword). // PKCS1 有密码证书 + FromPKCS1PrivateKeyWithPassword(priKeyPem, password). // PKCS1 有密码证书 // FromPKCS8PrivateKey(priKeyPem). - // FromPKCS8PrivateKeyWithPassword(priKeyPem, psssword). + // FromPKCS8PrivateKeyWithPassword(priKeyPem, password). // CreatePrivateKey(). - // CreatePrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePrivateKeyWithPassword(password, "AES256CBC"). // CreatePKCS1PrivateKey(). - // CreatePKCS1PrivateKeyWithPassword(psssword, "AES256CBC"). + // CreatePKCS1PrivateKeyWithPassword(password, "AES256CBC"). CreatePKCS8PrivateKey(). // 转为 PKCS8 编码 - // CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256"). + // CreatePKCS8PrivateKeyWithPassword(password, "AES256CBC", "SHA256"). ToKeyString() } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/docs/ssh.md b/pkg/lakego-pkg/go-cryptobin/docs/ssh.md index f9d79fa2..aa6a32b3 100644 --- a/pkg/lakego-pkg/go-cryptobin/docs/ssh.md +++ b/pkg/lakego-pkg/go-cryptobin/docs/ssh.md @@ -1,6 +1,6 @@ -### ssh 使用文档 +### SSH 使用文档 -#### ssh rsa生成使用 +#### SSH RSA 生成使用 ~~~go package main @@ -19,8 +19,8 @@ func main() { rsa := cryptobin_rsa.NewRsa().GenerateKey(2048) - // block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKey(obj.GetPrivateKey(), "comment") - // block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKeyWithPassword(obj.GetPrivateKey(), "comment", []byte("123")) + // block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKey(rsa.GetPrivateKey(), "comment") + // block, _ := cryptobin_ssh.MarshalOpenSSHPrivateKeyWithPassword(rsa.GetPrivateKey(), "comment", []byte("123")) rsaBlock, _ := cryptobin_ssh.MarshalOpenSSHPrivateKeyWithPassword( rsa.GetPrivateKey(), "comment", @@ -36,14 +36,14 @@ func main() { ) rsaBlockkeyData := pem.EncodeToMemory(rsaBlock) - fs.Put("./runtime/key/ssh/rsa-en", string(rsaBlockkeyData)) + fs.Put("./ssh-key/rsa-en", string(rsaBlockkeyData)) fmt.Println("生成成功") } ~~~ -#### ssh rsa解析使用 +#### SSH RSA 解析使用 ~~~go package main @@ -62,8 +62,9 @@ func main() { fs := filesystem.New() // ssh - sshRsaEn, _ := fs.Get("./runtime/key/ssh/rsa-en") + sshRsaEn, _ := fs.Get("./ssh-key/rsa-en") sshRsaEnBlock, _ := pem.Decode([]byte(sshRsaEn)) + // sshRsaKey, comment, err := cryptobin_ssh.ParseOpenSSHPrivateKey(sshRsaEnBlock.Bytes) sshRsaKey, comment, err := cryptobin_ssh.ParseOpenSSHPrivateKeyWithPassword(sshRsaEnBlock.Bytes, []byte("123")) @@ -73,18 +74,18 @@ func main() { CreatePKCS8PrivateKey(). ToKeyString() - fs.Put("./runtime/key/ssh/rsa-key-pkcs8", sshRsaPriKey) + fs.Put("./ssh-key/rsa-key-pkcs8", sshRsaPriKey) + + fmt.Println("解析 key 成功") + fmt.Println("证书 comment 为: " + comment) } else { - fs.Put("./runtime/key/ssh/rsa-key-pkcs8", err.Error()) + fmt.Println("解析 key 失败,错误信息为: " + err.Error()) } - - fmt.Println("解析 key 成功") - fmt.Println("证书 comment 为: " + comment) } ~~~ -#### ssh 生成公钥 +#### SSH 生成公钥 ~~~go package main @@ -103,16 +104,17 @@ func main() { sshRsaPubKey := cryptobin_rsa.New(). GenerateKey(). GetPublicKey() + sshPubKey, _ := ssh.NewPublicKey(sshRsaPubKey) sshAuthorizedKey := ssh.MarshalAuthorizedKeyWithComment(sshPubKey, "abc@email.com") - fs.Put("./runtime/key/ssh/pub/rsa.pub", string(sshAuthorizedKey)) + fs.Put("./ssh-key/pub/rsa.pub", string(sshAuthorizedKey)) fmt.Println("生成公钥成功") } ~~~ -#### ssh 解析 +#### SSH 解析 ~~~go package main @@ -128,18 +130,19 @@ import ( func main() { fs := filesystem.New() - sshPri1, _ := fs.Get("./runtime/key/ssh/pub/dsa.pub") - sshPubKey, sshcomment, sshoptions, sshrest, ssherr := ssh.ParseAuthorizedKey([]byte(sshPri1)) + sshPri1, _ := fs.Get("./ssh-key/pub/dsa.pub") + + sshPubKey, sshcomment, sshoptions, sshrest, err := ssh.ParseAuthorizedKey([]byte(sshPri1)) var sshAuthorizedKey []byte - if ssherr == nil { + if err == nil { sshAuthorizedKey = ssh.MarshalAuthorizedKeyWithComment(sshPubKey, "abc@qq.com") - fs.Put("./runtime/key/ssh/pub/dsa_2.pub", string(sshAuthorizedKey)) + fs.Put("./ssh-key/pub/dsa_2.pub", string(sshAuthorizedKey)) } - fmt.Println("解析公钥成功") + fmt.Println("解析公钥成功") } ~~~ diff --git a/pkg/lakego-pkg/go-cryptobin/mac/ansi_retail_mac.go b/pkg/lakego-pkg/go-cryptobin/mac/ansi_retail_mac.go index 00101697..1a0b6541 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/ansi_retail_mac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/ansi_retail_mac.go @@ -52,6 +52,7 @@ func (e *ansiRetailMAC) MAC(src []byte) []byte { for len(src) > 0 { subtle.XORBytes(tag, tag, src[:blockSize]) e.b1.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/cbc_mac.go b/pkg/lakego-pkg/go-cryptobin/mac/cbc_mac.go index f62eea09..e558d9a9 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/cbc_mac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/cbc_mac.go @@ -43,6 +43,7 @@ func (c *cbcmac) MAC(src []byte) []byte { for len(src) > 0 { subtle.XORBytes(tag, tag, src[:blockSize]) c.b.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/cbcr_mac.go b/pkg/lakego-pkg/go-cryptobin/mac/cbcr_mac.go index 13ce56a4..b14640f4 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/cbcr_mac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/cbcr_mac.go @@ -48,6 +48,7 @@ func (c *cbcrMAC) MAC(src []byte) []byte { for len(src) > blockSize { subtle.XORBytes(tag, tag, src[:blockSize]) c.b.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/cmac.go b/pkg/lakego-pkg/go-cryptobin/mac/cmac.go index 6c58bf03..d6b7746b 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/cmac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/cmac.go @@ -67,6 +67,7 @@ func (c *cmac) MAC(src []byte) []byte { } c.b.Encrypt(tag, tag) + src = src[blockSize:] } @@ -75,6 +76,7 @@ func (c *cmac) MAC(src []byte) []byte { subtle.XORBytes(tag, src, tag) subtle.XORBytes(tag, c.k2, tag) tag[len(src)] ^= 0b10000000 + c.b.Encrypt(tag, tag) } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/emac.go b/pkg/lakego-pkg/go-cryptobin/mac/emac.go index e02808cd..1c205b8d 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/emac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/emac.go @@ -53,6 +53,7 @@ func (e *emac) MAC(src []byte) []byte { for len(src) > 0 { subtle.XORBytes(tag, tag, src[:blockSize]) e.b1.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/mac_des.go b/pkg/lakego-pkg/go-cryptobin/mac/mac_des.go index 0ab1e6ee..c2a5fc28 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/mac_des.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/mac_des.go @@ -70,6 +70,7 @@ func (m *macDES) MAC(src []byte) []byte { for len(src) > 0 { subtle.XORBytes(tag, tag, src[:blockSize]) m.b1.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mac/tr_cbc_mac.go b/pkg/lakego-pkg/go-cryptobin/mac/tr_cbc_mac.go index 5fee4a32..759e5c31 100644 --- a/pkg/lakego-pkg/go-cryptobin/mac/tr_cbc_mac.go +++ b/pkg/lakego-pkg/go-cryptobin/mac/tr_cbc_mac.go @@ -46,6 +46,7 @@ func (t *trCBCMAC) MAC(src []byte) []byte { for len(src) > 0 { subtle.XORBytes(tag, tag, src[:blockSize]) t.b.Encrypt(tag, tag) + src = src[blockSize:] } diff --git a/pkg/lakego-pkg/go-cryptobin/mode/ccm/ccm.go b/pkg/lakego-pkg/go-cryptobin/mode/ccm/ccm.go index 7f68adb6..9c88ff29 100644 --- a/pkg/lakego-pkg/go-cryptobin/mode/ccm/ccm.go +++ b/pkg/lakego-pkg/go-cryptobin/mode/ccm/ccm.go @@ -76,7 +76,11 @@ func NewCCMWithNonceAndTagSize(cipher go_cipher.Block, nonceSize, tagSize int) ( return nil, errors.New("cipher: NewCCM requires 128-bit block cipher") } - c := &ccm{cipher: cipher, nonceSize: nonceSize, tagSize: tagSize} + c := &ccm{ + cipher: cipher, + nonceSize: nonceSize, + tagSize: tagSize, + } return c, nil } diff --git a/pkg/lakego-pkg/go-cryptobin/padding/iso10126.go b/pkg/lakego-pkg/go-cryptobin/padding/iso10126.go index ff339d04..49e9982e 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/iso10126.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/iso10126.go @@ -21,7 +21,7 @@ func NewISO10126() ISO10126 { // 填充至符合块大小的整数倍,填充值最后一个字节为填充的数量数,其他字节填充随机字节。 func (this ISO10126) Padding(text []byte, blockSize int) []byte { n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } @@ -38,7 +38,7 @@ func (this ISO10126) Padding(text []byte, blockSize int) []byte { func (this ISO10126) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } num := n - int(src[n-1]) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/iso7816_4.go b/pkg/lakego-pkg/go-cryptobin/padding/iso7816_4.go index 5ec5147d..6ab8f78f 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/iso7816_4.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/iso7816_4.go @@ -21,18 +21,17 @@ func NewISO7816_4() ISO7816_4 { // ISO7816_4Padding // 填充至符合块大小的整数倍,填充值第一个字节为0x80,其他字节填0x00。 func (this ISO7816_4) Padding(text []byte, blockSize int) []byte { - n := len(text) - if n == 0 || blockSize < 1 { + num := len(text) + if blockSize < 1 { return text } // 补位 blockSize 值 - paddingSize := blockSize - n%blockSize + overhead := blockSize - num%blockSize + paddingText := bytes.Repeat([]byte{0}, overhead) - text = append(text, 0x80) - - paddingText := bytes.Repeat([]byte{0x00}, paddingSize - 1) text = append(text, paddingText...) + text[num] = 0x80 return text } @@ -40,7 +39,7 @@ func (this ISO7816_4) Padding(text []byte, blockSize int) []byte { func (this ISO7816_4) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } num := bytes.LastIndexByte(src, 0x80) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/iso97971.go b/pkg/lakego-pkg/go-cryptobin/padding/iso97971.go index 4b1e5ad9..5acde528 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/iso97971.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/iso97971.go @@ -3,8 +3,6 @@ package padding import ( "bytes" "errors" - - "github.com/deatil/go-cryptobin/tool/alias" ) /** @@ -23,21 +21,24 @@ func NewISO97971() ISO97971 { // ISO/IEC 9797-1 Padding Method 2 // 填充至符合块大小的整数倍,填充值第一个字节为0x80,其他字节填0x00。 func (this ISO97971) Padding(text []byte, blockSize int) []byte { - overhead := blockSize - len(text)%blockSize - ret, out := alias.SliceForAppend(text, overhead) - - out[0] = 0x80 - for i := 1; i < overhead; i++ { - out[i] = 0 + num := len(text) + if blockSize < 1 { + return text } - return ret + overhead := blockSize - num%blockSize + paddingText := bytes.Repeat([]byte{0}, overhead) + + text = append(text, paddingText...) + text[num] = 0x80 + + return text } func (this ISO97971) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } num := bytes.LastIndexByte(src, 0x80) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/padding.go b/pkg/lakego-pkg/go-cryptobin/padding/padding.go index 04918823..d6e06a53 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/padding.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/padding.go @@ -1,10 +1,26 @@ package padding +import ( + "math/rand" +) + // padding interface struct type Padding interface { - // Padding + // Padding func Padding(text []byte, blockSize int) []byte - // UnPadding + // UnPadding func UnPadding(src []byte) ([]byte, error) } + +// 随机字节 +func randomBytes(length uint) []byte { + charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" + + b := make([]byte, length) + for i := range b { + b[i] = charset[rand.Int63()%int64(len(charset))] + } + + return b +} diff --git a/pkg/lakego-pkg/go-cryptobin/padding/padding_test.go b/pkg/lakego-pkg/go-cryptobin/padding/padding_test.go index d0976a2c..27d4b793 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/padding_test.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/padding_test.go @@ -1,7 +1,6 @@ package padding import ( - // "fmt" "testing" "reflect" @@ -276,6 +275,7 @@ func Test_ISO97971_Pad(t *testing.T) { {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, } for _, tt := range tests { @@ -312,6 +312,8 @@ func Test_ISO97971_Unpad(t *testing.T) { {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"1 bytes with tag", []byte{0x80}, []byte{0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, {"3 bytes with tag", []byte{0x80, 0, 0x80}, []byte{0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, {"19 bytes with tag", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, @@ -327,7 +329,7 @@ func Test_ISO97971_Unpad(t *testing.T) { } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("case %v: ISO97971.UnPadding() = %v, want %v", tt.name, got, tt.want) + t.Errorf("case %v: ISO97971.UnPadding() = %#v, want %#v", tt.name, got, tt.want) } }) } @@ -357,6 +359,7 @@ func Test_X923_Pad(t *testing.T) { {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13}}, {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14}}, {"1 bytes", []byte{0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}}, + {"0 bytes", []byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -391,7 +394,9 @@ func Test_X923_Unpad(t *testing.T) { {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12}, false}, {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13}, false}, {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14}, false}, - {"1 bytes", []byte{0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}, false}, + {"1 bytes", []byte{2}, []byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}, false}, + {"0 bytes", []byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16}, false}, + // {"invalid src length", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}, true}, {"invalid padding length", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17}, true}, {"invalid padding bytes", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 15}, true}, @@ -405,7 +410,481 @@ func Test_X923_Unpad(t *testing.T) { } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("ansiX923Padding.UnPadding() = %v, want %v", got, tt.want) + t.Errorf("ansiX923Padding.UnPadding() = %#v, want %#v", got, tt.want) + } + }) + } +} + +func Test_ISO7816_4_Pad(t *testing.T) { + iso9797 := NewISO7816_4() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0x80}}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0x80, 0}}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0x80, 0, 0}}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x80, 0, 0, 0}}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0x80, 0, 0, 0, 0}}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x80, 0, 0, 0, 0, 0}}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0x80, 0, 0, 0, 0, 0, 0}}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0x80, 0, 0, 0, 0, 0, 0, 0}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0x80, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := iso9797.Padding(tt.src, 16); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ISO97971.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_ISO7816_4_Unpad(t *testing.T) { + iso9797 := NewISO7816_4() + + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0x80}, false}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0x80, 0}, false}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0x80, 0, 0}, false}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x80, 0, 0, 0}, false}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0x80, 0, 0, 0, 0}, false}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x80, 0, 0, 0, 0, 0}, false}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0x80, 0, 0, 0, 0, 0, 0}, false}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0x80, 0, 0, 0, 0, 0, 0, 0}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0x80, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + + {"1 bytes with tag", []byte{0x80}, []byte{0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes with tag", []byte{0x80, 0, 0x80}, []byte{0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"19 bytes with tag", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"invalid src length", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := iso9797.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("case %v: ISO97971.UnPadding() error = %v, wantErr %v", tt.name, err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("case %v: ISO97971.UnPadding() = %#v, want %#v", tt.name, got, tt.want) + } + }) + } +} + +func Test_PKCS7_Pad(t *testing.T) { + pkcs7 := NewPKCS7() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1}}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 2, 2}}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 3, 3}}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 4, 4, 4, 4}}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 5, 5, 5, 5}}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 6, 6, 6, 6, 6}}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 7, 7, 7, 7, 7, 7}}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14}}, + {"1 bytes", []byte{0}, []byte{0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}}, + {"0 bytes", []byte{}, []byte{16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := pkcs7.Padding(tt.src, 16); !reflect.DeepEqual(got, tt.want) { + t.Errorf("PKCS7.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_PKCS7_Unpad(t *testing.T) { + pkcs7 := NewPKCS7() + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}, false}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1}, false}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 2, 2}, false}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 3, 3}, false}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 4, 4, 4, 4}, false}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 5, 5, 5, 5}, false}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 6, 6, 6, 6, 6}, false}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 7, 7, 7, 7, 7, 7}, false}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14}, false}, + {"1 bytes", []byte{0}, []byte{0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}, false}, + {"0 bytes", []byte{}, []byte{16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}, false}, + + {"invalid src length", nil, []byte{0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}, true}, + {"invalid padding byte", nil, []byte{0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17}, true}, + {"inconsistent padding bytes", nil, []byte{0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 14, 15}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := pkcs7.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("PKCS7.UnPadding() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("PKCS7.UnPadding() = %#v, want %#v", got, tt.want) + } + }) + } +} + +func Test_PKCS5_Pad(t *testing.T) { + pkcs5 := NewPKCS5() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 1}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 2, 2}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 3, 3, 3}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 4, 4, 4, 4}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 5, 5, 5, 5, 5}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 6, 6, 6, 6, 6, 6}}, + {"1 bytes", []byte{0}, []byte{0, 7, 7, 7, 7, 7, 7, 7}}, + {"0 bytes", []byte{}, []byte{8, 8, 8, 8, 8, 8, 8, 8}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := pkcs5.Padding(tt.src, 8); !reflect.DeepEqual(got, tt.want) { + t.Errorf("PKCS5.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_PKCS5_Unpad(t *testing.T) { + pkcs5 := NewPKCS5() + + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 1}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 2, 2}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 3, 3, 3}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 4, 4, 4, 4}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 5, 5, 5, 5, 5}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 6, 6, 6, 6, 6, 6}, false}, + {"1 bytes", []byte{0}, []byte{0, 7, 7, 7, 7, 7, 7, 7}, false}, + {"0 bytes", []byte{}, []byte{8, 8, 8, 8, 8, 8, 8, 8}, false}, + + {"invalid src length", nil, []byte{0, 7, 7, 7, 7, 7, 7}, true}, + {"invalid padding byte", nil, []byte{0, 7, 7, 7, 7, 7, 7, 8}, true}, + {"inconsistent padding bytes", nil, []byte{0, 7, 7, 7, 7, 7, 6, 7}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := pkcs5.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("PKCS5.UnPadding() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("PKCS5.UnPadding() = %#v, want %#v", got, tt.want) + } + }) + } +} + +func Test_Zero_Pad(t *testing.T) { + zero := NewZero() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0}}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 0}}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0}}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0}}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0}}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0}}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0}}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"1 bytes", []byte{0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"0 bytes", []byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := zero.Padding(tt.src, 16); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Zero.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_Zero_Unpad(t *testing.T) { + zero := NewZero() + + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0}, false}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 0}, false}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0}, false}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0}, false}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0}, false}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0}, false}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0}, false}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"1 bytes", []byte{1}, []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"0 bytes", []byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := zero.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("Zero.UnPadding() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Zero.UnPadding() = %#v, want %#v", got, tt.want) + } + }) + } +} + +func Test_PBOC2_Pad(t *testing.T) { + pboc2 := NewPBOC2() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0x80}}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0x80, 0}}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0x80, 0, 0}}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x80, 0, 0, 0}}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0x80, 0, 0, 0, 0}}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x80, 0, 0, 0, 0, 0}}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0x80, 0, 0, 0, 0, 0, 0}}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0x80, 0, 0, 0, 0, 0, 0, 0}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0x80, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := pboc2.Padding(tt.src, 16); !reflect.DeepEqual(got, tt.want) { + t.Errorf("PBOC2.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_PBOC2_Unpad(t *testing.T) { + pboc2 := NewPBOC2() + + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0x80}, false}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0x80, 0}, false}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0x80, 0, 0}, false}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x80, 0, 0, 0}, false}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0x80, 0, 0, 0, 0}, false}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x80, 0, 0, 0, 0, 0}, false}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0x80, 0, 0, 0, 0, 0, 0}, false}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0x80, 0, 0, 0, 0, 0, 0, 0}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0x80, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"1 bytes", []byte{0}, []byte{0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"0 bytes", []byte{}, []byte{0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + + {"1 bytes with tag", []byte{0x80}, []byte{0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes with tag", []byte{0x80, 0, 0x80}, []byte{0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"19 bytes with tag", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x80, 0, 0x80, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"invalid src length", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := pboc2.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("case %v: PBOC2.UnPadding() error = %v, wantErr %v", tt.name, err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("case %v: PBOC2.UnPadding() = %#v, want %#v", tt.name, got, tt.want) + } + }) + } +} + +func Test_TBC_Pad(t *testing.T) { + tbc := NewTBC() + + tests := []struct { + name string + src []byte + want []byte + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0xFF}}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 0}}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0xFF, 0xFF, 0xFF}}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0}}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0}}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {"1 bytes", []byte{0}, []byte{0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + {"0 bytes", []byte{}, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tbc.Padding(tt.src, 16); !reflect.DeepEqual(got, tt.want) { + t.Errorf("TBC.Padding() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_TBC_Unpad(t *testing.T) { + tbc := NewTBC() + + tests := []struct { + name string + want []byte + src []byte + wantErr bool + }{ + {"16 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"15 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0xFF}, false}, + {"14 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 0}, false}, + {"13 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0xFF, 0xFF, 0xFF}, false}, + {"12 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0}, false}, + {"11 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"10 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0}, false}, + {"9 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"8 bytes", []byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"7 bytes", []byte{0, 1, 2, 3, 4, 5, 6}, []byte{0, 1, 2, 3, 4, 5, 6, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"6 bytes", []byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"5 bytes", []byte{0, 1, 2, 3, 4}, []byte{0, 1, 2, 3, 4, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"4 bytes", []byte{0, 1, 2, 3}, []byte{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"3 bytes", []byte{0, 1, 2}, []byte{0, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"2 bytes", []byte{0, 1}, []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, + {"1 bytes", []byte{0}, []byte{0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + {"0 bytes", []byte{}, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, false}, + + // {"invalid src length", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true}, + {"invalid padding byte", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, true}, + {"inconsistent padding bytes", nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 55, 0xFF}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tbc.UnPadding(tt.src) + if (err != nil) != tt.wantErr { + t.Errorf("TBC.UnPadding() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("TBC.UnPadding() = %#v, want %#v", got, tt.want) } }) } diff --git a/pkg/lakego-pkg/go-cryptobin/padding/pboc2.go b/pkg/lakego-pkg/go-cryptobin/padding/pboc2.go index 87b56fe7..65bc2c85 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/pboc2.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/pboc2.go @@ -3,8 +3,6 @@ package padding import ( "bytes" "errors" - - "github.com/deatil/go-cryptobin/tool/alias" ) /** @@ -22,23 +20,26 @@ func NewPBOC2() PBOC2 { // PBOC2.0的MAC运算数据填充规范 // 若原加密数据的最末字节可能是0x80,则不推荐使用该模式 -// 与 ISO97971Padding(text, blockSize) 一致 +// 与 ISO97971Padding 一致 func (this PBOC2) Padding(text []byte, blockSize int) []byte { - overhead := blockSize - len(text)%blockSize - ret, out := alias.SliceForAppend(text, overhead) - - out[0] = 0x80 - for i := 1; i < overhead; i++ { - out[i] = 0 + num := len(text) + if blockSize < 1 { + return text } - return ret + overhead := blockSize - num%blockSize + paddingText := bytes.Repeat([]byte{0}, overhead) + + text = append(text, paddingText...) + text[num] = 0x80 + + return text } func (this PBOC2) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } num := bytes.LastIndexByte(src, 0x80) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/pkcs1.go b/pkg/lakego-pkg/go-cryptobin/padding/pkcs1.go index 03f3fa8e..8984362f 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/pkcs1.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/pkcs1.go @@ -33,7 +33,7 @@ func NewPKCS1(bt string) PKCS1 { // BT = 02时,随机填充,但不能为00。 func (this PKCS1) Padding(text []byte, blockSize int) []byte { n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } @@ -79,7 +79,7 @@ func (this PKCS1) Padding(text []byte, blockSize int) []byte { func (this PKCS1) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } count := int(src[n-1]) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/pkcs5.go b/pkg/lakego-pkg/go-cryptobin/padding/pkcs5.go index e2f5bced..cce0ae6b 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/pkcs5.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/pkcs5.go @@ -24,7 +24,7 @@ func (this PKCS5) Padding(text []byte, _ int) []byte { blockSize := 8 n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } @@ -39,13 +39,13 @@ func (this PKCS5) Padding(text []byte, _ int) []byte { func (this PKCS5) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } unpadding := int(src[n-1]) num := n - unpadding - if num < 0 { + if num < 0 || unpadding > 8 { return nil, errors.New("invalid padding") } diff --git a/pkg/lakego-pkg/go-cryptobin/padding/pkcs7.go b/pkg/lakego-pkg/go-cryptobin/padding/pkcs7.go index 0314811d..fca8bc69 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/pkcs7.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/pkcs7.go @@ -22,7 +22,7 @@ func NewPKCS7() PKCS7 { // 填充至符合块大小的整数倍,填充值为填充数量数 func (this PKCS7) Padding(text []byte, blockSize int) []byte { n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } @@ -37,7 +37,7 @@ func (this PKCS7) Padding(text []byte, blockSize int) []byte { func (this PKCS7) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } unpadding := int(src[n-1]) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/tbc.go b/pkg/lakego-pkg/go-cryptobin/padding/tbc.go index 944d8665..f8aa2f8e 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/tbc.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/tbc.go @@ -22,18 +22,22 @@ func NewTBC() TBC { // 填充至符合块大小的整数倍,原文最后一位为1时填充0x00,最后一位为0时填充0xFF。 func (this TBC) Padding(text []byte, blockSize int) []byte { n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } // 补位 blockSize 值 paddingSize := blockSize - n%blockSize - lastBit := text[n - 1] & 0x1 - var paddingByte byte - if lastBit != 0 { - paddingByte = 0x00 + if n > 0 { + lastBit := text[n - 1] & 0x1 + + if lastBit != 0 { + paddingByte = 0x00 + } else { + paddingByte = 0xFF + } } else { paddingByte = 0xFF } @@ -47,25 +51,47 @@ func (this TBC) Padding(text []byte, blockSize int) []byte { func (this TBC) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } + res := []byte{} + lastByte := src[n-1] - switch { - case lastByte == 0x00: + switch lastByte { + case 0x00: for i := n - 2; i >= 0; i-- { if src[i] != 0x00 { - return src[:i+1], nil + res = src[:i+1] + break } } - case lastByte == 0xFF: + + if len(res) > 0 { + lastBit := res[len(res) - 1] & 0x1 + if lastBit == 0 { + return nil, errors.New("invalid padding") + } + } else { + return nil, errors.New("invalid padding") + } + case 0xFF: for i := n - 2; i >= 0; i-- { if src[i] != 0xFF { - return src[:i+1], nil + res = src[:i+1] + break + } + } + + if len(res) > 0 { + lastBit := res[len(res) - 1] & 0x1 + if lastBit != 0 { + return nil, errors.New("invalid padding") } } + default: + return nil, errors.New("invalid padding") } - return nil, errors.New("invalid padding") + return res, nil } diff --git a/pkg/lakego-pkg/go-cryptobin/padding/utils.go b/pkg/lakego-pkg/go-cryptobin/padding/utils.go deleted file mode 100644 index faf89248..00000000 --- a/pkg/lakego-pkg/go-cryptobin/padding/utils.go +++ /dev/null @@ -1,17 +0,0 @@ -package padding - -import ( - "math/rand" -) - -// 随机字节 -func randomBytes(length uint) []byte { - charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" - - b := make([]byte, length) - for i := range b { - b[i] = charset[rand.Int63()%int64(len(charset))] - } - - return b -} diff --git a/pkg/lakego-pkg/go-cryptobin/padding/x923.go b/pkg/lakego-pkg/go-cryptobin/padding/x923.go index 00065142..d0390cab 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/x923.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/x923.go @@ -1,9 +1,8 @@ package padding import ( + "bytes" "errors" - - "github.com/deatil/go-cryptobin/tool/alias" ) /** @@ -15,6 +14,7 @@ import ( type X923 struct {} // 构造函数 +// ansiX923 // https://www.ibm.com/docs/en/linux-on-systems?topic=processes-ansi-x923-cipher-block-chaining func NewX923() X923 { return X923{} @@ -23,21 +23,24 @@ func NewX923() X923 { // X923Padding / ansiX923Padding // 填充至符合块大小的整数倍,填充值最后一个字节为填充的数量数,其他字节填0 func (this X923) Padding(text []byte, blockSize int) []byte { - overhead := blockSize - len(text)%blockSize - ret, out := alias.SliceForAppend(text, overhead) - - out[overhead-1] = byte(overhead) - for i := 0; i < overhead-1; i++ { - out[i] = 0 + num := len(text) + if blockSize < 1 { + return text } - return ret + overhead := blockSize - num%blockSize + paddingText := bytes.Repeat([]byte{0}, overhead) + + text = append(text, paddingText...) + text[len(text)-1] = byte(overhead) + + return text } func (this X923) UnPadding(src []byte) ([]byte, error) { n := len(src) if n == 0 { - return nil, errors.New("invalid data len") + return nil, errors.New("invalid data length") } unpadding := int(src[n-1]) diff --git a/pkg/lakego-pkg/go-cryptobin/padding/zero.go b/pkg/lakego-pkg/go-cryptobin/padding/zero.go index 5b7a8e5b..2c35813f 100644 --- a/pkg/lakego-pkg/go-cryptobin/padding/zero.go +++ b/pkg/lakego-pkg/go-cryptobin/padding/zero.go @@ -20,7 +20,7 @@ func NewZero() Zero { // 数据长度不对齐时使用0填充,否则不填充 func (this Zero) Padding(text []byte, blockSize int) []byte { n := len(text) - if n == 0 || blockSize < 1 { + if blockSize < 1 { return text } diff --git a/pkg/lakego-pkg/go-cryptobin/pubkey/bip0340/batch.go b/pkg/lakego-pkg/go-cryptobin/pubkey/bip0340/batch.go index 87f6c7f4..30691025 100644 --- a/pkg/lakego-pkg/go-cryptobin/pubkey/bip0340/batch.go +++ b/pkg/lakego-pkg/go-cryptobin/pubkey/bip0340/batch.go @@ -12,7 +12,7 @@ func BatchVerify(pub []*PublicKey, m, sig [][]byte, hashFunc Hasher) bool { pub0 := pub[0] - for i := 0; i < u; i++ { + for i := 1; i < u; i++ { if pub[i].Curve != pub0.Curve { return false } diff --git a/pkg/lakego-pkg/go-filesystem/README.md b/pkg/lakego-pkg/go-filesystem/README.md index 22fd2557..7ab2acef 100644 --- a/pkg/lakego-pkg/go-filesystem/README.md +++ b/pkg/lakego-pkg/go-filesystem/README.md @@ -38,7 +38,7 @@ func main() { // 写入数据 path := "/path.txt" - contents := []byte("") + contents := []byte("testdata") ok, err := fs.Write(path, contents) if err != nil { @@ -52,49 +52,49 @@ func main() { ~~~go // 写入 -fs.Write(path, contents string) (bool, error) +Write(path, contents []byte) (bool, error) // 写入数据流 -fs.WriteStream(path string, resource io.Reader) (bool, error) +WriteStream(path string, resource io.Reader) (bool, error) // 添加数据 -fs.Put(path, contents string) (bool, error) +Put(path, contents []byte) (bool, error) // 添加数据流 -fs.PutStream(path string, resource io.Reader) (bool, error) +PutStream(path string, resource io.Reader) (bool, error) // 读取后删除 -fs.ReadAndDelete(path string) (any, error) +ReadAndDelete(path string) (any, error) // 更新 -fs.Update(path, contents string) (bool, error) +Update(path, contents []byte) (bool, error) // 读取 -fs.Read(path string) (string, error) +Read(path string) ([]byte, error) // 重命名 -fs.Rename(path, newpath string) (bool, error) +Rename(path, newpath string) (bool, error) // 复制 -fs.Copy(path, newpath string) (bool, error) +Copy(path, newpath string) (bool, error) // 删除 -fs.Delete(path string) (bool, error) +Delete(path string) (bool, error) // 删除文件夹 -fs.DeleteDir(dirname string) (bool, error) +DeleteDir(dirname string) (bool, error) // 创建文件夹 -fs.CreateDir(dirname string) (bool, error) +CreateDir(dirname string) (bool, error) // 列出内容 -fs.ListContents(dirname string) ([]map[string]any, error) +ListContents(dirname string) ([]map[string]any, error) ~~~ ### 开源协议 -* `go-filesystem` 文件管理器 遵循 `Apache2` 开源协议发布,在保留本软件版权的情况下提供个人及商业免费使用。 +* `go-filesystem` 遵循 `Apache2` 开源协议发布,在保留本软件版权的情况下提供个人及商业免费使用。 ### 版权 diff --git a/pkg/lakego-pkg/go-filesystem/filesystem/filesystem.go b/pkg/lakego-pkg/go-filesystem/filesystem/filesystem.go index 153c8b7c..c0a9fe83 100644 --- a/pkg/lakego-pkg/go-filesystem/filesystem/filesystem.go +++ b/pkg/lakego-pkg/go-filesystem/filesystem/filesystem.go @@ -179,7 +179,7 @@ func (this *Filesystem) PutStream(path string, resource io.Reader, conf ...map[s // 读取并删除 // read and delete -func (this *Filesystem) ReadAndDelete(path string) (any, error) { +func (this *Filesystem) ReadAndDelete(path string) ([]byte, error) { path = util.NormalizePath(path) contents, err := this.Read(path) diff --git a/pkg/lakego-pkg/go-filesystem/filesystem/filesystem_test.go b/pkg/lakego-pkg/go-filesystem/filesystem/filesystem_test.go index 8374a017..24d2bbbb 100644 --- a/pkg/lakego-pkg/go-filesystem/filesystem/filesystem_test.go +++ b/pkg/lakego-pkg/go-filesystem/filesystem/filesystem_test.go @@ -468,3 +468,49 @@ func Test_CreateDir(t *testing.T) { res33 := fs.Has("/testdir") assertEqual(res33, false, "Test_CreateDir Delete after Has") } + +func Test_HasDir(t *testing.T) { + assertEqual := assertEqualT(t) + + // 根目录 + root := "./testdata" + adapter := local_adapter.New(root) + + fs := filesystem.New(adapter) + + res := fs.Has("/testdir222") + assertEqual(res, true, "Test_HasDir") + + res2 := fs.Has("/testdir333") + assertEqual(res2, false, "Test_HasDir 2") +} + +func Test_ReadAndDelete(t *testing.T) { + assertEqual := assertEqualT(t) + + // 根目录 + root := "./testdata" + adapter := local_adapter.New(root) + + fs := filesystem.New(adapter) + + res, err := fs.Copy("/testcopy.txt", "/testReadAndDelete.txt") + if err != nil { + t.Fatal(err.Error()) + } + + assertEqual(res, true, "Test_ReadAndDelete") + + res2 := fs.Has("/testReadAndDelete.txt") + assertEqual(res2, true, "Test_ReadAndDelete Has") + + res3, err := fs.ReadAndDelete("/testReadAndDelete.txt") + if err != nil { + t.Fatal(err.Error()) + } + + assertEqual(string(res3), "testdata", "Test_ReadAndDelete ReadAndDelete") + + res33 := fs.Has("/testReadAndDelete.txt") + assertEqual(res33, false, "Test_ReadAndDelete ReadAndDelete after Has") +} diff --git a/pkg/lakego-pkg/go-filesystem/filesystem/testdata/testdir222/test.txt b/pkg/lakego-pkg/go-filesystem/filesystem/testdata/testdir222/test.txt new file mode 100644 index 00000000..830f5817 --- /dev/null +++ b/pkg/lakego-pkg/go-filesystem/filesystem/testdata/testdir222/test.txt @@ -0,0 +1 @@ +testdata \ No newline at end of file