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 AesCbcDecryptBase64(encrypted, key string) string
- func AesCbcEncrypt(data, key []byte) []byte
- func AesCbcEncryptBase64(data, key string) string
- func AesCfbDecrypt(encrypted, key []byte) []byte
- func AesCfbDecryptBase64(encrypted, key string) string
- func AesCfbEncrypt(data, key []byte) []byte
- func AesCfbEncryptBase64(data, key string) string
- 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 AesOfbDecryptBase64(encrypted, key string) string
- func AesOfbEncrypt(data, key []byte) []byte
- func AesOfbEncryptBase64(data, key string) string
- 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(data, key string) string
- func HmacSha1(data, key string) string
- func HmacSha256(data, key string) string
- func HmacSha512(data, key string) string
- func Md5File(filename string) (string, error)
- func Md5String(s string) string
- func RsaDecrypt(data []byte, privateKeyFileName string) []byte
- func RsaEncrypt(data []byte, pubKeyFileName string) []byte
- func Sha1(data string) string
- func Sha256(data string) string
- func Sha512(data 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
- HmacSha1
- HmacSha256
- HmacSha512
- Md5String
- RsaDecrypt
- RsaEncrypt
- Sha1
- Sha256
- Sha512
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 AesCbcDecryptBase64 ¶
AesCbcDecryptBase64 decrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32.
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 AesCbcEncryptBase64 ¶
AesCbcEncryptBase64 decrypt data with key use AES CBC base64 algorithm len(key) should be 16, 24 or 32.
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 AesCfbDecryptBase64 ¶
AesCfbDecryptBase64 decrypt data with key use AES CFB algorithm len(encrypted) should be great than 16, len(key) should be 16, 24 or 32.
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 AesCfbEncryptBase64 ¶
AesCfbEncryptBase64 encrypt data with key use AES CFB algorithm len(key) should be 16, 24 or 32.
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 AesOfbDecryptBase64 ¶
AesOfbDecryptBase64 decrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32.
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 AesOfbEncryptBase64 ¶
AesOfbEncryptBase64 encrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32.
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 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 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 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 Md5String ¶
Md5String return the md5 value of string. Play: https://go.dev/play/p/1bLcVetbTOI
Example ¶
str := "hello" md5Str := Md5String(str) fmt.Println(md5Str)
Output: 5d41402abc4b2a76b9719d911017c592
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 ¶
str := "hello" result := Sha1(str) fmt.Println(result)
Output: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
func Sha256 ¶
Sha256 return the sha256 value (SHA256 hash algorithm) of string. Play: https://go.dev/play/p/tU9tfBMIAr1
Example ¶
str := "hello" result := Sha256(str) fmt.Println(result)
Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
func Sha512 ¶
Sha512 return the sha512 value (SHA512 hash algorithm) of string. Play: https://go.dev/play/p/3WsvLYZxsHa
Example ¶
str := "hello" result := Sha512(str) fmt.Println(result)
Output: 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package morse implements morse encoding, fork from https://github.com/martinlindhe/morse
|
Package morse implements morse encoding, fork from https://github.com/martinlindhe/morse |
Package openssl for key certificate management
|
Package openssl for key certificate management |
from https://github.com/hashicorp/vault/blob/v1.13.1/shamir
|
from https://github.com/hashicorp/vault/blob/v1.13.1/shamir |