Created
March 29, 2018 23:57
-
-
Save spangenberg/a7a3db5d6417fd2e58e52a72b6965f7d to your computer and use it in GitHub Desktop.
UUID Base64
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
#!/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