Browse Source

Throw some more exceptions.

cl-refactor
chriseth 10 years ago
parent
commit
58e7640850
  1. 5
      libdevcore/CommonIO.cpp
  2. 8
      libdevcore/CommonIO.h
  3. 5
      libdevcrypto/Common.cpp
  4. 35
      libdevcrypto/Exceptions.h
  5. 10
      libdevcrypto/SecretStore.cpp
  6. 3
      libdevcrypto/SecretStore.h

5
libdevcore/CommonIO.cpp

@ -95,11 +95,12 @@ string dev::contentsString(string const& _file)
return contentsGeneric<string>(_file);
}
bool dev::writeFile(std::string const& _file, bytesConstRef _data)
void dev::writeFile(std::string const& _file, bytesConstRef _data)
{
ofstream s(_file, ios::trunc | ios::binary);
s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
return !!s;
if (!s)
BOOST_THROW_EXCEPTION(FileError());
}
std::string dev::getPassword(std::string const& _prompt)

8
libdevcore/CommonIO.h

@ -56,11 +56,11 @@ std::string contentsString(std::string const& _file);
bytesRef contentsNew(std::string const& _file, bytesRef _dest = bytesRef());
/// Write the given binary data into the given file, replacing the file if it pre-exists.
/// @returns true if writing succeeded.
bool writeFile(std::string const& _file, bytesConstRef _data);
/// Throws exception on error.
void writeFile(std::string const& _file, bytesConstRef _data);
/// Write the given binary data into the given file, replacing the file if it pre-exists.
inline bool writeFile(std::string const& _file, bytes const& _data) { return writeFile(_file, bytesConstRef(&_data)); }
inline bool writeFile(std::string const& _file, std::string const& _data) { return writeFile(_file, bytesConstRef(_data)); }
inline void writeFile(std::string const& _file, bytes const& _data) { writeFile(_file, bytesConstRef(&_data)); }
inline void writeFile(std::string const& _file, std::string const& _data) { writeFile(_file, bytesConstRef(_data)); }
/// Nicely renders the given bytes to a string, optionally as HTML.
/// @a _bytes: bytes array to be rendered as string. @a _width of a bytes line.

5
libdevcrypto/Common.cpp

@ -31,6 +31,7 @@
#include <libdevcore/FileSystem.h>
#include "AES.h"
#include "CryptoPP.h"
#include "Exceptions.h"
using namespace std;
using namespace dev;
using namespace dev::crypto;
@ -188,7 +189,7 @@ bytes dev::pbkdf2(string const& _pass, bytes const& _salt, unsigned _iterations,
_salt.size(),
_iterations
) != _iterations)
return bytes();
BOOST_THROW_EXCEPTION(CryptoException());
return ret;
}
@ -206,7 +207,7 @@ bytes dev::scrypt(std::string const& _pass, bytes const& _salt, uint64_t _n, uin
ret.data(),
ret.size()
) != 0)
return bytes();
BOOST_THROW_EXCEPTION(CryptoException());
return ret;
}

35
libdevcrypto/Exceptions.h

@ -0,0 +1,35 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum 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 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Exceptions.h
* @author Christian <c@ethdev.com>
* @date 2016
*/
#pragma once
#include <libdevcore/Exceptions.h>
namespace dev
{
namespace crypto
{
/// Rare malfunction of cryptographic functions.
DEV_SIMPLE_EXCEPTION_RLP(CryptoException);
}
}

10
libdevcrypto/SecretStore.cpp

@ -149,12 +149,10 @@ void SecretStore::save(string const& _keysPath)
v["crypto"] = crypto;
v["id"] = uuid;
v["version"] = c_keyFileVersion;
if (writeFile(filename, js::write_string(js::mValue(v), true)))
{
swap(k.second.filename, filename);
if (!filename.empty() && !fs::equivalent(filename, k.second.filename))
fs::remove(filename);
}
writeFile(filename, js::write_string(js::mValue(v), true));
swap(k.second.filename, filename);
if (!filename.empty() && !fs::equivalent(filename, k.second.filename))
fs::remove(filename);
}
}

3
libdevcrypto/SecretStore.h

@ -40,7 +40,8 @@ enum class KDF {
* and changes to the keys are automatically synced to the directory.
* Each file stores exactly one key in a specific JSON format whose file name is derived from the
* UUID of the key.
* @note that most of the functions here affect the filesystem and throw exceptions on failure.
* @note that most of the functions here affect the filesystem and throw exceptions on failure,
* and they also throw exceptions upon rare malfunction in the cryptographic functions.
*/
class SecretStore
{

Loading…
Cancel
Save