# Cryptor Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa, sm2, sm3, sm4.
## Source: - [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go)
## Usage: ```go import ( "github.com/duke-git/lancet/v2/cryptor" ) ```
## Index - [AesEcbEncrypt](#AesEcbEncrypt) - [AesEcbDecrypt](#AesEcbDecrypt) - [AesCbcEncrypt](#AesCbcEncrypt) - [AesCbcDecrypt](#AesCbcDecrypt) - [AesCtrCryptdeprecated](#AesCtrCrypt) - [AesCtrEncrypt](#AesCtrEncrypt) - [AesCtrDecrypt](#AesCtrDecrypt) - [AesCfbEncrypt](#AesCfbEncrypt) - [AesCfbDecrypt](#AesCfbDecrypt) - [AesOfbEncrypt](#AesOfbEncrypt) - [AesOfbDecrypt](#AesOfbDecrypt) - [AesGcmEncrypt](#AesGcmEncrypt) - [AesGcmDecrypt](#AesGcmDecrypt) - [Base64StdEncode](#Base64StdEncode) - [Base64StdDecode](#Base64StdDecode) - [DesEcbEncrypt](#DesEcbEncrypt) - [DesEcbDecrypt](#DesEcbDecrypt) - [DesCbcEncrypt](#DesCbcEncrypt) - [DesCbcDecrypt](#DesCbcDecrypt) - [DesCtrCryptdeprecated](#DesCtrCrypt) - [DesCfbEncrypt](#DesCfbEncrypt) - [DesCfbDecrypt](#DesCfbDecrypt) - [DesCfbEncrypt](#DesCfbEncrypt) - [DesCfbDecrypt](#DesCfbDecrypt) - [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbDecrypt](#DesOfbDecrypt) - [HmacMd5](#HmacMd5) - [HmacMd5WithBase64](#HmacMd5WithBase64) - [HmacSha1](#HmacSha1) - [HmacSha1WithBase64](#HmacSha1WithBase64) - [HmacSha256](#HmacSha256) - [HmacSha256WithBase64](#HmacSha256WithBase64) - [HmacSha512](#HmacSha512) - [HmacSha512WithBase64](#HmacSha512WithBase64) - [Md5String](#Md5String) - [Md5StringWithBase64](#Md5StringWithBase64) - [Md5Byte](#Md5Byte) - [Md5ByteWithBase64](#Md5ByteWithBase64) - [Md5File](#Md5File) - [Sha1](#Sha1) - [Sha1WithBase64](#Sha1WithBase64) - [Sha256](#Sha256) - [Sha256WithBase64](#Sha256WithBase64) - [Sha512](#Sha512) - [Sha512WithBase64](#Sha512WithBase64) - [GenerateRsaKey](#GenerateRsaKey) - [RsaEncrypt](#RsaEncrypt) - [RsaDecrypt](#RsaDecrypt) - [GenerateRsaKeyPair](#GenerateRsaKeyPair) - [RsaEncryptOAEP](#RsaEncryptOAEP) - [RsaDecryptOAEP](#RsaDecryptOAEP) - [RsaSign](#RsaSign) - [RsaVerifySign](#RsaVerifySign) - [Sm3](#Sm3) - [Sm4EcbEncrypt](#Sm4EcbEncrypt) - [Sm4EcbDecrypt](#Sm4EcbDecrypt) - [Sm4CbcEncrypt](#Sm4CbcEncrypt) - [Sm4CbcDecrypt](#Sm4CbcDecrypt) - [GenerateSm2Key](#GenerateSm2Key) - [Sm2Encrypt](#Sm2Encrypt) - [Sm2Decrypt](#Sm2Decrypt)
## Documentation ### AesEcbEncrypt

Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesEcbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/zI6xsmuQRbn) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesEcbDecrypt

Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesEcbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/zI6xsmuQRbn) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCbcEncrypt

Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesCbcEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/IOq_g8_lKZD) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCbcDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesCbcDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/IOq_g8_lKZD) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCtrCrypt

Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.

> ⚠️ This function is deprecated. use `AesCtrEncrypt` and `AesCtrDecrypt` instead. Signature: ```go func AesCtrCrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/SpaZO0-5Nsp) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCtrEncrypt

Encrypt data with key use AES CTR algorithm

Signature: ```go func AesCtrEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/x6pjPAvThRz) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCtrEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCtrDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCtrDecrypt

