Sha256: 3ac60fdc5aa1d879f7b5a9a0ac4b6eeaa4dcd677425fe9ccd2cc5dc0e49ea7f6
Contents?: true
Size: 1.04 KB
Versions: 2
Compression:
Stored size: 1.04 KB
Contents
require 'openssl' require 'base64' require 'securerandom' module Ktct class DigitalEnvelop attr_accessor :aes, :algorithm, :key def initialize(content) _, @key = content.split('|') end 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 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 def add_padding(content) if content.bytesize % 16 == 0 content else content + "\x00" * (16 - content.bytesize % 16) end end def to_s "01|#{@key}" end class << self def get new('01|%s' % SecureRandom.uuid.gsub(/-/, '')[0, 16]) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ktct-0.1.1 | lib/ktct/digital_envelop.rb |
ktct-0.1.0 | lib/ktct/digital_envelop.rb |