Created
September 12, 2013 19:53
-
-
Save scotttam/6542931 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'base64' | |
require 'digest' | |
require 'openssl' | |
def encode(cryptkey, iv, cleardata) | |
cipher = OpenSSL::Cipher.new('AES-256-CBC') | |
cipher.encrypt # set cipher to be encryption mode | |
cipher.key = cryptkey | |
cipher.iv = iv | |
encrypted = '' | |
encrypted << cipher.update(cleardata) | |
encrypted << cipher.final | |
encrypted | |
end | |
def b64enc(data) | |
Base64.encode64(data).gsub(/\n/, '') | |
end | |
cryptkey = Digest::SHA256.digest('Nixnogen') | |
iv = 'a2xhcgAAAAAAAAAA' | |
buf = "Here is some data for the coding" # 32 chars | |
enc = encode(cryptkey, iv, buf) | |
puts "Encoded length: #{enc.length}" | |
puts "Encoded in Base64: " + b64enc(enc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment