| 1 | // |
|---|---|
| 2 | // hmacmd5.cpp |
| 3 | // |
| 4 | // This sample demonstrates the HMACEngine class. |
| 5 | // |
| 6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/HMACEngine.h" |
| 14 | #include "Poco/MD5Engine.h" |
| 15 | #include "Poco/DigestStream.h" |
| 16 | #include "Poco/StreamCopier.h" |
| 17 | #include <fstream> |
| 18 | #include <iostream> |
| 19 | |
| 20 | |
| 21 | using Poco::DigestEngine; |
| 22 | using Poco::HMACEngine; |
| 23 | using Poco::MD5Engine; |
| 24 | using Poco::DigestOutputStream; |
| 25 | using Poco::StreamCopier; |
| 26 | |
| 27 | |
| 28 | int main(int argc, char** argv) |
| 29 | { |
| 30 | if (argc != 3) |
| 31 | { |
| 32 | std::cout << "usage: "<< argv[0] << ": <passphrase> <input_file>"<< std::endl |
| 33 | << " create the HMAC-MD5 for <input_file>, using <passphrase>"<< std::endl; |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | std::string passphrase(argv[1]); |
| 38 | |
| 39 | std::ifstream istr(argv[2], std::ios::binary); |
| 40 | if (!istr) |
| 41 | { |
| 42 | std::cerr << "cannot open input file: "<< argv[2] << std::endl; |
| 43 | return 2; |
| 44 | } |
| 45 | |
| 46 | HMACEngine<MD5Engine> hmac(passphrase); |
| 47 | DigestOutputStream dos(hmac); |
| 48 | |
| 49 | StreamCopier::copyStream(istr, dos); |
| 50 | dos.close(); |
| 51 | |
| 52 | std::cout << DigestEngine::digestToHex(hmac.digest()) << std::endl; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 |