2013-03-05 07:36:32 +01:00
|
|
|
|
2008-01-07 09:09:04 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2013-03-05 07:36:32 +01:00
|
|
|
* Copyright (c) 2013 Valient Gough
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
2013-03-05 07:36:32 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
* later version.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
2013-03-05 07:36:32 +01:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
2012-10-03 07:12:17 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-07 09:09:04 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
#include "cipher/CipherKey.h"
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
#include "base/shared_ptr.h"
|
|
|
|
#include "base/types.h"
|
|
|
|
#include "cipher/MemoryPool.h"
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
CipherKey::CipherKey()
|
|
|
|
: _valid(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherKey::CipherKey(int length)
|
|
|
|
: _valid(true)
|
|
|
|
{
|
|
|
|
if (length > 0)
|
|
|
|
_mem.reset(new SecureMem(length));
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherKey::CipherKey(const byte *data, int length)
|
|
|
|
: _valid(true)
|
|
|
|
{
|
|
|
|
_mem.reset(new SecureMem(length));
|
2013-03-06 09:02:23 +01:00
|
|
|
memcpy(_mem->data(), data, length);
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CipherKey::CipherKey(const CipherKey& src)
|
|
|
|
: _valid(src._valid),
|
|
|
|
_mem(src._mem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CipherKey::~CipherKey()
|
2008-01-07 09:09:04 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
void CipherKey::operator = (const CipherKey& src)
|
2008-01-07 09:09:04 +01:00
|
|
|
{
|
2013-03-05 07:36:32 +01:00
|
|
|
_mem = src._mem;
|
|
|
|
_valid = src._valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte *CipherKey::data() const
|
|
|
|
{
|
2013-03-06 09:02:23 +01:00
|
|
|
return !_mem ? NULL : _mem->data();
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CipherKey::size() const
|
|
|
|
{
|
2013-03-06 09:02:23 +01:00
|
|
|
return !_mem ? 0 : _mem->size();
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CipherKey::reset()
|
|
|
|
{
|
|
|
|
_mem.reset();
|
|
|
|
_valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CipherKey::valid() const
|
|
|
|
{
|
|
|
|
return _valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator == (const CipherKey &a, const CipherKey &b) {
|
|
|
|
if (a.size() != b.size())
|
|
|
|
return false;
|
|
|
|
return memcmp(a.data(), b.data(), a.size()) == 0;
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
} // namespace encfs
|
|
|
|
|