github.com/gogf/gf@v1.16.9/crypto/gmd5/gmd5.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package gmd5 provides useful API for MD5 encryption algorithms.
     8  package gmd5
     9  
    10  import (
    11  	"crypto/md5"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  
    16  	"github.com/gogf/gf/util/gconv"
    17  )
    18  
    19  // Encrypt encrypts any type of variable using MD5 algorithms.
    20  // It uses gconv package to convert <v> to its bytes type.
    21  func Encrypt(data interface{}) (encrypt string, err error) {
    22  	return EncryptBytes(gconv.Bytes(data))
    23  }
    24  
    25  // MustEncrypt encrypts any type of variable using MD5 algorithms.
    26  // It uses gconv package to convert <v> to its bytes type.
    27  // It panics if any error occurs.
    28  func MustEncrypt(data interface{}) string {
    29  	result, err := Encrypt(data)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return result
    34  }
    35  
    36  // EncryptBytes encrypts <data> using MD5 algorithms.
    37  func EncryptBytes(data []byte) (encrypt string, err error) {
    38  	h := md5.New()
    39  	if _, err = h.Write([]byte(data)); err != nil {
    40  		return "", err
    41  	}
    42  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    43  }
    44  
    45  // MustEncryptBytes encrypts <data> using MD5 algorithms.
    46  // It panics if any error occurs.
    47  func MustEncryptBytes(data []byte) string {
    48  	result, err := EncryptBytes(data)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	return result
    53  }
    54  
    55  // EncryptBytes encrypts string <data> using MD5 algorithms.
    56  func EncryptString(data string) (encrypt string, err error) {
    57  	return EncryptBytes([]byte(data))
    58  }
    59  
    60  // MustEncryptString encrypts string <data> using MD5 algorithms.
    61  // It panics if any error occurs.
    62  func MustEncryptString(data string) string {
    63  	result, err := EncryptString(data)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	return result
    68  }
    69  
    70  // EncryptFile encrypts file content of <path> using MD5 algorithms.
    71  func EncryptFile(path string) (encrypt string, err error) {
    72  	f, err := os.Open(path)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  	defer f.Close()
    77  	h := md5.New()
    78  	_, err = io.Copy(h, f)
    79  	if err != nil {
    80  		return "", err
    81  	}
    82  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    83  }
    84  
    85  // MustEncryptFile encrypts file content of <path> using MD5 algorithms.
    86  // It panics if any error occurs.
    87  func MustEncryptFile(path string) string {
    88  	result, err := EncryptFile(path)
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  	return result
    93  }