based on GoRSA, GoCrypt Optimized the following points for packaging:
- Optimization of public and private keys requires registration and initialization in advance.
- The encryption machine does not perform base64 processing, and avoids secondary encapsulation of base64 during cross-program transfer or storage
- The incoming return uses the string type uniformly to avoid conversion trouble
- Supports RSAWithSHA1 and RSAWithSHA256 signature verification algorithms
此版本新增了 AES、DES、3DES、HMAC、HASH 等常见加/解密及 hash 获取方式,结构作了些调整,由之前的版本更新到此版本时一定要注意引用方法包名的变化。AES/DES 块加密时,填充默认采用 PKCS7 padding( 如果块大小为 64 位(8 字节)时,此时 PKCS7 与 PKCS5 结果一致).
go get -u -v github.com/houseme/gocrypto
handleRSA := rsa.NewRSACrypt(secretInfo) // RSA
handleDES := des.NewDESCrypt(key) // des
handle3DES := des.NewTripleDESCrypt(key) // 3des
handleAES := aes.NewAESCrypt(key) // aes
handleHash := hash.NewHash(hashType) // common hash
handleHMAC := hash.NewHMAC(hashType, key) // hmac
加密指定字符串,并以指定编码格式输出结果
encrypt, err := handle.Encrypt(src) // des/3des/aes
解密指定格式编码后的加密串,返回原字符串
decrypt, err := handle.Decrypt(src) // des/3des/aes
哈希运算/密钥相关的哈希运算。
handle.Get([]byte("123456"))// 输出 []byte
handle.EncodeToString([]byte("123456"), gocrypto.HEX) // 输出为 hex 格式的字符串
secretInfo := rsa.SecretInfo{
PublicKey: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyoiAraTnAbCoqGVOKugFDM2/ms2szXmb3zTOU3ByicH/XPZqy7Eougbs8OQQIoNW4xKw8PNyWf0lfr90qBfPj27INn6N7umVmbHCNCKkQ4frPn46xesw1ywtc2GhOEzZlC8ajlnzBUkj5FJZcrNjXfFmfsQcFQP0g/o/3CAUpk1BXFXt7eZsaYdyn0m7fMoyFt1wlF8egQeGYYE98vtKsvrII51HK8vOEf+5VXU4UZxGfvyzS3A8kuNEkKEh1n9mazjfPBT0KGSiOGh7Nugks+jjfswSgXRK/b2eP3fS7U625rbS798pKxnoS2E0Pgp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oJQGxCdzbZMLxtTbyTZe3+iAVG++8u6NWMV3dUCgYA1dm1rnQto321kGy+6Z/2OMXTNBeufGwDDDfilzZdTkNwASMhEAW7trLuXcV8bahcsymMUTUevQawOFBnYupq/lAEluSOtq5vZBAF+huAdLJptFiJT6rKFkM5j+z2jW3DJnyMz6UmXT7GTDTVqCWoaBqIFfbsY60NjXlK92YhJzQKBgQDWfQjktbSHasLw9RV0oPRklD+cBhfBgfOpZ+0En3CxR+j+MxhW1gSBQwZS5wxTIGXrEeHlo4UmUe5diExE0dRsi+ToVPM1qw6P1SuwbQd3tXSNmu0NyOWCnfblm/j4YNLFB1p9IK9s5dLRQKJxpG/ribw15FuK6n2QM5vOyIPIvQKBgE5PUzRUCCVsjKAxZOfaZQatMbSzAUSB3bNmUw+F3pDq8ibs6XXvtySowG2femlPDNL7mDMuUc9kYrtTFTQNrEsQGB55wBopX3UxzRjpXJoAQ/d+RPdrSJC7xJyu+URoFI6ae0I3bx1BzjctYU0Rv5DUh+j9leMH5N2S9vHb+vqu",
PrivateKeyType: gocrypto.PKCS1,
PrivateKeyDataType: gocrypto.Base64,
HashType: gocrypto.SHA256,
}
handle := rsa.NewRSACrypt(secretInfo)
加密指定字符串,并以指定编码格式输出结果
encrypt, err := handle.Encrypt("test", gocrypto.HEX)
if err != nil {
fmt.Println("encrypt error :", err)
return
}
fmt.Println("encrypt data :", encrypt)
解密指定格式编码后的加密串,返回原字符串
decrypt, err := handle.Decrypt(encrypt, gocrypto.HEX)
if err != nil {
fmt.Println("decrypt error :", err)
return
}
fmt.Println("decrypt data :", decrypt)
以指定摘要算法签名,并以指定编码格式输出结果,仅适用于 RSA。
sign, err := handle.Sign("test", gocrypto.HEX)
if err != nil {
fmt.Println("sign error :", err)
return
}
fmt.Println("sign data :", sign)
验证字符串是否是以指定摘要算法编码的签名串的原始字符串,仅适用于 RSA。
verifySign, err := handle.VerifySign("test", sign, gocrypto.HEX)
if err != nil {
fmt.Println("verifySign error :", err)
return
}
fmt.Println("verifySign result :", verifySign)
加密指定字符串,并以指定编码格式输出结果
encrypt, err := handle.EncryptByPriKey("test", gocrypto.HEX)
if err != nil {
fmt.Println("encrypt error :", err)
return
}
fmt.Println("encrypt data :", encrypt)
解密指定格式编码后的加密串,返回原字符串
decrypt, err := handle.DecryptByPublic(encrypt, gocrypto.HEX)
if err != nil {
fmt.Println("decrypt error :", err)
return
}
fmt.Println("decrypt data :", decrypt)
Licensed under either of Apache License, Version 2.0, LICENSE at your option.