Skip to content

Instantly share code, notes, and snippets.

@rbmrclo
Created November 27, 2013 08:11
Show Gist options
  • Save rbmrclo/7672241 to your computer and use it in GitHub Desktop.
Save rbmrclo/7672241 to your computer and use it in GitHub Desktop.
Obfuscate an id
module Obfuscate
def cipher
OpenSSL::Cipher::Cipher.new('aes-256-cbc')
end
def cipher_key
'any string that you want'
end
def deobfuscate(value)
c = cipher.decrypt
c.key = Digest::SHA256.digest(cipher_key)
c.update(Base64.decode64(value.to_s)) + c.final
end
def obfuscate(value)
c = cipher.encrypt
c.key = Digest::SHA256.digest(cipher_key)
Base64.encode64(c.update(value.to_s) + c.final).squish
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment