From 4715301240b19093c8596c5272f3152cf49a9850 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 27 Jul 2023 11:20:06 +0800 Subject: [PATCH] doc: update doc for cryptor package --- cryptor/basic.go | 68 +++++------ docs/cryptor.md | 262 +++++++++++++++++++++++++++++++++++++++- docs/cryptor_zh-CN.md | 269 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 550 insertions(+), 49 deletions(-) diff --git a/cryptor/basic.go b/cryptor/basic.go index 46049d8..b37ff97 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -40,7 +40,7 @@ func Md5String(s string) string { return hex.EncodeToString(h.Sum(nil)) } -// Md5String return the md5 value of string. +// Md5StringWithBase64 return the md5 value of string with base64. // Play: https://go.dev/play/p/Lx4gH7Vdr5_y func Md5StringWithBase64(s string) string { h := md5.New() @@ -56,7 +56,7 @@ func Md5Byte(data []byte) string { return hex.EncodeToString(h.Sum(nil)) } -// Md5Byte return the md5 string of byte slice. +// Md5ByteWithBase64 return the md5 string of byte slice with base64. // Play: https://go.dev/play/p/CkN9hYKGeAy func Md5ByteWithBase64(data []byte) string { h := md5.New() @@ -98,104 +98,104 @@ func Md5File(filename string) (string, error) { // HmacMd5 return the hmac hash of string use md5. // Play: https://go.dev/play/p/uef0q1fz53I -func HmacMd5(data, key string) string { +func HmacMd5(str, key string) string { h := hmac.New(md5.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return hex.EncodeToString(h.Sum([]byte(""))) } // HmacSha1 return the hmac hash of string use sha1. // Play: https://go.dev/play/p/1UI4oQ4WXKM -func HmacSha1(data, key string) string { +func HmacSha1(str, key string) string { h := hmac.New(sha1.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return hex.EncodeToString(h.Sum([]byte(""))) } -// HmacSha1 return the hmac hash of string use sha1. +// HmacSha1WithBase64 return the hmac hash of string use sha1 with base64. // Play: https://go.dev/play/p/47JmmGrnF7B -func HmacSha1WithBase64(data, key string) string { +func HmacSha1WithBase64(str, key string) string { h := hmac.New(sha1.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return base64.StdEncoding.EncodeToString(h.Sum([]byte(""))) } // HmacSha256 return the hmac hash of string use sha256. // Play: https://go.dev/play/p/HhpwXxFhhC0 -func HmacSha256(data, key string) string { +func HmacSha256(str, key string) string { h := hmac.New(sha256.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return hex.EncodeToString(h.Sum([]byte(""))) } -// HmacSha256 return the hmac hash of string use sha256 with base64. +// HmacSha256WithBase64 return the hmac hash of string use sha256 with base64. // Play: https://go.dev/play/p/EKbkUvPTLwO -func HmacSha256WithBase64(data, key string) string { +func HmacSha256WithBase64(str, key string) string { h := hmac.New(sha256.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return base64.StdEncoding.EncodeToString(h.Sum([]byte(""))) } // HmacSha512 return the hmac hash of string use sha512. // Play: https://go.dev/play/p/59Od6m4A0Ud -func HmacSha512(data, key string) string { +func HmacSha512(str, key string) string { h := hmac.New(sha512.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return hex.EncodeToString(h.Sum([]byte(""))) } -// HmacSha512 return the hmac hash of string use sha512 with base64.. +// HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. // Play: https://go.dev/play/p/61wBBOKO-GH -func HmacSha512WithBase64(data, key string) string { +func HmacSha512WithBase64(str, key string) string { h := hmac.New(sha512.New, []byte(key)) - h.Write([]byte(data)) + h.Write([]byte(str)) return base64.StdEncoding.EncodeToString(h.Sum([]byte(""))) } // Sha1 return the sha1 value (SHA-1 hash algorithm) of string. // Play: https://go.dev/play/p/_m_uoD1deMT -func Sha1(data string) string { +func Sha1(str string) string { sha1 := sha1.New() - sha1.Write([]byte(data)) + sha1.Write([]byte(str)) return hex.EncodeToString(sha1.Sum([]byte(""))) } -// Sha1 return the sha1 value (SHA-1 hash algorithm) of base64 string. +// Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string. // Play: todo -func Sha1WithBase64(data string) string { +func Sha1WithBase64(str string) string { sha1 := sha1.New() - sha1.Write([]byte(data)) + sha1.Write([]byte(str)) return base64.StdEncoding.EncodeToString(sha1.Sum([]byte(""))) } // Sha256 return the sha256 value (SHA256 hash algorithm) of string. // Play: https://go.dev/play/p/tU9tfBMIAr1 -func Sha256(data string) string { +func Sha256(str string) string { sha256 := sha256.New() - sha256.Write([]byte(data)) + sha256.Write([]byte(str)) return hex.EncodeToString(sha256.Sum([]byte(""))) } -// Sha256 return the sha256 value (SHA256 hash algorithm) of base64 string. +// Sha256WithBase64 return the sha256 value (SHA256 hash algorithm) of base64 string. // Play: https://go.dev/play/p/85IXJHIal1k -func Sha256WithBase64(data string) string { +func Sha256WithBase64(str string) string { sha256 := sha256.New() - sha256.Write([]byte(data)) + sha256.Write([]byte(str)) return base64.StdEncoding.EncodeToString(sha256.Sum([]byte(""))) } // Sha512 return the sha512 value (SHA512 hash algorithm) of string. // Play: https://go.dev/play/p/3WsvLYZxsHa -func Sha512(data string) string { +func Sha512(str string) string { sha512 := sha512.New() - sha512.Write([]byte(data)) + sha512.Write([]byte(str)) return hex.EncodeToString(sha512.Sum([]byte(""))) } -// Sha512 return the sha512 value (SHA512 hash algorithm) of base64 string. +// Sha512WithBase64 return the sha512 value (SHA512 hash algorithm) of base64 string. // Play: https://go.dev/play/p/q_fY2rA-k5I -func Sha512WithBase64(data string) string { +func Sha512WithBase64(str string) string { sha512 := sha512.New() - sha512.Write([]byte(data)) + sha512.Write([]byte(str)) return base64.StdEncoding.EncodeToString(sha512.Sum([]byte(""))) } diff --git a/docs/cryptor.md b/docs/cryptor.md index 7cf707b..d3da8e8 100644 --- a/docs/cryptor.md +++ b/docs/cryptor.md @@ -45,14 +45,22 @@ import ( - [DesOfbDecrypt](#DesOfbDecrypt) - [HmacMd5](#HmacMd5) - [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) @@ -761,7 +769,7 @@ func main() { Signature: ```go -func HmacSha1(data, key string) string +func HmacSha1(str, key string) string ``` Example: @@ -785,6 +793,39 @@ func main() { // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } ``` + +### HmacSha1WithBase64 + +

