class Ktct::DigitalEnvelop

Attributes

aes[RW]
algorithm[RW]
key[RW]

Public Class Methods

get() click to toggle source
# File lib/ktct/digital_envelop.rb, line 43
def get
  new('01|%s' % SecureRandom.uuid.gsub(/-/, '')[0, 16])
end
new(content) click to toggle source
# File lib/ktct/digital_envelop.rb, line 8
def initialize(content)
  _, @key = content.split('|')
end

Public Instance Methods

add_padding(content) click to toggle source
# File lib/ktct/digital_envelop.rb, line 30
def add_padding(content)
  if content.bytesize % 16 == 0
    content
  else
    content + "\x00" * (16 - content.bytesize % 16)
  end
end
decrypt(content) click to toggle source
# File lib/ktct/digital_envelop.rb, line 21
def decrypt(content)
  @aes = OpenSSL::Cipher::AES.new(128, :CBC)
  @aes.decrypt
  @aes.padding = 0
  @aes.key = @key
  @aes.iv = @key
  Base64.decode64((@aes.update([content].pack('H*')) + @aes.final).strip).force_encoding('UTF-8')
end
encrypt(content) click to toggle source
# File lib/ktct/digital_envelop.rb, line 12
def encrypt(content)
  @aes = OpenSSL::Cipher::AES.new(128, :CBC)
  @aes.encrypt
  @aes.padding = 0
  @aes.key = @key
  @aes.iv = @key
  (@aes.update(add_padding(Base64.encode64(content))) + @aes.final).unpack('H*').first
end
to_s() click to toggle source
# File lib/ktct/digital_envelop.rb, line 38
def to_s
  "01|#{@key}"
end