各種メッセージダイジェストアルゴリズムのエンジンのベースクラスとなる Poco::DigestEngine と、それを継承したエンジン Poco::MD2Engine, Poco::MD4Engine, Poco::MD5Engine, Poco::SHA1Engine, Poco::HMACEngine を紹介します。
(上記5種のエンジン以外にも Poco::Crypto::RSADigestEngine があるのですが、Complete Edition にしか入っていない別パッケージなので、ここでは扱いません)
DigestEngineTest.cpp
・Poco::HTTPClientSession の記事の時に使った http://poco.roundsquare.net/downloads/test.txt にある
テキストファイルをダイジェストエンジンに食わせるソースとする。
・予め次のコマンドで作成しておいた結果:
$ openssl dgst -md2 test.txt $ openssl dgst -md4 test.txt $ openssl dgst -md5 test.txt $ openssl dgst -sha1 test.txt $ openssl dgst -hmac Poco test.txt |
のダイジェスト部分だけを、それぞれ
http://poco.roundsquare.net/downloads/test.md2
http://poco.roundsquare.net/downloads/test.md4
http://poco.roundsquare.net/downloads/test.md5
http://poco.roundsquare.net/downloads/test.sha1
http://poco.roundsquare.net/downloads/test.hmac
というファイルにして置いておく。
・ダイジェストエンジンの出力を Poco::DigestEngine::digestToHex で取り出したものと、上記予め
作成されたファイルとを比較し、OK/NG を表示。
(2011.02.27 追記)
・dos.close(); を呼ぶのを忘れていたため、正しい結果になっていなかったのを修正しました。
(2011.08.31 追記)
・Poco v1.4.2 で、Poco::MD2Engine が無くなったのに対応しました。
#include <Poco/URI.h> #include <Poco/TypeList.h> #include <Poco/DigestEngine.h> #ifndef POCO_VERSION #include <Poco/Version.h> #endif #if (POCO_VERSION < 0x01040200) #include <Poco/MD2Engine.h> #endif #include <Poco/MD4Engine.h> #include <Poco/MD5Engine.h> #include <Poco/SHA1Engine.h> #include <Poco/HMACEngine.h> #include <Poco/DigestStream.h> #include <Poco/Format.h> #include <Poco/String.h> #include <string> #include <sstream> #include <vector> #include <utility> #include <algorithm> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" #include "HTTPGetter.h" const char* kPassphrase = "Poco"; class TestDigestEngine { public: TestDigestEngine(ScopedLogMessage& msg, HTTPGetter& getter, const Poco::URI& uri, const char* passphrase=kPassphrase) : m_msg(msg) , m_getter(getter) , m_uri(uri) , m_passphrase(passphrase) { } void operator()(std::pair<Poco::DigestEngine*, std::string>& item) { Poco::DigestOutputStream dos(*item.first); m_getter.Get(m_uri.getPath(), dos); dos.close(); std::string digestStr(Poco::DigestEngine::digestToHex(item.first->digest())); Poco::URI digestUri("http://poco.roundsquare.net/downloads/test."+item.second); HTTPGetter digestgetter(digestUri.getHost(), digestUri.getPort()); std::stringstream ss; digestgetter.Get(digestUri.getPath(), ss); m_msg.Message(Poco::format(" %s: %s [%s]" , Poco::toUpper((4 == item.second.length()) ? item.second:(" "+item.second)) , digestStr , std::string((0 == ss.str().compare(digestStr)) ? "OK":"NG")) ); delete item.first; item.first = NULL; } private: ScopedLogMessage& m_msg; HTTPGetter& m_getter; const Poco::URI& m_uri; const char* m_passphrase; }; int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("DigestEngineTest ", "start", "end"); Poco::URI uri("http://poco.roundsquare.net/downloads/test.txt"); HTTPGetter getter(uri.getHost(), uri.getPort()); std::stringstream ss; getter.Get(uri.getPath(), ss); msg.Message(" source text:"); std::cout << ss.str(); std::vector< std::pair<Poco::DigestEngine*, std::string> > engineVec; #if (POCO_VERSION < 0x01040200) engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD2Engine, "md2")); #endif engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD4Engine, "md4")); engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::MD5Engine, "md5")); engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::SHA1Engine, "sha1")); engineVec.push_back(std::pair<Poco::DigestEngine*, std::string>(new Poco::HMACEngine<Poco::MD5Engine>(kPassphrase), "hmac")); std::for_each(engineVec.begin(), engineVec.end(), TestDigestEngine(msg, getter, uri)); return 0; } |
Results of execution
[0] DigestEngineTest start [0] source text: --- This is a sample text file for HTTPClientSessionTest --- [0] MD4: 61f79367426e3b0f53428c22cf13dd0a [OK] [0] MD5: 1d5acafd189d20e30daca7381e1846a0 [OK] [0] SHA1: 085ba5956bb1420f47229ae410a7760f01320ea5 [OK] [0] HMAC: e72891e5af825ba8bdd7fe3ced51dac9 [OK] [0] DigestEngineTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年5月31日からのダウンロード数:1204
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・http://pocoproject.org にある RandomCrypto のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|