设为首页 加入收藏

TOP

POCO C++库学习和分析 -- 随机数和数字摘要 (二)
2014-11-24 03:12:45 】 浏览:10118
Tags:POCO 学习 分析 随机 数字 摘要
igestEngine::Digest& digest = hmac.digest();
// finish HMAC computation and obtain digest
std::string digestString(DigestEngine::digestToHex(digest));
// convert to a string of hexadecimal numbers
return 0;
}

#include "Poco/HMACEngine.h"
#include "Poco/SHA1Engine.h"
using Poco::DigestEngine;
using Poco::HMACEngine;
using Poco::SHA1Engine;
int main(int argc, char** argv)
{
std::string message1("This is a top-secret message.");
std::string message2("Don't tell anyone!");
std::string passphrase("s3cr3t"); // HMAC needs a passphrase
HMACEngine hmac(passphrase); // we'll compute a HMAC-SHA1
hmac.update(message1);
hmac.update(message2);
const DigestEngine::Digest& digest = hmac.digest();
// finish HMAC computation and obtain digest
std::string digestString(DigestEngine::digestToHex(digest));
// convert to a string of hexadecimal numbers
return 0;
}


2.3 与DigestEngine类相关的流(DigestInputStream/DigestOutputStream)
可以通过Poco::DigestInputStream和Poco::DigestOutputStream类对DigestEngine类进行输入输出操作。过程很简单,只要在在构造Stream时,把相关的DigestEngine类传入即可。需要注意的是,在向DigestOutputStream类写入后,要及时调用flush函数,以确保Stream把所有数据都输入进DigestEngine类。

下面是相关的一个例子:

[cpp]
#include "Poco/DigestStream.h"
#include "Poco/MD5Engine.h"
using Poco::DigestOutputStream;
using Poco::DigestEngine;
using Poco::MD5Engine;
int main(int argc, char** argv)
{
MD5Engine md5;
DigestOutputStream ostr(md5);
ostr << "This is some text";
ostr.flush(); // Ensure everything gets passed to the digest engine
const DigestEngine::Digest& digest = md5.digest(); // obtain result
std::string result = DigestEngine::digestToHex(digest);
return 0;
}

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HDU2317:Nasty Hacks 下一篇130407周赛

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目