pgp_s2k.cpp
来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 67 行
CPP
67 行
/************************************************** OpenPGP S2K Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/pgp_s2k.h>#include <botan/lookup.h>namespace Botan {/************************************************** Derive a key using the OpenPGP S2K algorithm **************************************************/SymmetricKey OpenPGP_S2K::derive_key(const std::string& passphrase, u32bit keylen) const { static const byte ZERO_BYTE[1] = { 0 }; SecureVector<byte> key(keylen), hash_buf; u32bit pass = 0, generated = 0, total_size = passphrase.size() + salt.size(); u32bit to_hash = std::max(iterations(), total_size); hash->clear(); while(keylen > generated) { for(u32bit j = 0; j != pass; j++) hash->update(ZERO_BYTE, 1); u32bit left = to_hash; while(left >= total_size) { hash->update(salt, salt.size()); hash->update(passphrase); left -= total_size; } if(left <= salt.size()) hash->update(salt, left); else { hash->update(salt, salt.size()); left -= salt.size(); hash->update((const byte*)passphrase.c_str(), left); } hash_buf = hash->final(); key.copy(generated, hash_buf, hash->OUTPUT_LENGTH); generated += hash->OUTPUT_LENGTH; pass++; } return SymmetricKey(key, key.size()); }/************************************************** OpenPGP S2K Constructor **************************************************/OpenPGP_S2K::OpenPGP_S2K(const std::string& h, u32bit i, u32bit salt_size) : S2K(true) { hash = get_hash(h); set_iterations(i); if(salt_size) new_random_salt(salt_size); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?