diff --git a/g/encoding/gcrc32/gcrc32.go b/g/crypto/gcrc32/gcrc32.go similarity index 84% rename from g/encoding/gcrc32/gcrc32.go rename to g/crypto/gcrc32/gcrc32.go index da9124565..38cf205db 100644 --- a/g/encoding/gcrc32/gcrc32.go +++ b/g/crypto/gcrc32/gcrc32.go @@ -11,10 +11,10 @@ import ( "hash/crc32" ) -func EncodeString(v string) uint32 { +func EncryptString(v string) uint32 { return crc32.ChecksumIEEE([]byte(v)) } -func EncodeBytes(v []byte) uint32 { +func EncryptBytes(v []byte) uint32 { return crc32.ChecksumIEEE(v) } diff --git a/g/encoding/gdes/gdes.go b/g/crypto/gdes/gdes.go similarity index 100% rename from g/encoding/gdes/gdes.go rename to g/crypto/gdes/gdes.go diff --git a/g/encoding/gdes/gdes_test.go b/g/crypto/gdes/gdes_test.go similarity index 99% rename from g/encoding/gdes/gdes_test.go rename to g/crypto/gdes/gdes_test.go index 8d19e1805..00dc2d13b 100644 --- a/g/encoding/gdes/gdes_test.go +++ b/g/crypto/gdes/gdes_test.go @@ -1,4 +1,4 @@ -package gdes_test +package gdes import ( "testing" diff --git a/g/encoding/gmd5/gmd5.go b/g/crypto/gmd5/gmd5.go similarity index 64% rename from g/encoding/gmd5/gmd5.go rename to g/crypto/gmd5/gmd5.go index 9b7dc1339..8f9efb595 100644 --- a/g/encoding/gmd5/gmd5.go +++ b/g/crypto/gmd5/gmd5.go @@ -10,46 +10,35 @@ package gmd5 import ( "crypto/md5" "fmt" - "encoding/json" - "reflect" "os" "io" - "gitee.com/johng/gf/g/os/glog" + "gitee.com/johng/gf/g/util/gconv" ) // 将任意类型的变量进行md5摘要(注意map等非排序变量造成的不同结果) -func Encode(v interface{}) string { +func Encrypt(v interface{}) string { h := md5.New() - if "string" == reflect.TypeOf(v).String() { - h.Write([]byte(v.(string))) - } else { - b, err := json.Marshal(v) - if err != nil { - return "" - } else { - h.Write(b) - } - } + h.Write([]byte(gconv.Bytes(v))) return fmt.Sprintf("%x", h.Sum(nil)) } // 将字符串进行MD5哈希摘要计算 -func EncodeString(v string) string { +func EncryptString(v string) string { h := md5.New() h.Write([]byte(v)) return fmt.Sprintf("%x", h.Sum(nil)) } // 将文件内容进行MD5哈希摘要计算 -func EncodeFile(path string) string { +func EncryptFile(path string) string { f, e := os.Open(path) if e != nil { - glog.Fatalln(e) + return "" } h := md5.New() _, e = io.Copy(h, f) if e != nil { - glog.Fatalln(e) + return "" } return fmt.Sprintf("%x", h.Sum(nil)) } diff --git a/g/encoding/gsha1/gsha1.go b/g/crypto/gsha1/gsha1.go similarity index 75% rename from g/encoding/gsha1/gsha1.go rename to g/crypto/gsha1/gsha1.go index 051a6c9b4..fe95e2e22 100644 --- a/g/encoding/gsha1/gsha1.go +++ b/g/crypto/gsha1/gsha1.go @@ -12,32 +12,32 @@ import ( "encoding/hex" "os" "io" - "gitee.com/johng/gf/g/os/glog" - "gitee.com/johng/gf/g/encoding/gmd5" + "gitee.com/johng/gf/g/util/gconv" ) // 将任意类型的变量进行SHA摘要(注意map等非排序变量造成的不同结果) // 内部使用了md5计算,因此效率会稍微差一些,更多情况请使用 EncodeString -func Encode(v interface{}) string { - return EncodeString(gmd5.Encode(v)) +func Encrypt(v interface{}) string { + r := sha1.Sum(gconv.Bytes(v)) + return hex.EncodeToString(r[:]) } // 对字符串行SHA1摘要计算 -func EncodeString(s string) string { +func EncryptString(s string) string { r := sha1.Sum([]byte(s)) return hex.EncodeToString(r[:]) } // 对文件内容进行SHA1摘要计算 -func EncodeFile(path string) string { +func EncryptFile(path string) string { f, e := os.Open(path) if e != nil { - glog.Fatalln(e) + return "" } h := sha1.New() _, e = io.Copy(h, f) if e != nil { - glog.Fatalln(e) + return "" } return hex.EncodeToString(h.Sum(nil)) } \ No newline at end of file