Decrypt data with key use AES CTR algorithm

Signature: ```go func AesCtrDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/x6pjPAvThRz) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCtrEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCtrDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCfbEncrypt

Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesCfbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/tfkF10B13kH) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesCfbDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesCfbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/tfkF10B13kH) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesOfbEncrypt

Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesOfbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/VtHxtkUj-3F) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesOfbDecrypt

Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature: ```go func AesOfbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/VtHxtkUj-3F) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesGcmEncrypt

Encrypt data with key use AES GCM algorithm.

Signature: ```go func AesGcmEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/rUt0-DmsPCs) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### AesGcmDecrypt

Decrypt data with key use AES GCM algorithm.

Signature: ```go func AesGcmDecrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/rUt0-DmsPCs) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefghijklmnop" encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### Base64StdEncode

Encode string with base64 encoding.

Signature: ```go func Base64StdEncode(s string) string ``` Example:[Run](https://go.dev/play/p/VOaUyQUreoK) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { base64Str := cryptor.Base64StdEncode("hello") fmt.Println(base64Str) // Output: // aGVsbG8= } ``` ### Base64StdDecode

Decode a base64 encoded string.

Signature: ```go func Base64StdDecode(s string) string ``` Example:[Run](https://go.dev/play/p/RWQylnJVgIe) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := cryptor.Base64StdDecode("aGVsbG8=") fmt.Println(str) // Output: // hello } ``` ### DesEcbEncrypt

Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

Signature: ```go func DesEcbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/8qivmPeZy4P) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesEcbDecrypt

Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

Signature: ```go func DesEcbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/8qivmPeZy4P) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCbcEncrypt

Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature: ```go func DesCbcEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/4cC4QvWfe3_1) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCbcDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature: ```go func DesCbcDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/4cC4QvWfe3_1) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCtrCrypt

Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.

> ⚠️ This function is deprecated. use `DesCtrEncrypt` and `DesCtrDecrypt` instead. Signature: ```go func DesCtrCrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/9-T6OjKpcdw) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCtrCrypt

Encrypt data with key use DES CTR algorithm. Length of `key` param should be 8.

Signature: ```go func DesCtrEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/S6p_WHCgH1d) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCtrEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCtrDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCtrDecrypt

Decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.

Signature: ```go func DesCtrDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/S6p_WHCgH1d) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCtrEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCtrDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCfbEncrypt

Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.

Signature: ```go func DesCfbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/y-eNxcFBlxL) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesCfbDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature: ```go func DesCfbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/y-eNxcFBlxL) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesOfbEncrypt

Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

Signature: ```go func DesOfbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/74KmNadjN1J) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### DesOfbDecrypt

Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

Signature: ```go func DesOfbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/74KmNadjN1J) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := "hello" key := "abcdefgh" encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) fmt.Println(string(decrypted)) // Output: // hello } ``` ### HmacMd5

Get the md5 hmac hash of string.

Signature: ```go func HmacMd5(str, key string) string ``` Example:[Run](https://go.dev/play/p/uef0q1fz53I) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacMd5(str, key) fmt.Println(hms) // Output: // e834306eab892d872525d4918a7a639a } ``` ### HmacMd5WithBase64

Get the md5 hmac hash of base64 string.

Signature: ```go func HmacMd5WithBase64(str, key string) string ``` Example:[Run](https://go.dev/play/p/UY0ng2AefFC) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacMd5WithBase64(str, key) fmt.Println(hms) // Output: // 6DQwbquJLYclJdSRinpjmg== } ``` ### HmacSha1

Get the sha1 hmac hash of string.

Signature: ```go func HmacSha1(str, key string) string ``` Example:[Run](https://go.dev/play/p/1UI4oQ4WXKM) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha1(str, key) fmt.Println(hms) // Output: // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } ``` ### HmacSha1WithBase64

Return the hmac hash of string use sha1 with base64.

Signature: ```go func HmacSha1WithBase64(str, key string) string ``` Example:[Run](https://go.dev/play/p/47JmmGrnF7B) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha1WithBase64(str, key) fmt.Println(hms) // Output: // XGqdsMzLkuNu0DI/0Jt/k23prOA= } ``` ### HmacSha256

Get the sha256 hmac hash of string

Signature: ```go func HmacSha256(str, key string) string ``` Example:[Run](https://go.dev/play/p/HhpwXxFhhC0) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha256(str, key) fmt.Println(hms) // Output: // 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75 } ``` ### HmacSha256WithBase64

Return the hmac hash of string use sha256 with base64.

Signature: ```go func HmacSha256WithBase64(str, key string) string ``` Example:[Run](https://go.dev/play/p/EKbkUvPTLwO) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha256WithBase64(str, key) fmt.Println(hms) // Output: // MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU= } ``` ### HmacSha512

Get the sha512 hmac hash of string.

Signature: ```go func HmacSha512(str, key string) string ``` Example:[Run](https://go.dev/play/p/59Od6m4A0Ud) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha512(str, key) fmt.Println(hms) // Output: // dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc } ``` ### HmacSha512WithBase64

Return the hmac hash of string use sha512 with base64.

Signature: ```go func HmacSha512WithBase64(str, key string) string ``` Example:[Run](https://go.dev/play/p/c6dSe3E2ydU) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" key := "12345" hms := cryptor.HmacSha512WithBase64(str, key) fmt.Println(hms) // Output: // 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A== } ``` ### Md5String

Get the md5 value of string.

Signature: ```go func Md5String(s string) string ``` Example:[Run](https://go.dev/play/p/1bLcVetbTOI) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" md5Str := cryptor.Md5String(str) fmt.Println(md5Str) // Output: // 5d41402abc4b2a76b9719d911017c592 } ``` ### Md5StringWithBase64

Return the md5 value of string with base64.

Signature: ```go func Md5StringWithBase64(s string) string ``` Example:[Run](https://go.dev/play/p/Tcb-Z7LN2ax) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { md5Str := cryptor.Md5StringWithBase64("hello") fmt.Println(md5Str) // Output: // XUFAKrxLKna5cZ2REBfFkg== } ``` ### Md5Byte

Return the md5 string of byte slice.

Signature: ```go func Md5Byte(data []byte) string ``` Example:[Run](https://go.dev/play/p/suraalH8lyC) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { md5Str := cryptor.Md5Byte([]byte{'a'}) fmt.Println(md5Str) // Output: // 0cc175b9c0f1b6a831c399e269772661 } ``` ### Md5ByteWithBase64

Return the md5 string of byte slice with base64.

Signature: ```go func Md5ByteWithBase64(data []byte) string ``` Example:[Run](https://go.dev/play/p/Lx4gH7Vdr5_y) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { md5Str := cryptor.Md5ByteWithBase64([]byte("hello")) fmt.Println(md5Str) // Output: // XUFAKrxLKna5cZ2REBfFkg== } ``` ### Md5File

Get the md5 value of file.

Signature: ```go func Md5File(filepath string) (string, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { s := cryptor.Md5File("./main.go")) fmt.Println(s) } ``` ### Sha1

Get the sha1 value of string.

Signature: ```go func Sha1(str string) string ``` Example:[Run](https://go.dev/play/p/_m_uoD1deMT) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" result := cryptor.Sha1(str) fmt.Println(result) // Output: // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d } ``` ### Sha1WithBase64

Return the sha1 value (SHA-1 hash algorithm) of base64 string.

Signature: ```go func Sha1WithBase64(str string) string ``` Example:[Run](https://go.dev/play/p/fSyx-Gl2l2-) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { result := cryptor.Sha1WithBase64("hello") fmt.Println(result) // Output: // qvTGHdzF6KLavt4PO0gs2a6pQ00= } ``` ### Sha256

Get the sha256 value of string.

Signature: ```go func Sha256(str string) string ``` Example:[Run](https://go.dev/play/p/tU9tfBMIAr1) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" result := cryptor.Sha256(str) fmt.Println(result) // Output: // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 } ``` ### Sha256WithBase64

Return the sha256 value (SHA256 hash algorithm) of base64 string.

Signature: ```go func Sha256WithBase64(str string) string ``` Example:[Run](https://go.dev/play/p/85IXJHIal1k) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { result := cryptor.Sha256WithBase64("hello") fmt.Println(result) // Output: // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= } ``` ### Sha512

Get the sha512 value of string.

Signature: ```go func Sha512(str string) string ``` Example:[Run](https://go.dev/play/p/3WsvLYZxsHa) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { str := "hello" result := cryptor.Sha512(str) fmt.Println(result) // Output: // 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 } ``` ### Sha512WithBase64

Get the sha512 value of string with base64.

Signature: ```go func Sha512WithBase64(str string) string ``` Example:[Run](https://go.dev/play/p/q_fY2rA-k5I) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { result := cryptor.Sha512WithBase64("hello") fmt.Println(result) // Output: // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw== } ``` ### GenerateRsaKey

Create the rsa public and private key file in current directory.

Signature: ```go func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error ``` Example:[Run](https://go.dev/play/p/zutRHrDqs0X) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { fmt.Println(err) } } ``` ### RsaEncrypt

Encrypt data with public key file useing ras algorithm.

Signature: ```go func RsaEncrypt(data []byte, pubKeyFileName string) []byte ``` Example:[Run](https://go.dev/play/p/7_zo6mrx-eX) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { return } data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") fmt.Println(string(decrypted)) // Output: // hello } ``` ### RsaDecrypt

Decrypt data with private key file useing ras algorithm.

Signature: ```go func RsaDecrypt(data []byte, privateKeyFileName string) []byte ``` Example:[Run](https://go.dev/play/p/7_zo6mrx-eX) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { return } data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") fmt.Println(string(decrypted)) // Output: // hello } ``` ### GenerateRsaKeyPair

Creates rsa private and public key.

Signature: ```go func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) ``` Example:[Run](https://go.dev/play/p/sSVmkfENKMz) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { pri, pub := cryptor.GenerateRsaKeyPair(1024) } ``` ### RsaEncryptOAEP

Encrypts the given data with RSA-OAEP.

Signature: ```go func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) ``` Example:[Run](https://go.dev/play/p/sSVmkfENKMz) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { pri, pub := cryptor.GenerateRsaKeyPair(1024) data := []byte("hello world") label := []byte("123456") encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) if err != nil { return } decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) if err != nil { return } fmt.Println(string(decrypted)) // Output: // hello world } ``` ### RsaDecryptOAEP

Decrypts the data with RSA-OAEP.

Signature: ```go func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) ``` Example:[Run](https://go.dev/play/p/sSVmkfENKMz) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { pri, pub := cryptor.GenerateRsaKeyPair(1024) data := []byte("hello world") label := []byte("123456") encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) if err != nil { return } decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) if err != nil { return } fmt.Println(string(decrypted)) // Output: // hello world } ``` ### RsaSign

Signs the data with RSA algorithm.

Signature: ```go func RsaSign(hash crypto.Hash, data []byte, privateKeyFileName string) ([]byte, error) ``` Example:[Run](https://go.dev/play/p/qhsbf8BJ6Mf) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := []byte("This is a test data for RSA signing") hash := crypto.SHA256 privateKey := "./rsa_private.pem" publicKey := "./rsa_public.pem" signature, err := RsaSign(hash, data, privateKey) if err != nil { return } err = RsaVerifySign(hash, data, signature, publicKey) if err != nil { return } } ``` ### RsaVerifySign

Verifies the signature of the data with RSA algorithm.

Signature: ```go func RsaVerifySign(hash crypto.Hash, data, signature []byte, pubKeyFileName string) error ``` Example:[Run](https://go.dev/play/p/qhsbf8BJ6Mf) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := []byte("This is a test data for RSA signing") hash := crypto.SHA256 privateKey := "./rsa_private.pem" publicKey := "./rsa_public.pem" signature, err := RsaSign(hash, data, privateKey) if err != nil { return } err = RsaVerifySign(hash, data, signature, publicKey) if err != nil { return } } ``` ### Sm3

Calculate SM3 hash (Chinese National Cryptography SM3 Hash Algorithm). SM3 is a cryptographic hash algorithm published by the Chinese State Cryptography Administration, designed to replace MD5/SHA-1/SHA-2.

Signature: ```go func Sm3(data []byte) []byte ``` Example:[Run](https://go.dev/play/p/zDAQpteAiOc) ```go package main import ( "encoding/hex" "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { data := []byte("hello world") hash := cryptor.Sm3(data) fmt.Println(hex.EncodeToString(hash)) // Output: // 44f0061e69fa6fdfc290c494654a05dc0c053da7e5c52b84ef93a9d67d3fff88 } ``` ### Sm4EcbEncrypt

Encrypt data using SM4 ECB mode (Chinese National Cryptography SM4 Block Cipher Algorithm). Key length must be 16 bytes.

Signature: ```go func Sm4EcbEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/l5IQxYuuaED) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { key := []byte("1234567890abcdef") // 16 bytes key plaintext := []byte("hello world") encrypted := cryptor.Sm4EcbEncrypt(plaintext, key) decrypted := cryptor.Sm4EcbDecrypt(encrypted, key) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### Sm4EcbDecrypt

Decrypt data using SM4 ECB mode. Key length must be 16 bytes.

Signature: ```go func Sm4EcbDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/l5IQxYuuaED) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { key := []byte("1234567890abcdef") plaintext := []byte("hello world") encrypted := cryptor.Sm4EcbEncrypt(plaintext, key) decrypted := cryptor.Sm4EcbDecrypt(encrypted, key) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### Sm4CbcEncrypt

Encrypt data using SM4 CBC mode. Key length must be 16 bytes. The returned ciphertext contains IV (first 16 bytes).

Signature: ```go func Sm4CbcEncrypt(data, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/65Q6iYhLRTa) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { key := []byte("1234567890abcdef") plaintext := []byte("hello world") encrypted := cryptor.Sm4CbcEncrypt(plaintext, key) decrypted := cryptor.Sm4CbcDecrypt(encrypted, key) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### Sm4CbcDecrypt

Decrypt data using SM4 CBC mode. Key length must be 16 bytes. The ciphertext should contain IV (first 16 bytes).

Signature: ```go func Sm4CbcDecrypt(encrypted, key []byte) []byte ``` Example:[Run](https://go.dev/play/p/65Q6iYhLRTa) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { key := []byte("1234567890abcdef") plaintext := []byte("hello world") encrypted := cryptor.Sm4CbcEncrypt(plaintext, key) decrypted := cryptor.Sm4CbcDecrypt(encrypted, key) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### GenerateSm2Key

Generate SM2 key pair (Chinese National Cryptography SM2 Elliptic Curve Public Key Algorithm). SM2 is an asymmetric encryption algorithm based on elliptic curve cryptography.

Signature: ```go func GenerateSm2Key() (*Sm2PrivateKey, error) ``` Example:[Run](https://go.dev/play/p/bKYMqRLvIx3) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { privateKey, err := cryptor.GenerateSm2Key() if err != nil { return } plaintext := []byte("hello world") ciphertext, _ := cryptor.Sm2Encrypt(&privateKey.PublicKey, plaintext) decrypted, _ := cryptor.Sm2Decrypt(privateKey, ciphertext) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### Sm2Encrypt

Encrypt data using SM2 public key. The returned ciphertext format is: C1(65 bytes) || C3(32 bytes) || C2(plaintext length).

Signature: ```go func Sm2Encrypt(pub *Sm2PublicKey, plaintext []byte) ([]byte, error) ``` Example:[Run](https://go.dev/play/p/bKYMqRLvIx3) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { privateKey, _ := cryptor.GenerateSm2Key() plaintext := []byte("hello world") ciphertext, _ := cryptor.Sm2Encrypt(&privateKey.PublicKey, plaintext) decrypted, _ := cryptor.Sm2Decrypt(privateKey, ciphertext) fmt.Println(string(decrypted)) // Output: // hello world } ``` ### Sm2Decrypt

Decrypt data using SM2 private key. The ciphertext format should be: C1(65 bytes) || C3(32 bytes) || C2(plaintext length).

Signature: ```go func Sm2Decrypt(priv *Sm2PrivateKey, ciphertext []byte) ([]byte, error) ``` Example:[Run](https://go.dev/play/p/bKYMqRLvIx3) ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/cryptor" ) func main() { privateKey, _ := cryptor.GenerateSm2Key() plaintext := []byte("hello world") ciphertext, _ := cryptor.Sm2Encrypt(&privateKey.PublicKey, plaintext) decrypted, _ := cryptor.Sm2Decrypt(privateKey, ciphertext) fmt.Println(string(decrypted)) // Output: // hello world } ```