ls 5 years ago
parent
commit
383614bcdb
1 changed files with 41 additions and 0 deletions
  1. 41 0
      hash.go

+ 41 - 0
hash.go

@@ -0,0 +1,41 @@
+package tyr
+
+import (
+	"crypto/md5"
+	"crypto/sha1"
+	"crypto/sha256"
+	"crypto/sha512"
+	"encoding/hex"
+)
+
+// SHA512 hash
+func SHA512(data []byte) ([]byte, error) {
+	hash := sha512.New()
+	_, err := hash.Write(data)
+	if err != nil {
+		return nil, err
+	}
+
+	return hash.Sum(nil), nil
+}
+
+// SHA256 hash string
+func SHA256(data string) string {
+	hash := sha256.New()
+	hash.Write([]byte(data))
+	return hex.EncodeToString(hash.Sum(nil))
+}
+
+// MD5 hash string
+func MD5(data string) string {
+	hash := md5.New()
+	hash.Write([]byte(data))
+	return hex.EncodeToString(hash.Sum(nil))
+}
+
+// SHA1 hash string
+func SHA1(data string) string {
+	hash := sha1.New()
+	hash.Write([]byte(data))
+	return hex.EncodeToString(hash.Sum(nil))
+}