diff --git a/cryptor/basic.go b/cryptor/basic.go index 9fa6e78..b69b665 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -40,6 +40,14 @@ func Md5String(s string) string { return hex.EncodeToString(h.Sum(nil)) } +// Md5String return the md5 value of string. +// Play: https://go.dev/play/p/Lx4gH7Vdr5_y +func Md5StringWithBase64(s string) string { + h := md5.New() + h.Write([]byte(s)) + return base64.StdEncoding.EncodeToString(h.Sum(nil)) +} + // Md5Byte return the md5 string of byte slice. // Play: https://go.dev/play/p/suraalH8lyC func Md5Byte(data []byte) string { @@ -48,6 +56,14 @@ func Md5Byte(data []byte) string { return hex.EncodeToString(h.Sum(nil)) } +// Md5Byte return the md5 string of byte slice. +// Play: https://go.dev/play/p/CkN9hYKGeAy +func Md5ByteWithBase64(data []byte) string { + h := md5.New() + h.Write(data) + return base64.StdEncoding.EncodeToString(h.Sum(nil)) +} + // Md5File return the md5 value of file. func Md5File(filename string) (string, error) { if fileInfo, err := os.Stat(filename); err != nil { @@ -96,6 +112,14 @@ func HmacSha1(data, key string) string { return hex.EncodeToString(h.Sum([]byte(""))) } +// HmacSha1 return the hmac hash of string use sha1. +// Play: https://go.dev/play/p/47JmmGrnF7B +func HmacSha1WithBase64(data, key string) string { + h := hmac.New(sha1.New, []byte(key)) + h.Write([]byte(data)) + 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 { @@ -104,6 +128,14 @@ func HmacSha256(data, key string) string { return hex.EncodeToString(h.Sum([]byte(""))) } +// HmacSha256 return the hmac hash of string use sha256 with base64. +// Play: https://go.dev/play/p/EKbkUvPTLwO +func HmacSha256WithBase64(data, key string) string { + h := hmac.New(sha256.New, []byte(key)) + h.Write([]byte(data)) + 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 { @@ -112,6 +144,14 @@ func HmacSha512(data, key string) string { return hex.EncodeToString(h.Sum([]byte(""))) } +// HmacSha512 return the hmac hash of string use sha512. +// Play: https://go.dev/play/p/61wBBOKO-GH +func HmacSha512WithBase64(data, key string) string { + h := hmac.New(sha512.New, []byte(key)) + h.Write([]byte(data)) + 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 { @@ -120,6 +160,14 @@ func Sha1(data string) string { return hex.EncodeToString(sha1.Sum([]byte(""))) } +// Sha1 return the sha1 value (SHA-1 hash algorithm) of string. +// Play: qvTGHdzF6KLavt4PO0gs2a6pQ00= +func Sha1WithBase64(data string) string { + sha1 := sha1.New() + sha1.Write([]byte(data)) + 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 { @@ -128,6 +176,14 @@ func Sha256(data string) string { return hex.EncodeToString(sha256.Sum([]byte(""))) } +// Sha256 return the sha256 value (SHA256 hash algorithm) of string. +// Play: https://go.dev/play/p/85IXJHIal1k +func Sha256WithBase64(data string) string { + sha256 := sha256.New() + sha256.Write([]byte(data)) + 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 { @@ -135,3 +191,11 @@ func Sha512(data string) string { sha512.Write([]byte(data)) return hex.EncodeToString(sha512.Sum([]byte(""))) } + +// Sha512 return the sha512 value (SHA512 hash algorithm) of string. +// Play: https://go.dev/play/p/q_fY2rA-k5I +func Sha512WithBase64(data string) string { + sha512 := sha512.New() + sha512.Write([]byte(data)) + return base64.StdEncoding.EncodeToString(sha512.Sum([]byte(""))) +} \ No newline at end of file