Skip to content

Instantly share code, notes, and snippets.

@xkef
Created January 15, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkef/73b8763e80bd0b67f3f01e1bc2842696 to your computer and use it in GitHub Desktop.
Save xkef/73b8763e80bd0b67f3f01e1bc2842696 to your computer and use it in GitHub Desktop.
url shorteners
#!/bin/sh
# say we have a service shrtnr.io,
# this bcrypt implementation (10 salting rounds)
# gives a unique hash for the short url
# -> shrtnr.io/abcd (abcd - postfix must be choosen with constraints by user)
$ htpasswd -nbBC 10 abcd 'www.google.com'
# out: abcd:$2y$10$SkjTJYoRgELA7E1dZe11KOB4cT0DsYxbqZGIBJTwZSHbbx.TPnAZq
# bcrypt hash of www.google.com for short '/abcd'
$ echo "$(htpasswd -nbBC 10 abcd 'www.google.com' | sed 's/.*\://g')" | urlenc --enc # urlenc --version -> v1.1 (1386011)
# out: %242y%2410%24%2F.ptThgwxETajDl0vxIHKuyZ0M4MCo.QySqajA9H4v8t7SQXLzJvm
# we would have in our key-value store:
# { "$2y$10$SkjTJYoRgELA7E1dZe11KOB4cT0DsYxbqZGIBJTwZSHbbx.TPnAZq": "abcd" }
# ----
# to give the user rights for deletetion this is easily extensible,
# he just gets a random hash of this which can be verified
$ htpasswd -nbBC 10 "$2y$10$SkjTJYoRgELA7E1dZe11KOB4cT0DsYxbqZGIBJTwZSHbbx.TPnAZq - abcd" "good (internal password) password"
# deletetion secret: "y.TPnAZq:$2y$10$HGFa/jlSArcjPUBCyz05e.GnOx8.U4QEbb7CUeukUDjYUZEtlow1m"
# ---
# To make this scale better and approach the url compression problem,
# I would look at AVL Tree data structures.
# To not always go to your store, use caching - bcrypt is expensive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment