Skip to content

Instantly share code, notes, and snippets.

@oleganza
Forked from Bertrand/gist:239360
Created November 20, 2009 08:56
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 oleganza/239365 to your computer and use it in GitHub Desktop.
Save oleganza/239365 to your computer and use it in GitHub Desktop.
def smallAsciiRepresentation(digest)
digestParts = digest.unpack("Q2") # digest is a 128 bit buffer, cut it into two 64 bits ruby Integer
smallDigestPart = digestParts[0] ^ digestParts[1] # XOR the two 64 bits parts
smallDigest = [smallDigestPart].pack("Q") # and transform the result into a 64 bits binary buffer
smallDigestB64 = [smallDigest].pack("m") # b64-encode the result
smallID = smallDigestB64.gsub(/[\n=]/, '').gsub(/\+/,'-').gsub(/\//,'_') # and make it URL-friendly by shortening it (no '=' at the end, no '+', no '/')
return smallID
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment