Sha256: f8714268278d6ac62384b35dc827569ca3d21f8bcfdb0556d28f7e02313bc317

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents


module CcipherFactory
  class SymKeyKeystore
    include TR::CondUtils
    def self.from_encoded(bin, &block)
      
      raise SymKeyCipherError, "Block is required" if not block

      ts = BinStruct.instance.struct_from_bin(bin)
      from_tspec(ts, &block)
    end

    def self.from_tspec(ts, &block)
      
      sk = CcipherFactory::SymKey.from_encoded(ts.symkey_derived) do |ops|
        case ops
        when :password
          block.call(:password)
        end
      end

      dec = CcipherFactory::SymKeyCipher.att_decryptor
      decOut = MemBuf.new
      dec.output(decOut)
      dec.key = sk
      dec.att_decrypt_init
      dec.att_decrypt_update(ts.symkey_cipher)
      dec.att_decrypt_final

      CcipherFactory::SymKey.from_encoded(decOut.bytes)

    end

    def to_keystore(key, &block)
     
      raise SymKeyCipherError, "Key is required" if is_empty?(key)
      raise SymKeyCipherError, "Block is required" if not block

      # 1. Derive session key from user password
      sk = CcipherFactory::SymKeyGenerator.derive(:aes, 256) do |ops|
        case ops
        when :password
          pass = block.call(:password)
          if is_empty?(pass)
            raise SymKeyCipherError, "Password is required" 
          end
          pass
        end
      end
    
      # 2. Encrypt the given key with session key
      enc = CcipherFactory::SymKeyCipher.att_encryptor 
      enc.mode = :gcm
      enc.key = sk

      encOut = MemBuf.new
      enc.output(encOut)

      key.attach_mode

      enc.att_encrypt_init
      enc.att_encrypt_update(key.encoded)
      enc.att_encrypt_final

      ts = BinStruct.instance.struct(:symkey_keystore)
      ts.symkey_derived = sk.encoded
      ts.symkey_cipher = encOut.bytes
      ts.symkey = "testing"
      ts.encoded

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccipher_factory-0.1.2 lib/ccipher_factory/symkey_keystore/symkey_keystore.rb