Documentation
¶
Overview ¶
Package cryptor implements some util functions to encrypt and decrypt. Contain base64, hmac, sha, aes, des, and rsa
Package cryptor implements some util functions to encrypt and decrypt. Note: 1. for aes crypt function, the `key` param length should be 16, 24 or 32. if not, will panic.
Index ¶
- func AesCbcDecrypt(encrypted, key []byte) []byte
- func AesCbcEncrypt(data, key []byte) []byte
- func AesCfbDecrypt(encrypted, key []byte) []byte
- func AesCfbEncrypt(data, key []byte) []byte
- func AesCtrCrypt(data, key []byte) []byte
- func AesEcbDecrypt(encrypted, key []byte) []byte
- func AesEcbEncrypt(data, key []byte) []byte
- func AesOfbDecrypt(data, key []byte) []byte
- func AesOfbEncrypt(data, key []byte) []byte
- func Base64StdDecode(s string) string
- func Base64StdEncode(s string) string
- func DesCbcDecrypt(encrypted, key []byte) []byte
- func DesCbcEncrypt(data, key []byte) []byte
- func DesCfbDecrypt(encrypted, key []byte) []byte
- func DesCfbEncrypt(data, key []byte) []byte
- func DesCtrCrypt(data, key []byte) []byte
- func DesEcbDecrypt(encrypted, key []byte) []byte
- func DesEcbEncrypt(data, key []byte) []byte
- func DesOfbDecrypt(data, key []byte) []byte
- func DesOfbEncrypt(data, key []byte) []byte
- func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error
- func HmacMd5(str, key string) string
- func HmacMd5WithBase64(data, key string) string
- func HmacSha1(str, key string) string
- func HmacSha1WithBase64(str, key string) string
- func HmacSha256(str, key string) string
- func HmacSha256WithBase64(str, key string) string
- func HmacSha512(str, key string) string
- func HmacSha512WithBase64(str, key string) string
- func Md5Byte(data []byte) string
- func Md5ByteWithBase64(data []byte) string
- func Md5File(filename string) (string, error)
- func Md5String(s string) string
- func Md5StringWithBase64(s string) string
- func RsaDecrypt(data []byte, privateKeyFileName string) []byte
- func RsaEncrypt(data []byte, pubKeyFileName string) []byte
- func Sha1(str string) string
- func Sha1WithBase64(str string) string
- func Sha256(str string) string
- func Sha256WithBase64(str string) string
- func Sha512(str string) string
- func Sha512WithBase64(str string) string
Examples ¶
- AesCbcDecrypt
- AesCbcEncrypt
- AesCfbDecrypt
- AesCfbEncrypt
- AesCtrCrypt
- AesEcbDecrypt
- AesEcbEncrypt
- AesOfbDecrypt
- AesOfbEncrypt
- Base64StdDecode
- Base64StdEncode
- DesCbcDecrypt
- DesCbcEncrypt
- DesCfbDecrypt
- DesCfbEncrypt
- DesCtrCrypt
- DesEcbDecrypt
- DesEcbEncrypt
- DesOfbDecrypt
- DesOfbEncrypt
- GenerateRsaKey
- HmacMd5
- HmacMd5WithBase64
- HmacSha1
- HmacSha1WithBase64
- HmacSha256
- HmacSha256WithBase64
- HmacSha512
- HmacSha512WithBase64
- Md5Byte
- Md5ByteWithBase64
- Md5String
- Md5StringWithBase64
- RsaDecrypt
- RsaEncrypt
- Sha1
- Sha1WithBase64
- Sha256
- Sha256WithBase64
- Sha512
- Sha512WithBase64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AesCbcDecrypt ¶
AesCbcDecrypt decrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/IOq_g8_lKZD
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesCbcEncrypt([]byte(data), []byte(key)) decrypted := AesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesCbcEncrypt ¶
AesCbcEncrypt encrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/IOq_g8_lKZD
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesCbcEncrypt([]byte(data), []byte(key)) decrypted := AesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesCfbDecrypt ¶
AesCfbDecrypt decrypt data with key use AES CFB algorithm len(encrypted) should be great than 16, len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/tfkF10B13kH
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesCfbEncrypt([]byte(data), []byte(key)) decrypted := AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesCfbEncrypt ¶
AesCfbEncrypt encrypt data with key use AES CFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/tfkF10B13kH
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesCfbEncrypt([]byte(data), []byte(key)) decrypted := AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesCtrCrypt ¶
AesCtrCrypt encrypt data with key use AES CTR algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/SpaZO0-5Nsp
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesCtrCrypt([]byte(data), []byte(key)) decrypted := AesCtrCrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesEcbDecrypt ¶
AesEcbDecrypt decrypt data with key use AES ECB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/jT5irszHx-j
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesEcbEncrypt([]byte(data), []byte(key)) decrypted := AesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesEcbEncrypt ¶
AesEcbEncrypt encrypt data with key use AES ECB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/jT5irszHx-j
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesEcbEncrypt([]byte(data), []byte(key)) decrypted := AesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesOfbDecrypt ¶
AesOfbDecrypt decrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/VtHxtkUj-3F
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesOfbEncrypt([]byte(data), []byte(key)) decrypted := AesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func AesOfbEncrypt ¶
AesOfbEncrypt encrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/VtHxtkUj-3F
Example ¶
data := "hello" key := "abcdefghijklmnop" encrypted := AesOfbEncrypt([]byte(data), []byte(key)) decrypted := AesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func Base64StdDecode ¶
Base64StdDecode decode a base64 encoded string. Play: https://go.dev/play/p/RWQylnJVgIe
Example ¶
str := Base64StdDecode("aGVsbG8=") fmt.Println(str)
Output: hello
func Base64StdEncode ¶
Base64StdEncode encode string with base64 encoding. Play: https://go.dev/play/p/VOaUyQUreoK
Example ¶
base64Str := Base64StdEncode("hello") fmt.Println(base64Str)
Output: aGVsbG8=
func DesCbcDecrypt ¶
DesCbcDecrypt decrypt data with key use DES CBC algorithm len(key) should be 8. Play: https://go.dev/play/p/4cC4QvWfe3_1
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesCbcEncrypt([]byte(data), []byte(key)) decrypted := DesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesCbcEncrypt ¶
DesCbcEncrypt encrypt data with key use DES CBC algorithm len(key) should be 8. Play: https://go.dev/play/p/4cC4QvWfe3_1
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesCbcEncrypt([]byte(data), []byte(key)) decrypted := DesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesCfbDecrypt ¶
DesCfbDecrypt decrypt data with key use DES CFB algorithm len(encrypted) should be great than 16, len(key) should be 8. Play: https://go.dev/play/p/y-eNxcFBlxL
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesCfbEncrypt([]byte(data), []byte(key)) decrypted := DesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesCfbEncrypt ¶
DesCfbEncrypt encrypt data with key use DES CFB algorithm len(key) should be 8. Play: https://go.dev/play/p/y-eNxcFBlxL
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesCfbEncrypt([]byte(data), []byte(key)) decrypted := DesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesCtrCrypt ¶
DesCtrCrypt encrypt data with key use DES CTR algorithm len(key) should be 8. Play: https://go.dev/play/p/9-T6OjKpcdw
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesCtrCrypt([]byte(data), []byte(key)) decrypted := DesCtrCrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesEcbDecrypt ¶
DesEcbDecrypt decrypt data with key use DES ECB algorithm len(key) should be 8. Play: https://go.dev/play/p/8qivmPeZy4P
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesEcbEncrypt([]byte(data), []byte(key)) decrypted := DesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesEcbEncrypt ¶
DesEcbEncrypt encrypt data with key use DES ECB algorithm len(key) should be 8. Play: https://go.dev/play/p/8qivmPeZy4P
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesEcbEncrypt([]byte(data), []byte(key)) decrypted := DesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesOfbDecrypt ¶
DesOfbDecrypt decrypt data with key use DES OFB algorithm len(key) should be 8. Play: https://go.dev/play/p/74KmNadjN1J
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesOfbEncrypt([]byte(data), []byte(key)) decrypted := DesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func DesOfbEncrypt ¶
DesOfbEncrypt encrypt data with key use DES OFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/74KmNadjN1J
Example ¶
data := "hello" key := "abcdefgh" encrypted := DesOfbEncrypt([]byte(data), []byte(key)) decrypted := DesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted))
Output: hello
func GenerateRsaKey ¶
GenerateRsaKey create rsa private and public pemo file. Play: https://go.dev/play/p/zutRHrDqs0X
Example ¶
// Create ras private and public pem file err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { return } fmt.Println("foo")
Output: foo
func HmacMd5 ¶
HmacMd5 return the hmac hash of string use md5. Play: https://go.dev/play/p/uef0q1fz53I
Example ¶
str := "hello" key := "12345" hms := HmacMd5(str, key) fmt.Println(hms)
Output: e834306eab892d872525d4918a7a639a
func HmacMd5WithBase64 ¶ added in v2.2.4
HmacMd5WithBase64 return the hmac hash of string use md5 with base64. todo
Example ¶
str := "hello" key := "12345" hms := HmacMd5WithBase64(str, key) fmt.Println(hms)
Output: 6DQwbquJLYclJdSRinpjmg==
func HmacSha1 ¶
HmacSha1 return the hmac hash of string use sha1. Play: https://go.dev/play/p/1UI4oQ4WXKM
Example ¶
str := "hello" key := "12345" hms := HmacSha1(str, key) fmt.Println(hms)
Output: 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
func HmacSha1WithBase64 ¶ added in v2.2.4
HmacSha1WithBase64 return the hmac hash of string use sha1 with base64. Play: https://go.dev/play/p/47JmmGrnF7B
Example ¶
str := "hello" key := "12345" hms := HmacSha1WithBase64(str, key) fmt.Println(hms)
Output: XGqdsMzLkuNu0DI/0Jt/k23prOA=
func HmacSha256 ¶
HmacSha256 return the hmac hash of string use sha256. Play: https://go.dev/play/p/HhpwXxFhhC0
Example ¶
str := "hello" key := "12345" hms := HmacSha256(str, key) fmt.Println(hms)
Output: 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
func HmacSha256WithBase64 ¶ added in v2.2.4
HmacSha256WithBase64 return the hmac hash of string use sha256 with base64. Play: https://go.dev/play/p/EKbkUvPTLwO
Example ¶
str := "hello" key := "12345" hms := HmacSha256WithBase64(str, key) fmt.Println(hms)
Output: MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
func HmacSha512 ¶
HmacSha512 return the hmac hash of string use sha512. Play: https://go.dev/play/p/59Od6m4A0Ud
Example ¶
str := "hello" key := "12345" hms := HmacSha512(str, key) fmt.Println(hms)
Output: dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
func HmacSha512WithBase64 ¶ added in v2.2.4
HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. Play: https://go.dev/play/p/61wBBOKO-GH
Example ¶
str := "hello" key := "12345" hms := HmacSha512WithBase64(str, key) fmt.Println(hms)
Output: 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
func Md5Byte ¶ added in v2.2.3
Md5Byte return the md5 string of byte slice. Play: https://go.dev/play/p/suraalH8lyC
Example ¶
md5Str := Md5Byte([]byte{'a'}) fmt.Println(md5Str)
Output: 0cc175b9c0f1b6a831c399e269772661
func Md5ByteWithBase64 ¶ added in v2.2.4
Md5ByteWithBase64 return the md5 string of byte slice with base64. Play: https://go.dev/play/p/CkN9hYKGeAy
Example ¶
md5Str := Md5ByteWithBase64([]byte("hello")) fmt.Println(md5Str)
Output: XUFAKrxLKna5cZ2REBfFkg==
func Md5String ¶
Md5String return the md5 value of string. Play: https://go.dev/play/p/1bLcVetbTOI
Example ¶
md5Str := Md5String("hello") fmt.Println(md5Str)
Output: 5d41402abc4b2a76b9719d911017c592
func Md5StringWithBase64 ¶ added in v2.2.4
Md5StringWithBase64 return the md5 value of string with base64. Play: https://go.dev/play/p/Lx4gH7Vdr5_y
Example ¶
md5Str := Md5StringWithBase64("hello") fmt.Println(md5Str)
Output: XUFAKrxLKna5cZ2REBfFkg==
func RsaDecrypt ¶
RsaDecrypt decrypt data with ras algorithm. Play: https://go.dev/play/p/rDqTT01SPkZ
Example ¶
// Create ras private and public pem file err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { return } data := []byte("hello") encrypted := RsaEncrypt(data, "rsa_public.pem") decrypted := RsaDecrypt(encrypted, "rsa_private.pem") fmt.Println(string(decrypted))
Output: hello
func RsaEncrypt ¶
RsaEncrypt encrypt data with ras algorithm. Play: https://go.dev/play/p/rDqTT01SPkZ
Example ¶
// Create ras private and public pem file err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { return } data := []byte("hello") encrypted := RsaEncrypt(data, "rsa_public.pem") decrypted := RsaDecrypt(encrypted, "rsa_private.pem") fmt.Println(string(decrypted))
Output: hello
func Sha1 ¶
Sha1 return the sha1 value (SHA-1 hash algorithm) of string. Play: https://go.dev/play/p/_m_uoD1deMT
Example ¶
result := Sha1("hello") fmt.Println(result)
Output: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
func Sha1WithBase64 ¶ added in v2.2.4
Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string. Play: todo
Example ¶
result := Sha1WithBase64("hello") fmt.Println(result)
Output: qvTGHdzF6KLavt4PO0gs2a6pQ00=
func Sha256 ¶
Sha256 return the sha256 value (SHA256 hash algorithm) of string. Play: https://go.dev/play/p/tU9tfBMIAr1
Example ¶
result := Sha256("hello") fmt.Println(result)
Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
func Sha256WithBase64 ¶ added in v2.2.4
Sha256WithBase64 return the sha256 value (SHA256 hash algorithm) of base64 string. Play: https://go.dev/play/p/85IXJHIal1k
Example ¶
result := Sha256WithBase64("hello") fmt.Println(result)
Output: LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
func Sha512 ¶
Sha512 return the sha512 value (SHA512 hash algorithm) of string. Play: https://go.dev/play/p/3WsvLYZxsHa
Example ¶
result := Sha512("hello") fmt.Println(result)
Output: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
func Sha512WithBase64 ¶ added in v2.2.4
Sha512WithBase64 return the sha512 value (SHA512 hash algorithm) of base64 string. Play: https://go.dev/play/p/q_fY2rA-k5I
Example ¶
result := Sha512WithBase64("hello") fmt.Println(result)
Output: m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
Types ¶
This section is empty.