gmd5.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Package gmd5 provides useful API for MD5 encryption algorithms.
  7. package gmd5
  8. import (
  9. "crypto/md5"
  10. "fmt"
  11. "io"
  12. "os"
  13. "github.com/gogf/gf/v2/errors/gerror"
  14. "github.com/gogf/gf/v2/util/gconv"
  15. )
  16. // Encrypt encrypts any type of variable using MD5 algorithms.
  17. // It uses gconv package to convert `v` to its bytes type.
  18. func Encrypt(data interface{}) (encrypt string, err error) {
  19. return EncryptBytes(gconv.Bytes(data))
  20. }
  21. // MustEncrypt encrypts any type of variable using MD5 algorithms.
  22. // It uses gconv package to convert `v` to its bytes type.
  23. // It panics if any error occurs.
  24. func MustEncrypt(data interface{}) string {
  25. result, err := Encrypt(data)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return result
  30. }
  31. // EncryptBytes encrypts `data` using MD5 algorithms.
  32. func EncryptBytes(data []byte) (encrypt string, err error) {
  33. h := md5.New()
  34. if _, err = h.Write(data); err != nil {
  35. err = gerror.Wrap(err, `hash.Write failed`)
  36. return "", err
  37. }
  38. return fmt.Sprintf("%x", h.Sum(nil)), nil
  39. }
  40. // MustEncryptBytes encrypts `data` using MD5 algorithms.
  41. // It panics if any error occurs.
  42. func MustEncryptBytes(data []byte) string {
  43. result, err := EncryptBytes(data)
  44. if err != nil {
  45. panic(err)
  46. }
  47. return result
  48. }
  49. // EncryptString encrypts string `data` using MD5 algorithms.
  50. func EncryptString(data string) (encrypt string, err error) {
  51. return EncryptBytes([]byte(data))
  52. }
  53. // MustEncryptString encrypts string `data` using MD5 algorithms.
  54. // It panics if any error occurs.
  55. func MustEncryptString(data string) string {
  56. result, err := EncryptString(data)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return result
  61. }
  62. // EncryptFile encrypts file content of `path` using MD5 algorithms.
  63. func EncryptFile(path string) (encrypt string, err error) {
  64. f, err := os.Open(path)
  65. if err != nil {
  66. err = gerror.Wrapf(err, `os.Open failed for name "%s"`, path)
  67. return "", err
  68. }
  69. defer f.Close()
  70. h := md5.New()
  71. _, err = io.Copy(h, f)
  72. if err != nil {
  73. err = gerror.Wrap(err, `io.Copy failed`)
  74. return "", err
  75. }
  76. return fmt.Sprintf("%x", h.Sum(nil)), nil
  77. }
  78. // MustEncryptFile encrypts file content of `path` using MD5 algorithms.
  79. // It panics if any error occurs.
  80. func MustEncryptFile(path string) string {
  81. result, err := EncryptFile(path)
  82. if err != nil {
  83. panic(err)
  84. }
  85. return result
  86. }