diff options
author | Keith Isdale <keith.isdale@nokia.com> | 2010-07-26 14:56:53 +1000 |
---|---|---|
committer | Keith Isdale <keith.isdale@nokia.com> | 2010-07-26 14:56:53 +1000 |
commit | 9f034793bcfc51c2b7c1dd14db806f7258f9a9eb (patch) | |
tree | 63bd0f50ce5b77828ad8205eafd7b9412810499e /botan/doc/examples/encrypt2.cpp | |
parent | 619d92cfef29e653bfdf852e83888e50cfc4348f (diff) | |
parent | 65271649dbc90f3af1184ad1b23bdb64c0c07d07 (diff) |
Merge branch 'master' of git://git-nokia.trolltech.com.au/qtsoftware/research/qtuitest
Diffstat (limited to 'botan/doc/examples/encrypt2.cpp')
-rw-r--r-- | botan/doc/examples/encrypt2.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/botan/doc/examples/encrypt2.cpp b/botan/doc/examples/encrypt2.cpp new file mode 100644 index 0000000..4af0cf0 --- /dev/null +++ b/botan/doc/examples/encrypt2.cpp @@ -0,0 +1,55 @@ +#include <botan/botan.h> +#include <botan/pbkdf2.h> +#include <botan/hmac.h> +#include <botan/sha160.h> + +#include <fstream> + +using namespace Botan; + +int main() + { + Botan::LibraryInitializer init; + + AutoSeeded_RNG rng; + + std::string passphrase = "secret"; + + std::ifstream infile("readme.txt"); + std::ofstream outfile("readme.txt.enc"); + + PKCS5_PBKDF2 pbkdf2(new HMAC(new SHA_160)); + + pbkdf2.set_iterations(4096); + pbkdf2.new_random_salt(rng, 8); + SecureVector<byte> the_salt = pbkdf2.current_salt(); + + SecureVector<byte> master_key = pbkdf2.derive_key(48, passphrase).bits_of(); + + KDF* kdf = get_kdf("KDF2(SHA-1)"); + + SymmetricKey key = kdf->derive_key(20, master_key, "cipher key"); + SymmetricKey mac_key = kdf->derive_key(20, master_key, "hmac key"); + InitializationVector iv = kdf->derive_key(8, master_key, "cipher iv"); + + Pipe pipe(new Fork( + new Chain( + get_cipher("Blowfish/CBC/PKCS7", key, iv, ENCRYPTION), + new Base64_Encoder, + new DataSink_Stream(outfile) + ), + new Chain( + new MAC_Filter("HMAC(SHA-1)", mac_key), + new Hex_Encoder) + ) + ); + + outfile.write((const char*)the_salt.begin(), the_salt.size()); + + pipe.start_msg(); + infile >> pipe; + pipe.end_msg(); + + SecureVector<byte> hmac = pipe.read_all(1); + outfile.write((const char*)hmac.begin(), hmac.size()); + } |