github.com/zhongdalu/gf@v1.0.0/g/crypto/gsha1/gsha1.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/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/zhongdalu/gf/g/errors/gerror" 17 "github.com/zhongdalu/gf/g/util/gconv" 18 ) 19 20 // Encrypt encrypts any type of variable using SHA1 algorithms. 21 // It uses gconv package to convert <v> to its bytes type. 22 func Encrypt(v interface{}) string { 23 r := sha1.Sum(gconv.Bytes(v)) 24 return hex.EncodeToString(r[:]) 25 } 26 27 // EncryptString is alias of Encrypt. 28 // Deprecated. 29 func EncryptString(s string) string { 30 return Encrypt(s) 31 } 32 33 // EncryptFile encrypts file content of <path> using SHA1 algorithms. 34 func EncryptFile(path string) (encrypt string, err error) { 35 f, err := os.Open(path) 36 if err != nil { 37 return "", err 38 } 39 defer func() { 40 err = gerror.Wrap(f.Close(), "file closing error") 41 }() 42 h := sha1.New() 43 _, err = io.Copy(h, f) 44 if err != nil { 45 return "", err 46 } 47 return hex.EncodeToString(h.Sum(nil)), nil 48 }