diff options
| author | Ryan Mehri <[email protected]> | 2020-05-09 21:17:40 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-09 21:17:40 -0600 |
| commit | 14e120bc3fa4da442af0063c1a00852e67fbc6a2 (patch) | |
| tree | 9258832576f9c1bcad6126cf76ef8f7f6a128e82 /backend/hashing | |
| parent | Merge pull request #3 from jackyzha0/doc-expiry (diff) | |
| parent | folder refactor (diff) | |
| download | ctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.tar.xz ctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.zip | |
Merge pull request #4 from jackyzha0/folder-refactor
folder refactor
Diffstat (limited to 'backend/hashing')
| -rw-r--r-- | backend/hashing/hash.go | 26 |
1 files changed, 26 insertions, 0 deletions
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 |