Return the hmac hash of string use sha1 with base64.

+ +Signature: + +```go +func HmacSha1WithBase64(str, key string) string +``` + +Example: + +```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

@@ -792,7 +833,7 @@ func main() { Signature: ```go -func HmacSha256(data, key string) string +func HmacSha256(str, key string) string ``` Example: @@ -817,6 +858,38 @@ func main() { } ``` +### HmacSha256WithBase64 + +

Return the hmac hash of string use sha256 with base64.

+ +Signature: + +```go +func HmacSha256WithBase64(str, key string) string +``` + +Example: + +```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.

@@ -824,7 +897,7 @@ func main() { Signature: ```go -func HmacSha512(data, key string) string +func HmacSha512(str, key string) string ``` Example: @@ -849,6 +922,38 @@ func main() { } ``` +### HmacSha512WithBase64 + +

Return the hmac hash of string use sha512 with base64.

+ +Signature: + +```go +func HmacSha512WithBase64(str, key string) string +``` + +Example: + +```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 @@ -881,6 +986,35 @@ func main() { } ``` +### Md5StringWithBase64 + +

Return the md5 value of string with base64.

+ +Signature: + +```go +func Md5StringWithBase64(s string) string +``` + +Example: + +```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.

@@ -910,6 +1044,35 @@ func main() { } ``` +### Md5ByteWithBase64 + +

Return the md5 string of byte slice with base64.

+ +Signature: + +```go +func Md5ByteWithBase64(data []byte) string +``` + +Example: + +```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.

@@ -943,7 +1106,7 @@ func main() { Signature: ```go -func Sha1(data string) string +func Sha1(str string) string ``` Example: @@ -967,6 +1130,35 @@ func main() { } ``` +### Sha1WithBase64 + +

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

+ +Signature: + +```go +func Sha1WithBase64(str string) string +``` + +Example: + +```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.

@@ -974,7 +1166,7 @@ func main() { Signature: ```go -func Sha256(data string) string +func Sha256(str string) string ``` Example: @@ -998,6 +1190,35 @@ func main() { } ``` +### Sha256WithBase64 + +

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

