mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// 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,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package gmd5 provides useful API for MD5 encryption algorithms.
|
|
package gmd5
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/gogf/gf/errors/gerror"
|
|
"github.com/gogf/gf/util/gconv"
|
|
)
|
|
|
|
// Encrypt encrypts any type of variable using MD5 algorithms.
|
|
// It uses gconv package to convert <v> to its bytes type.
|
|
func Encrypt(data interface{}) (encrypt string, err error) {
|
|
return EncryptBytes(gconv.Bytes(data))
|
|
}
|
|
|
|
// EncryptBytes encrypts <data> using MD5 algorithms.
|
|
func EncryptBytes(data []byte) (encrypt string, err error) {
|
|
h := md5.New()
|
|
if _, err = h.Write([]byte(data)); err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
|
}
|
|
|
|
// EncryptBytes encrypts string <data> using MD5 algorithms.
|
|
func EncryptString(data string) (encrypt string, err error) {
|
|
return EncryptBytes([]byte(data))
|
|
}
|
|
|
|
// EncryptFile encrypts file content of <path> using MD5 algorithms.
|
|
func EncryptFile(path string) (encrypt string, err error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer func() {
|
|
err = gerror.Wrap(f.Close(), "file closing error")
|
|
}()
|
|
h := md5.New()
|
|
_, err = io.Copy(h, f)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
|
}
|