diff options
| author | Ryan Mehri <[email protected]> | 2020-05-09 14:59:25 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-09 14:59:25 -0600 |
| commit | 65f1d4b86b419fa4631f81038c38306b7f37441e (patch) | |
| tree | 57022980cf53860c3d19cb11684a794c78938701 | |
| parent | outline insert op (diff) | |
| parent | Add hashing function (diff) | |
| download | ctrl-v-65f1d4b86b419fa4631f81038c38306b7f37441e.tar.xz ctrl-v-65f1d4b86b419fa4631f81038c38306b7f37441e.zip | |
Merge pull request #1 from jackyzha0/hash
Add hashing function
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | hashing/hash.go | 26 |
2 files changed, 28 insertions, 1 deletions
@@ -1 +1,2 @@ -.env
\ No newline at end of file +.env +.idea
\ No newline at end of file diff --git a/hashing/hash.go b/hashing/hash.go new file mode 100644 index 0000000..400659e --- /dev/null +++ b/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 |