From 488b1aa78b21b12b998816a8907879148dc925ed Mon Sep 17 00:00:00 2001 From: john Date: Wed, 8 Aug 2018 10:05:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=8A=A0=E5=AF=86/=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84=E5=8C=85=E4=BB=8Eencoding=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E8=BF=81=E7=A7=BB=E5=88=B0crypto=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E4=B8=8B=EF=BC=8C=E5=B9=B6=E6=94=B9=E8=BF=9B=E5=8C=85=E4=B8=AD?= =?UTF-8?q?=E7=9A=84interface{]=E5=8F=82=E6=95=B0=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/{encoding => crypto}/gcrc32/gcrc32.go | 4 ++-- g/{encoding => crypto}/gdes/gdes.go | 0 g/{encoding => crypto}/gdes/gdes_test.go | 2 +- g/{encoding => crypto}/gmd5/gmd5.go | 25 +++++++----------------- g/{encoding => crypto}/gsha1/gsha1.go | 16 +++++++-------- 5 files changed, 18 insertions(+), 29 deletions(-) rename g/{encoding => crypto}/gcrc32/gcrc32.go (84%) rename g/{encoding => crypto}/gdes/gdes.go (100%) rename g/{encoding => crypto}/gdes/gdes_test.go (99%) rename g/{encoding => crypto}/gmd5/gmd5.go (64%) rename g/{encoding => crypto}/gsha1/gsha1.go (75%) 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