Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sentientmonkey/9300069 to your computer and use it in GitHub Desktop.
Save sentientmonkey/9300069 to your computer and use it in GitHub Desktop.
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
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