github.com/gogf/gf@v1.16.9/crypto/gsha1/gsha1.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 gsha1 provides useful API for SHA1 encryption algorithms. 8 package gsha1 9 10 import ( 11 "crypto/sha1" 12 "encoding/hex" 13 "io" 14 "os" 15 16 "github.com/gogf/gf/util/gconv" 17 ) 18 19 // Encrypt encrypts any type of variable using SHA1 algorithms. 20 // It uses gconv package to convert <v> to its bytes type. 21 func Encrypt(v interface{}) string { 22 r := sha1.Sum(gconv.Bytes(v)) 23 return hex.EncodeToString(r[:]) 24 } 25 26 // EncryptFile encrypts file content of <path> using SHA1 algorithms. 27 func EncryptFile(path string) (encrypt string, err error) { 28 f, err := os.Open(path) 29 if err != nil { 30 return "", err 31 } 32 defer f.Close() 33 h := sha1.New() 34 _, err = io.Copy(h, f) 35 if err != nil { 36 return "", err 37 } 38 return hex.EncodeToString(h.Sum(nil)), nil 39 } 40 41 // MustEncryptFile encrypts file content of <path> using SHA1 algorithms. 42 // It panics if any error occurs. 43 func MustEncryptFile(path string) string { 44 result, err := EncryptFile(path) 45 if err != nil { 46 panic(err) 47 } 48 return result 49 }