本文整理汇总了C++中SHA256类的典型用法代码示例。如果您正苦于以下问题:C++ SHA256类的具体用法?C++ SHA256怎么用?C++ SHA256使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SHA256类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_hash
void TestApp::test_hash(const SHA256 &sha256, const char *hash_text)
{
std::string hash = sha256.get_hash(true);
if (hash != hash_text)
fail();
if (strlen(hash_text) != 64)
fail();
unsigned char out_hash[32];
sha256.get_hash(out_hash);
for (int cnt=0; cnt<32; cnt++)
{
unsigned int value = out_hash[cnt];
char nibble_high = *(hash_text++);
char nibble_low = *(hash_text++);
nibble_high >= 'A' ? (nibble_high = nibble_high - 'A' + 10) : nibble_high = nibble_high - '0';
nibble_low >= 'A' ? (nibble_low = nibble_low - 'A' + 10) : nibble_low = nibble_low - '0';
if (value != ((nibble_high << 4) | (nibble_low) ))
fail();
}
}
示例2: getMacAddress
QVector<QString> & PBSKeygen::getKeys() {
SHA256 sha;
QString macS = getMacAddress();
if (macS.length() != 12) {
throw ERROR;
}
char mac[6];
bool status;
for (int i = 0; i < 12; i += 2)
mac[i / 2] = (macS.mid(i, 1).toInt(&status, 16) << 4)
+ macS.mid(i + 1, 1).toInt(&status, 16);
if (!status)
throw ERROR;
sha.reset();
sha.addData(saltSHA256, (unsigned long)sizeof(saltSHA256));
sha.addData(mac, (unsigned long)sizeof(mac));
unsigned char hash[32];
sha.result((unsigned char *) hash);
QString key = "";
for (int i = 0; i < 13; ++i) {
key.append(lookup.at(hash[i] % lookup.length()));
}
results.append(key);
return results;
}
示例3: lock
//---------------------------------------------------------------------
bool PeerContactProfile::usesContactProfileSecret(const char *contactProfileSecret)
{
AutoRecursiveLock lock(mLock);
if (NULL == contactProfileSecret) return false;
if (!mDocument) return false;
try {
ElementPtr contactProfileElement = getContactProfileElement();
if (!contactProfileElement) return false;
ElementPtr proofElement = contactProfileElement->findFirstChildElementChecked("private")->findFirstChildElementChecked("proof");
String proofAsBase64 = proofElement->getText(true);
SecureByteBlock proofHash;
convertFromBase64(proofAsBase64, proofHash);
SecureByteBlock calculatedProofHash(32);
if (calculatedProofHash.size() != proofHash.size()) return false;
SHA256 shaProof;
shaProof.Update((const BYTE *)"proof:", strlen("proof:"));
shaProof.Update((const BYTE *)contactProfileSecret, strlen(contactProfileSecret));
shaProof.Final(calculatedProofHash);
if (0 != memcmp(calculatedProofHash, proofHash, proofHash.size())) return false;
// this is the secret and it is verified
mContactProfileSecret = contactProfileSecret;
} catch (zsLib::XML::Exceptions::CheckFailed &) {
return false;
}
return true;
}
示例4: EncryptPassword
// Non-public secure PBKDF2 hash function with salting and 1,337 iterations
std::string JSI_Lobby::EncryptPassword(const std::string& password, const std::string& username)
{
const int DIGESTSIZE = SHA_DIGEST_SIZE;
const int ITERATIONS = 1337;
static const unsigned char salt_base[DIGESTSIZE] = {
244, 243, 249, 244, 32, 33, 34, 35, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 32, 33, 244, 224, 127, 129, 130, 140, 153, 133, 123, 234, 123 };
// initialize the salt buffer
unsigned char salt_buffer[DIGESTSIZE] = {0};
SHA256 hash;
hash.update(salt_base, sizeof(salt_base));
hash.update(username.c_str(), username.length());
hash.finish(salt_buffer);
// PBKDF2 to create the buffer
unsigned char encrypted[DIGESTSIZE];
pbkdf2(encrypted, (unsigned char*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS);
static const char base16[] = "0123456789ABCDEF";
char hex[2 * DIGESTSIZE];
for (int i = 0; i < DIGESTSIZE; ++i)
{
hex[i*2] = base16[encrypted[i] >> 4]; // 4 high bits
hex[i*2 + 1] = base16[encrypted[i] & 0x0F]; // 4 low bits
}
return std::string(hex, sizeof(hex));
}
示例5: clip
/**
* Clear from clipboard data, that we put there previously
* @return \c true, if we cleared our data, or stored data don't belong to us
*/
bool PWSclipboard::ClearCBData()
{
wxMutexLocker clip(m_clipboardMutex);
if (m_set && wxTheClipboard->Open()) {
wxTextDataObject obj;
if (wxTheClipboard->IsSupported(wxDF_UNICODETEXT) && wxTheClipboard->GetData(obj)) {
StringX buf(obj.GetText().data(), obj.GetText().size());
if (buf.length()) {
// check if the data on the clipboard is the same we put there
unsigned char digest[SHA256::HASHLEN];
SHA256 ctx;
ctx.Update(reinterpret_cast<const unsigned char *>(buf.c_str()), buf.length()*sizeof(wchar_t));
ctx.Final(digest);
if (memcmp(digest, m_digest, SHA256::HASHLEN) == 0) {
// clear & reset
wxTheClipboard->Clear();
memset(m_digest, 0, SHA256::HASHLEN);
m_set = false;
// Also trash data in buffer and clipboard somehow?
pws_os::Trace0(L"Cleared our data from buffer.\n");
}
else{
pws_os::Trace0(L"Buffer doesn't contain our data. Nothing to clear.\n");
}
}
}
wxTheClipboard->Close();
}
return !m_set;
}
示例6: ConvertFromBase58ShaSquare
Blob ConvertFromBase58ShaSquare(RCString s) {
BigInteger bi = 0;
for (const char *p=s; *p; ++p) {
if (const char *q = strchr(s_pszBase58, *p)) {
bi = bi*58 + BigInteger(q-s_pszBase58);
} else
Throw(E_INVALIDARG);
}
vector<byte> v((bi.Length+7)/8);
bi.ToBytes(&v[0], v.size());
if (v.size()>=2 && v.end()[-1]==0 && v.end()[-1]>=0x80)
v.resize(v.size()-1);
vector<byte> r;
for (const char *p=s; *p==s_pszBase58[0]; ++p)
r.push_back(0);
r.resize(r.size()+v.size());
std::reverse_copy(v.begin(), v.end(), r.end()-v.size());
if (r.size() < 4)
Throw(E_FAIL);
SHA256 sha;
HashValue hash = HashValue(sha.ComputeHash(sha.ComputeHash(ConstBuf(&r[0], r.size()-4))));
if (memcmp(hash.data(), &r.end()[-4], 4))
Throw(HRESULT_FROM_WIN32(ERROR_CRC));
return Blob(&r[0], r.size()-4);
}
示例7: sha256_test
int sha256_test()
{
SHA256 sha;
byte hash[SHA256::DIGEST_SIZE];
testVector test_sha[] =
{
testVector("abc",
"\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
"\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
"\x15\xAD"),
testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
"\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
"\x06\xC1")
};
int times( sizeof(test_sha) / sizeof(testVector) );
for (int i = 0; i < times; ++i) {
sha.Update(test_sha[i].input_, test_sha[i].inLen_);
sha.Final(hash);
if (memcmp(hash, test_sha[i].output_, SHA256::DIGEST_SIZE) != 0)
return -1 - i;
}
return 0;
}
示例8: IncorporateEntropy
void RandomPool::IncorporateEntropy(const byte *input, size_t length)
{
SHA256 hash;
hash.Update(m_key, 32);
hash.Update(input, length);
hash.Final(m_key);
m_keySet = false;
}
示例9: dhAgree
bool provider::dhAgree(byte_block& agree, const byte_block& priv, const byte_block& pub)
{
SHA256 hasher;
byte_block _agree(dh_agree_block_size);
if (!dh.Agree(_agree, priv, pub))
return false;
assert(_agree.SizeInBytes() == dh_agree_block_size);
hasher.CalculateDigest(agree, _agree, dh_agree_block_size);
return true;
}
示例10: ASSERT
void PWSrand::AddEntropy(unsigned char *bytes, unsigned int numBytes)
{
ASSERT(bytes != nullptr);
SHA256 s;
s.Update(K, sizeof(K));
s.Update(bytes, numBytes);
s.Final(K);
}
示例11: sizeof
void PWSrand::NextRandBlock()
{
SHA256 s;
s.Update(K, sizeof(K));
s.Final(R);
unsigned int *Kp = reinterpret_cast<unsigned int *>(K);
unsigned int *Rp = reinterpret_cast<unsigned int *>(R);
const int N = SHA256::HASHLEN / sizeof(uint32);
Kp[0]++;
for (int32 i = 0; i < N; i++)
Kp[i] += Rp[i];
}
示例12: getHash
const std::string getHash(std::string fname){
std::ifstream f;
f.open(fname.c_str());
SHA256 digest;
const size_t BufferSize = 144 * 7 * 1024;
char * buffer = new char[BufferSize];
if (f != NULL){
f.read(buffer, BufferSize);//read(char *s, streamsize n)
std::size_t numByte = size_t(f.gcount());//gcount(): return the number of characters extracted by the last unformatted input operation
digest.add(buffer, numByte);
}
f.close();
delete[] buffer;
return digest.getHash();
}
示例13:
void PWSfile::HashRandom256(unsigned char *p256)
{
/**
* This is for random data that will be written to the file directly.
* The idea is to avoid directly exposing our generated randomness
* to the attacker, since this might expose state of the RNG.
* Therefore, we'll hash the randomness.
*
* As the names imply, this works on 256 bit (32 byte) arrays.
*/
PWSrand::GetInstance()->GetRandomData(p256, 32);
SHA256 salter;
salter.Update(p256, 32);
salter.Final(p256);
}
示例14: ibRandomData
PWSrand::PWSrand()
: ibRandomData(SHA256::HASHLEN)
{
m_IsInternalPRNG = !pws_os::InitRandomDataFunction();
SHA256 s;
unsigned slen = 0;
unsigned char *p;
pws_os::GetRandomSeed(nullptr, slen);
p = new unsigned char[slen];
pws_os::GetRandomSeed(p, slen);
s.Update(p, slen);
delete[] p;
s.Final(K);
}
示例15: sha256
std::string sha256(const std::string& input)
{
SHA256::uint8 digest[SHA256::DIGEST_SIZE];
memset(digest,0,SHA256::DIGEST_SIZE);
SHA256 ctx = SHA256();
ctx.init();
ctx.update( (SHA256::uint8*)input.c_str(), input.length());
ctx._final(digest);
char buf[2*SHA256::DIGEST_SIZE+1];
buf[2*SHA256::DIGEST_SIZE] = 0;
for (int i = 0; i < SHA256::DIGEST_SIZE; i++)
sprintf(buf+i*2, "%02x", digest[i]);
return std::string(buf);
}