Skip to content

Instantly share code, notes, and snippets.

@elias19r
Last active June 22, 2022 03:42
Show Gist options
  • Save elias19r/4c8f804a9f1d6e73ffe56c6393a3c91b to your computer and use it in GitHub Desktop.
Save elias19r/4c8f804a9f1d6e73ffe56c6393a3c91b to your computer and use it in GitHub Desktop.
Encode/decode UUID string to/from Base64 string in Ruby
require 'base64'
class UUID
def self.encode64(uuid)
bytes_str = [uuid.delete('-')].pack('H*')
Base64.urlsafe_encode64(bytes_str, padding: false)
end
def self.decode64(base64)
bytes_array = Base64.urlsafe_decode64(base64).unpack('NnnnnN')
'%08x-%04x-%04x-%04x-%04x%08x' % bytes_array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment