Last active
August 29, 2015 13:56
-
-
Save sentientmonkey/9300069 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class FriendlyGuid | |
attr_reader :mask | |
def initialize(mask) | |
@mask = mask | |
end | |
def guid(id) | |
(id.to_i ^ mask).to_s(36) | |
end | |
def id(guid) | |
guid.to_i(36) ^ mask | |
end | |
end | |
if __FILE__ == $0 | |
mask = Random.new.rand(2**64) | |
id = Random.new.rand(2**64) | |
f = FriendlyGuid.new(mask) | |
puts "mask = #{mask}" | |
puts "id = #{id}" | |
g = f.guid(id) | |
puts "guid = #{g}" | |
puts "id = #{f.id(g)}" | |
end |
This file contains hidden or 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
mask = 1000723141368662252 | |
id = 2465229093246480629 | |
guid = q6parhf03kt5 | |
id = 2465229093246480629 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment