Last active
June 3, 2024 09:36
-
-
Save maxclav/3c3aed89839fc8bc6a49e2140f57a8ab to your computer and use it in GitHub Desktop.
Simple Key Generation (using base62 encoded hash) for Tiny URL in Go (GoLang).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tinuURL | |
import ( | |
"crypto/md5" | |
"fmt" | |
"time" | |
"github.com/google/uuid" | |
"github.com/jxskiss/base62" | |
) | |
// GenerateKey generates a new tiny URL key (string) | |
// by hashing the string (md5) and encoding the hash in base62. | |
// | |
// Example: https://go.dev/play/p/tidujUBZ3sY | |
func GenerateKey(str string, size int) string { | |
hash := md5.Sum([]byte(str)) | |
encodedHash := base62.Encode([]byte(hash[:])) | |
return string(encodedHash[:size]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment