Last active
June 22, 2022 03:42
-
-
Save elias19r/4c8f804a9f1d6e73ffe56c6393a3c91b to your computer and use it in GitHub Desktop.
Encode/decode UUID string to/from Base64 string in Ruby
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
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