diff --git a/.gitignore b/.gitignore index 6210f5f..fce5a7c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,4 @@ fileutil/*.txt fileutil/*.zip fileutil/*.link fileutil/unzip/* -cryptor/*.pem -docs/* \ No newline at end of file +cryptor/*.pem \ No newline at end of file diff --git a/docs/convertor.md b/docs/convertor.md new file mode 100644 index 0000000..7e72b08 --- /dev/null +++ b/docs/convertor.md @@ -0,0 +1,349 @@ +# Convertor +Package convertor contains some functions for data type convertion. + +
+ +## Source: + +[https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](!https://github.com/duke-git/lancet/blob/main/convertor/convertor.go) + + + +## Usage: +```go +import ( + "github.com/duke-git/lancet/convertor" +) +``` + + + +## Index +- [ColorHexToRGB](#ColorHexToRGB) +- [ColorRGBToHex](#ColorRGBToHex) +- [ToBool](#ToBool) +- [ToBytes](#ToBytes) +- [ToChar](#ToChar) +- [ToInt](#ToInt) +- [ToJson](#ToJson) +- [ToString](#ToString) +- [StructToMap](#StructToMap) + + + +## Documentation + + + +### ColorHexToRGB +Convert color hex to color rgb.
+ +Signature: + +```go +func ColorHexToRGB(colorHex string) (red, green, blue int) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + colorHex := "#003366" + r, g, b := ColorHexToRGB(colorHex) + fmt.Println(r, g, b) //0,51,102 +} +``` + + + +### ColorRGBToHex + +Convert color rgb to color hex.
+ +Signature: + +```go +func ColorRGBToHex(red, green, blue int) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + r := 0 + g := 51 + b := 102 + colorHex := ColorRGBToHex(r, g, b) + + fmt.Println(colorHex) //#003366 +} +``` + + + +### ToBool + +Convert string to a boolean value. Use strconv.ParseBool
+ +Signature: + +```go +func ToBool(s string) (bool, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v1, _ := convertor.ToBool("1") + fmt.Println(v1) //true + + v2, _ := convertor.ToBool("true") + fmt.Println(v2) //true + + v3, _ := convertor.ToBool("True") + fmt.Println(v3) //true + + v4, _ := convertor.ToBool("123") + fmt.Println(v4) //false +} +``` + + + +### ToBytes + +Convert interface to byte slice.
+ +Signature: + +```go +func ToBytes(data interface{}) ([]byte, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + bytesData, err := convertor.ToBytes("0") + if err != nil { + fmt.Println(err) + } + fmt.Println(bytesData) //[]bytes{3, 4, 0, 0} +} +``` + + + +### ToChar + +Convert string to char slice.
+ +Signature: + +```go +func ToChar(s string) []string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + chars := convertor.ToChar("") + fmt.Println(chars) //[]string{""} + + chars = convertor.ToChar("abc") + fmt.Println(chars) //[]string{"a", "b", "c"} + + chars = convertor.ToChar("1 2#3") + fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} +} +``` + + + +### ToFloat + +Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error.
+ +Signature: + +```go +func ToFloat(value interface{}) (float64, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v, err := convertor.ToFloat("") + if err != nil { + fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax + } + fmt.Println(v) //0 + + v, _ = convertor.ToFloat("-.11") + fmt.Println(v) //-0.11 +} +``` + + + +### ToInt + +Convert interface to a int64 value. If param is a invalid intable, will return 0 and error.
+ +Signature: + +```go +func ToInt(value interface{}) (int64, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v, err := convertor.ToInt("") + if err != nil { + fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax + } + fmt.Println(v) //0 + + v, _ = convertor.ToFloat(1.12) + fmt.Println(v) //1 +} +``` + + + +### ToJson + +Convert interface to json string. If param can't be converted, will return "" and error.
+ +Signature: + +```go +func ToJson(value interface{}) (string, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + var aMap = map[string]int{"a": 1, "b": 2, "c": 3} + jsonStr, _ := convertor.ToJson(aMap) + fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}" +} +``` + + + +### ToString + +Convert interface to string.
+ +Signature: + +```go +func ToString(value interface{}) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + fmt.Printf("%q", convertor.ToString(1)) //"1" + fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" + fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" +} +``` + + + +### StructToMap + +Convert struct to map, only convert exported field, struct field tag `json` should be set.
+ +Signature: + +```go +func StructToMap(value interface{}) (map[string]interface{}, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + type People struct { + Name string `json:"name"` + age int + } + p := People{ + "test", + 100, + } + pm, _ := convertor.StructToMap(p) + + fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] +} +``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md new file mode 100644 index 0000000..8a3a0b8 --- /dev/null +++ b/docs/convertor_zh-CN.md @@ -0,0 +1,351 @@ +# Convertor +convertor转换器包支持一些常见的数据类型转换 + + + +## 源码: + +[https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](!https://github.com/duke-git/lancet/blob/main/convertor/convertor.go) + + + +## 用法: + +```go +import ( + "github.com/duke-git/lancet/convertor" +) +``` + + + +## 目录 + +- [ColorHexToRGB](#ColorHexToRGB) +- [ColorRGBToHex](#ColorRGBToHex) +- [ToBool](#ToBool) +- [ToBytes](#ToBytes) +- [ToChar](#ToChar) +- [ToInt](#ToInt) +- [ToJson](#ToJson) +- [ToString](#ToString) +- [StructToMap](#StructToMap) + + + +## 文档 + + + +### ColorHexToRGB +颜色值十六进制转rgb
+ +函数签名: + +```go +func ColorHexToRGB(colorHex string) (red, green, blue int) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + colorHex := "#003366" + r, g, b := ColorHexToRGB(colorHex) + fmt.Println(r, g, b) //0,51,102 +} +``` + + + +### ColorRGBToHex + +颜色值rgb转十六进制
+ +函数签名: + +```go +func ColorRGBToHex(red, green, blue int) string +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + r := 0 + g := 51 + b := 102 + colorHex := ColorRGBToHex(r, g, b) + + fmt.Println(colorHex) //#003366 +} +``` + + + +### ToBool + +字符串转布尔类型,使用strconv.ParseBool
+ +函数签名: + +```go +func ToBool(s string) (bool, error) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v1, _ := convertor.ToBool("1") + fmt.Println(v1) //true + + v2, _ := convertor.ToBool("true") + fmt.Println(v2) //true + + v3, _ := convertor.ToBool("True") + fmt.Println(v3) //true + + v4, _ := convertor.ToBool("123") + fmt.Println(v4) //false +} +``` + + + +### ToBytes + +interface转字节切片.
+ +函数签名: + +```go +func ToBytes(data interface{}) ([]byte, error) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + bytesData, err := convertor.ToBytes("0") + if err != nil { + fmt.Println(err) + } + fmt.Println(bytesData) //[]bytes{3, 4, 0, 0} +} +``` + + + +### ToChar + +字符串转字符切片
+ +函数签名: + +```go +func ToChar(s string) []string +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + chars := convertor.ToChar("") + fmt.Println(chars) //[]string{""} + + chars = convertor.ToChar("abc") + fmt.Println(chars) //[]string{"a", "b", "c"} + + chars = convertor.ToChar("1 2#3") + fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} +} +``` + + + +### ToFloat + +将interface转成float64类型,如果参数无法转换,会返回0和error
+ +函数签名: + +```go +func ToFloat(value interface{}) (float64, error) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v, err := convertor.ToFloat("") + if err != nil { + fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax + } + fmt.Println(v) //0 + + v, _ = convertor.ToFloat("-.11") + fmt.Println(v) //-0.11 +} +``` + + + +### ToInt + +将interface转成intt64类型,如果参数无法转换,会返回0和error
+ +函数签名: + +```go +func ToInt(value interface{}) (int64, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + v, err := convertor.ToInt("") + if err != nil { + fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax + } + fmt.Println(v) //0 + + v, _ = convertor.ToFloat(1.12) + fmt.Println(v) //1 +} +``` + + + +### ToJson + +将interface转成json字符串,如果参数无法转换,会返回""和error
+ +函数签名: + +```go +func ToJson(value interface{}) (string, error) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + var aMap = map[string]int{"a": 1, "b": 2, "c": 3} + jsonStr, _ := convertor.ToJson(aMap) + fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}" +} +``` + + + +### ToString + +将interface转成字符串
+ +函数签名: + +```go +func ToString(value interface{}) string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + fmt.Printf("%q", convertor.ToString(1)) //"1" + fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" + fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" +} +``` + + + +### StructToMap + +将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记
+ +函数签名: + +```go +func StructToMap(value interface{}) (map[string]interface{}, error) +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/convertor" +) + +func main() { + type People struct { + Name string `json:"name"` + age int + } + p := People{ + "test", + 100, + } + pm, _ := convertor.StructToMap(p) + + fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] +} +``` \ No newline at end of file diff --git a/docs/cryptor.md b/docs/cryptor.md new file mode 100644 index 0000000..e09a093 --- /dev/null +++ b/docs/cryptor.md @@ -0,0 +1,1026 @@ +# Cryptor +Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa. + + + +## Source: + +- [https://github.com/duke-git/lancet/blob/main/cryptor/aes.go](!https://github.com/duke-git/lancet/blob/main/cryptor/aes.go) +- [https://github.com/duke-git/lancet/blob/main/cryptor/des.go](!https://github.com/duke-git/lancet/blob/main/cryptor/des.go) +- [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/rsa.go](!https://github.com/duke-git/lancet/blob/main/cryptor/ras.go) + + + +## Usage: +```go +import ( + "github.com/duke-git/lancet/cryptor" +) +``` + + + +## Index + +- [AesEcbEncrypt](#AesEcbEncrypt) +- [AesEcbDecrypt](#AesEcbDecrypt) +- [AesCbcEncrypt](#AesCbcEncrypt) +- [AesCbcDecrypt](#AesCbcDecrypt) +- [AesCtrCrypt](#AesCtrCrypt) +- [AesCfbEncrypt](#AesCfbEncrypt) +- [AesCfbDecrypt](#AesCfbDecrypt) +- [AesOfbEncrypt](#AesOfbEncrypt) +- [AesOfbDecrypt](#AesOfbDecrypt) +- [Base64StdEncode](#Base64StdEncode) +- [Base64StdDecode](#Base64StdDecode) +- [DesEcbEncrypt](#DesEcbEncrypt) +- [DesEcbDecrypt](#DesEcbDecrypt) +- [DesCbcEncrypt](#DesCbcEncrypt) +- [DesCbcDecrypt](#DesCbcDecrypt) +- [DesCtrCrypt](#DesCtrCrypt) +- [DesCfbEncrypt](#DesCfbEncrypt) +- [DesCfbDecrypt](#DesCfbDecrypt) +- [DesOfbEncrypt](#DesOfbEncrypt) +- [DesOfbDecrypt](#DesOfbDecrypt) +- [HmacMd5](#HmacMd5) +- [HmacSha1](#HmacSha1) +- [HmacSha256](#HmacSha256) +- [HmacSha512](#HmacSha512) + +- [Md5String](#Md5String) +- [Md5File](#Md5File) +- [Sha1](#Sha1) +- [Sha256](#Sha256) +- [Sha512](#Sha512) +- [GenerateRsaKey](#GenerateRsaKey) +- [RsaEncrypt](#RsaEncrypt) +- [RsaDecrypt](#RsaDecrypt) + + + + +## 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### AesCtrCrypt + +Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.
+ +Signature: + +```go +func AesCtrCrypt(data, key []byte) []byte +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### Base64StdEncode + +Encode string with base64 encoding.
+ +Signature: + +```go +func Base64StdEncode(s string) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + base64Str := cryptor.Base64StdEncode("hello world") + fmt.Println(base64Str) //aGVsbG8gd29ybGQ= +} +``` + + + +### Base64StdDecode + +Decode a base64 encoded string.
+ +Signature: + +```go +func Base64StdDecode(s string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=") + fmt.Println(str) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### DesCtrCrypt + +Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.
+ +Signature: + +```go +func DesCtrCrypt(data, key []byte) []byte +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### 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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### HmacMd5 + +Get the md5 hmac hash of string.
+ +Signature: + +```go +func HmacMd5(data, key string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacMd5("hello world", "12345")) + fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d +} +``` + + + +### HmacSha1 + +Get the sha1 hmac hash of string.
+ +Signature: + +```go +func HmacSha1(data, key string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha1("hello world", "12345")) + fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd +} +``` + + + +### HmacSha256 + +Get the sha256 hmac hash of string
+ +Signature: + +```go +func HmacSha256(data, key string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha256("hello world", "12345")) + fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8 +} +``` + + + +### HmacSha512 + +Get the sha512 hmac hash of string.
+ +Signature: + +```go +func HmacSha512(data, key string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha512("hello world", "12345")) + fmt.Println(s) + //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175 +} +``` + + + +### Md5String + +Get the md5 value of string.
+ +Signature: + +```go +func Md5String(s string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Md5String("hello")) + fmt.Println(s) //5d41402abc4b2a76b9719d911017c592 +} +``` + + + +### 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/cryptor" +) + +func main() { + s := cryptor.Md5File("./main.go")) + fmt.Println(s) +} +``` + + + +### Sha1 + +Get the sha1 value of string.
+ +Signature: + +```go +func Sha1(data string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha1("hello world")) + fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed +} +``` + + + +### Sha256 + +Get the sha256 value of string.
+ +Signature: + +```go +func Sha256(data string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha256("hello world")) + fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 +} +``` + + + +### Sha512 + +Get the sha512 value of string.
+ +Signature: + +```go +func Sha512(data string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha512("hello world")) + fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f +} +``` + + + +### GenerateRsaKey + +Create the rsa public and private key file in current directory.
+ +Signature: + +```go +func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/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: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") + if err != nil { + fmt.Println(err) + } + data := []byte("hello world") + encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") + decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### RsaDecrypt + +Decrypt data with private key file useing ras algorithm.
+ +Signature: + +```go +func RsaDecrypt(data []byte, privateKeyFileName string) []byte +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") + if err != nil { + fmt.Println(err) + } + data := []byte("hello world") + encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") + decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") + fmt.Println(string(decrypted)) //hello world +} +``` + + + diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md new file mode 100644 index 0000000..e806ee6 --- /dev/null +++ b/docs/cryptor_zh-CN.md @@ -0,0 +1,1026 @@ +# Cryptor +cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64, md5, hmac, aes, des, rsa。 + + + +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/cryptor/aes.go](!https://github.com/duke-git/lancet/blob/main/cryptor/aes.go) +- [https://github.com/duke-git/lancet/blob/main/cryptor/des.go](!https://github.com/duke-git/lancet/blob/main/cryptor/des.go) +- [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/rsa.go](!https://github.com/duke-git/lancet/blob/main/cryptor/ras.go) + + + +## 用法: +```go +import ( + "github.com/duke-git/lancet/cryptor" +) +``` + + + +## 目录 + +- [AesEcbEncrypt](#AesEcbEncrypt) +- [AesEcbDecrypt](#AesEcbDecrypt) +- [AesCbcEncrypt](#AesCbcEncrypt) +- [AesCbcDecrypt](#AesCbcDecrypt) +- [AesCtrCrypt](#AesCtrCrypt) +- [AesCfbEncrypt](#AesCfbEncrypt) +- [AesCfbDecrypt](#AesCfbDecrypt) +- [AesOfbEncrypt](#AesOfbEncrypt) +- [AesOfbDecrypt](#AesOfbDecrypt) +- [Base64StdEncode](#Base64StdEncode) +- [Base64StdDecode](#Base64StdDecode) +- [DesEcbEncrypt](#DesEcbEncrypt) +- [DesEcbDecrypt](#DesEcbDecrypt) +- [DesCbcEncrypt](#DesCbcEncrypt) +- [DesCbcDecrypt](#DesCbcDecrypt) +- [DesCtrCrypt](#DesCtrCrypt) +- [DesCfbEncrypt](#DesCfbEncrypt) +- [DesCfbDecrypt](#DesCfbDecrypt) +- [DesOfbEncrypt](#DesOfbEncrypt) +- [DesOfbDecrypt](#DesOfbDecrypt) +- [HmacMd5](#HmacMd5) +- [HmacSha1](#HmacSha1) +- [HmacSha256](#HmacSha256) +- [HmacSha512](#HmacSha512) + +- [Md5String](#Md5String) +- [Md5File](#Md5File) +- [Sha1](#Sha1) +- [Sha256](#Sha256) +- [Sha512](#Sha512) +- [GenerateRsaKey](#GenerateRsaKey) +- [RsaEncrypt](#RsaEncrypt) +- [RsaDecrypt](#RsaDecrypt) + + + + +## 文档 + + + +### AesEcbEncrypt + +使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesEcbEncrypt(data, key []byte) []byte +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### AesEcbDecrypt + +使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。 + +函数签名: + +```go +func AesEcbDecrypt(encrypted, key []byte) []byte +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### AesCbcEncrypt + +
使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesCbcEncrypt(data, key []byte) []byte +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### AesCbcDecrypt + +使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesCbcDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### AesCtrCrypt + +使用AES CTR算法模式加密/解密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesCtrCrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### AesCfbEncrypt + +使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesCfbEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### AesCfbDecrypt + +使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。
+ +函数签名: + +```go +func AesCfbDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### AesOfbEncrypt + +使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32
+ +函数签名: + +```go +func AesOfbEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### AesOfbDecrypt + +使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32
+ +函数签名: + +```go +func AesOfbDecrypt(encrypted, key []byte) []byte +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefghijklmnop" + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### Base64StdEncode + +将字符串base64编码
+ +函数签名: + +```go +func Base64StdEncode(s string) string +``` +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + base64Str := cryptor.Base64StdEncode("hello world") + fmt.Println(base64Str) //aGVsbG8gd29ybGQ= +} +``` + + + +### Base64StdDecode + +解码base64字符串
+ +函数签名: + +```go +func Base64StdDecode(s string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=") + fmt.Println(str) //hello world +} +``` + + + +### DesEcbEncrypt + +使用DES ECB算法模式加密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesEcbEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### DesEcbDecrypt + +使用DES ECB算法模式解密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesEcbDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### DesCbcEncrypt + +使用DES CBC算法模式加密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesCbcEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + + fmt.Println(string(encrypted)) +} +``` + + + +### DesCbcDecrypt + +使用DES CBC算法模式解密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesCbcDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### DesCtrCrypt + +使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesCtrCrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### DesCfbEncrypt + +使用DES CFB算法模式加密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesCfbEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### DesCfbDecrypt + +使用DES CFB算法模式解密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesCfbDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### DesOfbEncrypt + +使用DES OFB算法模式加密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesOfbEncrypt(data, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + fmt.Println(string(encrypted)) +} +``` + + + +### DesOfbDecrypt + +使用DES OFB算法模式解密数据. 参数`key`的长度是8
+ +函数签名: + +```go +func DesOfbDecrypt(encrypted, key []byte) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + data := "hello world" + key := "abcdefgh" + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### HmacMd5 + +获取字符串md5 hmac值
+ +函数签名: + +```go +func HmacMd5(data, key string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacMd5("hello world", "12345")) + fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d +} +``` + + + +### HmacSha1 + +获取字符串sha1 hmac值
+ +函数签名: + +```go +func HmacSha1(data, key string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha1("hello world", "12345")) + fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd +} +``` + + + +### HmacSha256 + +获取字符串sha256 hmac值
+ +函数签名: + +```go +func HmacSha256(data, key string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha256("hello world", "12345")) + fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8 +} +``` + + + +### HmacSha512 + +获取字符串sha512 hmac值
+ +函数签名: + +```go +func HmacSha512(data, key string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.HmacSha512("hello world", "12345")) + fmt.Println(s) + //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175 +} +``` + + + +### Md5String + +获取字符串md5值
+ +函数签名: + +```go +func Md5String(s string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Md5String("hello")) + fmt.Println(s) //5d41402abc4b2a76b9719d911017c592 +} +``` + + + +### Md5File + +获取文件md5值.
+ +函数签名: + +```go +func Md5File(filepath string) (string, error) +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Md5File("./main.go")) + fmt.Println(s) +} +``` + + + +### Sha1 + +获取字符串sha1值
+ +函数签名: + +```go +func Sha1(data string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha1("hello world")) + fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed +} +``` + + + +### Sha256 + +获取字符串sha256值
+ +函数签名: + +```go +func Sha256(data string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha256("hello world")) + fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 +} +``` + + + +### Sha512 + +获取字符串sha512值
+ +函数签名: + +```go +func Sha512(data string) string +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + s := cryptor.Sha512("hello world")) + fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f +} +``` + + + +### GenerateRsaKey + +在当前目录下创建rsa私钥文件和公钥文件
+ +函数签名: + +```go +func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") + if err != nil { + fmt.Println(err) + } +} +``` + + + +### RsaEncrypt + +用公钥文件ras加密数据
+ +函数签名: + +```go +func RsaEncrypt(data []byte, pubKeyFileName string) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") + if err != nil { + fmt.Println(err) + } + data := []byte("hello world") + encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") + decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") + fmt.Println(string(decrypted)) //hello world +} +``` + + + +### RsaDecrypt + +用私钥文件rsa解密数据
+ +函数签名: + +```go +func RsaDecrypt(data []byte, privateKeyFileName string) []byte +``` + +列子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") + if err != nil { + fmt.Println(err) + } + data := []byte("hello world") + encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") + decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") + fmt.Println(string(decrypted)) //hello world +} +``` + + +