+ +Signature: + +```go +func Sha256WithBase64(str string) string +``` + +Example: + +```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.

@@ -1005,7 +1226,7 @@ func main() { Signature: ```go -func Sha512(data string) string +func Sha512(str string) string ``` Example: @@ -1029,6 +1250,35 @@ func main() { } ``` +### Sha512WithBase64 + +

Get the sha512 value of string with base64.

+ +Signature: + +```go +func Sha512WithBase64(str string) string +``` + +Example: + +```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.

diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index ba6dc69..adb5a53 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -42,14 +42,22 @@ import ( - [DesOfbDecrypt](#DesOfbDecrypt) - [HmacMd5](#HmacMd5) - [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) @@ -755,12 +763,12 @@ func main() { ``` ### HmacSha1 -

获取字符串sha1 hmac值。

+

获取字符串的sha1 hmac值。

函数签名: ```go -func HmacSha1(data, key string) string +func HmacSha1(str, key string) string ``` 示例: @@ -784,6 +792,40 @@ func main() { // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } ``` + +### HmacSha1WithBase64 + +

获取字符串的sha1 base64值。

+ +函数签名: + +```go +func HmacSha1WithBase64(str, key string) string +``` + +实例: + +```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

获取字符串sha256 hmac值。

@@ -791,7 +833,7 @@ func main() { 函数签名: ```go -func HmacSha256(data, key string) string +func HmacSha256(str, key string) string ``` 示例: @@ -816,6 +858,38 @@ func main() { } ``` +### HmacSha256WithBase64 + +

获取字符串sha256 hmac base64值。

+ +函数签名: + +```go +func HmacSha256WithBase64(str, key string) string +``` + +实例: + +```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

获取字符串sha512 hmac值。

@@ -823,7 +897,7 @@ func main() { 函数签名: ```go -func HmacSha512(data, key string) string +func HmacSha512(str, key string) string ``` 示例: @@ -848,6 +922,38 @@ func main() { } ``` +### HmacSha512WithBase64 + +

获取字符串sha512 hmac base64值。

+ +函数签名: + +```go +func HmacSha512WithBase64(str, key string) string +``` + +实例: + +```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 @@ -856,7 +962,7 @@ func main() { 函数签名: ```go -func Md5String(s string) string +func Md5String(str string) string ``` 示例: @@ -880,9 +986,38 @@ func main() { } ``` +### Md5StringWithBase64 + +

获取字符串md5 base64值。

+ +函数签名: + +```go +func Md5StringWithBase64(s string) string +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + md5Str := cryptor.Md5StringWithBase64("hello") + fmt.Println(md5Str) + + // Output: + // XUFAKrxLKna5cZ2REBfFkg== +} +``` + ### Md5Byte -

获取byte slice的md5至。

+

获取byte slice的md5值。

函数签名: @@ -909,6 +1044,35 @@ func main() { } ``` +### Md5ByteWithBase64 + +

获取byte slice的md5 base64值。

+ +函数签名: + +```go +func Md5ByteWithBase64(data []byte) string +``` + +实例: + +```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

获取文件md5值。

@@ -942,7 +1106,7 @@ func main() { 函数签名: ```go -func Sha1(data string) string +func Sha1(str string) string ``` 示例: @@ -966,6 +1130,35 @@ func main() { } ``` +### Sha1WithBase64 + +

获取字符串sha1 base64值。

+ +函数签名: + +```go +func Sha1WithBase64(str string) string +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + result := cryptor.Sha1WithBase64("hello") + fmt.Println(result) + + // Output: + // qvTGHdzF6KLavt4PO0gs2a6pQ00= +} +``` + ### Sha256

获取字符串sha256值。

@@ -973,7 +1166,7 @@ func main() { 函数签名: ```go -func Sha256(data string) string +func Sha256(str string) string ``` 示例: @@ -997,6 +1190,35 @@ func main() { } ``` +### Sha256WithBase64 + +

获取字符串sha256 base64值。

+ +函数签名: + +```go +func Sha256WithBase64(str string) string +``` + +实例: + +```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

获取字符串sha512值。

@@ -1004,7 +1226,7 @@ func main() { 函数签名: ```go -func Sha512(data string) string +func Sha512(str string) string ``` 示例: @@ -1028,6 +1250,35 @@ func main() { } ``` +### Sha512WithBase64 + +

获取字符串sha512 base64值。

+ +函数签名: + +```go +func Sha512WithBase64(str string) string +``` + +实例: + +```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

在当前目录下创建rsa私钥文件和公钥文件。