/*****************************************************************************\
*                                                                             *
*  Name   : key_user                                                          *
*  Author : Chris Koeritz                                                     *
*                                                                             *
*******************************************************************************
* Copyright (c) 2002-$now By Author.  This program is free software; you can  *
* redistribute it and/or modify it under the terms of the GNU General Public  *
* License as published by the Free Software Foundation; either version 2 of   *
* the License or (at your option) any later version.  This is online at:      *
*     http://www.fsf.org/copyleft/gpl.html                                    *
* Please send any updates to: fred@gruntose.com                               *
\*****************************************************************************/

#include "ice_key.h"
#include "key_user.h"

#include <basis/byte_array.h>

key_user::key_user()
: _stage(0),
  _key(new byte_array),
  _crypt(NIL)
{}

key_user::~key_user()
{
  WHACK(_key);
  WHACK(_crypt);
}

byte_array &key_user::current_key() const { return *_key; }

void key_user::update(int new_stage, const byte_array &new_key)
{
  _stage = new_stage;
  *_key = new_key;
  WHACK(_crypt);
  _crypt = new ice_key(_key->length() / 8);
  _crypt->set(*_key);
}

bool key_user::encrypt(const byte_array &to_encrypt, byte_array &encrypted)
{
  if (!_crypt) return false;  
  return _crypt->encrypt(to_encrypt, encrypted);
}

bool key_user::decrypt(const byte_array &to_decrypt, byte_array &decrypted)
{
  if (!_crypt) return false;  
  return _crypt->decrypt(to_decrypt, decrypted);
}

