2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-05-24 19:59:08 +08:00
|
|
|
// Package gmd5 provides useful API for MD5 encryption algorithms.
|
2017-11-23 10:21:28 +08:00
|
|
|
package gmd5
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"crypto/md5"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
2019-06-21 22:23:07 +08:00
|
|
|
|
2021-12-21 22:59:14 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
|
2019-05-24 19:59:08 +08:00
|
|
|
// Encrypt encrypts any type of variable using MD5 algorithms.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It uses gconv package to convert `v` to its bytes type.
|
2019-07-26 22:33:49 +08:00
|
|
|
func Encrypt(data interface{}) (encrypt string, err error) {
|
|
|
|
|
return EncryptBytes(gconv.Bytes(data))
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 11:40:23 +08:00
|
|
|
// MustEncrypt encrypts any type of variable using MD5 algorithms.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It uses gconv package to convert `v` to its bytes type.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
|
func MustEncrypt(data interface{}) string {
|
|
|
|
|
result, err := Encrypt(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// EncryptBytes encrypts `data` using MD5 algorithms.
|
2019-07-26 22:33:49 +08:00
|
|
|
func EncryptBytes(data []byte) (encrypt string, err error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
h := md5.New()
|
2021-12-21 22:59:14 +08:00
|
|
|
if _, err = h.Write(data); err != nil {
|
|
|
|
|
err = gerror.Wrap(err, `hash.Write failed`)
|
2019-06-21 22:23:07 +08:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// MustEncryptBytes encrypts `data` using MD5 algorithms.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
|
func MustEncryptBytes(data []byte) string {
|
|
|
|
|
result, err := EncryptBytes(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 22:59:14 +08:00
|
|
|
// EncryptString encrypts string `data` using MD5 algorithms.
|
2019-07-26 22:33:49 +08:00
|
|
|
func EncryptString(data string) (encrypt string, err error) {
|
|
|
|
|
return EncryptBytes([]byte(data))
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// MustEncryptString encrypts string `data` using MD5 algorithms.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
|
func MustEncryptString(data string) string {
|
|
|
|
|
result, err := EncryptString(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// EncryptFile encrypts file content of `path` using MD5 algorithms.
|
2019-06-21 22:23:07 +08:00
|
|
|
func EncryptFile(path string) (encrypt string, err error) {
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrapf(err, `os.Open failed for name "%s"`, path)
|
2019-06-21 22:23:07 +08:00
|
|
|
return "", err
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-10-11 22:54:25 +08:00
|
|
|
defer f.Close()
|
2019-06-19 09:06:52 +08:00
|
|
|
h := md5.New()
|
2019-06-21 22:23:07 +08:00
|
|
|
_, err = io.Copy(h, f)
|
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `io.Copy failed`)
|
2019-06-21 22:23:07 +08:00
|
|
|
return "", err
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-06-21 22:23:07 +08:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
2019-12-12 11:40:23 +08:00
|
|
|
|
2021-09-16 20:57:59 +08:00
|
|
|
// MustEncryptFile encrypts file content of `path` using MD5 algorithms.
|
2019-12-12 11:40:23 +08:00
|
|
|
// It panics if any error occurs.
|
|
|
|
|
func MustEncryptFile(path string) string {
|
|
|
|
|
result, err := EncryptFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|