Skip to content

Instantly share code, notes, and snippets.

@maxclav
Last active June 3, 2024 09:36
Show Gist options
  • Save maxclav/3c3aed89839fc8bc6a49e2140f57a8ab to your computer and use it in GitHub Desktop.
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).
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