Skip to content

Instantly share code, notes, and snippets.

@spangenberg
Created March 29, 2018 23:57
Show Gist options
  • Save spangenberg/a7a3db5d6417fd2e58e52a72b6965f7d to your computer and use it in GitHub Desktop.
Save spangenberg/a7a3db5d6417fd2e58e52a72b6965f7d to your computer and use it in GitHub Desktop.
UUID Base64
#!/usr/bin/ruby
require "securerandom"
require "base64"
100.times do
uuid = SecureRandom.uuid
puts " UUID: #{uuid.inspect}"
puts " HEX: #{(hex = uuid.gsub("-", "")).inspect}"
puts " BYTES: #{(bytes = [hex].pack('H*')).inspect}"
base64 = Base64.urlsafe_encode64(bytes, padding: false)
puts "BASE64: #{base64.inspect}"
puts " BYTES: #{(bytes2 = Base64.urlsafe_decode64(base64)).inspect}"
puts " HEX: #{(hex2 = bytes2.unpack("H*")[0]).inspect}"
puts " UUID: #{(uuid2 = [hex2[0..7], hex2[8..11], hex2[12..15], hex2[16..19], hex2[20..31]].join("-")).inspect}"
puts
raise unless uuid == uuid2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment