Skip to content

Instantly share code, notes, and snippets.

@mguezuraga
Created January 6, 2017 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mguezuraga/1d71cbfbaa516ef956d4108ad9bae43f to your computer and use it in GitHub Desktop.
Save mguezuraga/1d71cbfbaa516ef956d4108ad9bae43f to your computer and use it in GitHub Desktop.
require 'openssl'
require 'digest/sha1'
require 'base64'
@key = Digest::SHA1.hexdigest('74cf0e9a3c2b904e9c445771cdccb3e6ce3e4da7')
@cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
def encrypt(data)
@cipher.encrypt
@cipher.key = @key
rc = @cipher.update(data)
rc << @cipher.final
return rc
end
def decrypt(data)
@cipher.decrypt
@cipher.key = @key
rc = @cipher.update(Base64::decode64(data))
rc << @cipher.final
return rc
end
token_txt = "serveradmin:bob:1234567"
token = encrypt(token_txt)
token64 = Base64::encode64(token).strip.delete("\n")
puts "Encrypted text (base64): #{token64}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment