From dedabf41a18820527aed9e77b75564e69c9030ce Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Sat, 9 May 2020 20:15:59 -0700 Subject: folder refactor --- backend/hashing/hash.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 backend/hashing/hash.go (limited to 'backend/hashing/hash.go') diff --git a/backend/hashing/hash.go b/backend/hashing/hash.go new file mode 100644 index 0000000..400659e --- /dev/null +++ b/backend/hashing/hash.go @@ -0,0 +1,26 @@ +package hashing + +import ( + "crypto/md5" + "encoding/hex" + "math/big" + "time" +) + +const UrlLength = 7 + +// GenerateURI creates a unique identifier for a paste based on ip and timestamp +func GenerateURI(ip string) string { + timeStamp := time.Now().String() + return hashString(ip + timeStamp)[:UrlLength] +} + +// hashes using MD5 and then converts to base 62 +func hashString(text string) string { + hash := md5.Sum([]byte(text)) + hexStr := hex.EncodeToString(hash[:]) + + bi := big.NewInt(0) + bi.SetString(hexStr, 16) + return bi.Text(62) +} \ No newline at end of file -- cgit v1.2.3