设为首页 加入收藏

TOP

POCO C++库学习和分析 -- 随机数和数字摘要 (一)
2014-11-24 03:12:45 】 浏览:10117
Tags:POCO 学习 分析 随机 数字 摘要

POCO C++库学习和分析 -- 随机数和数字摘要

在程序设计时,有时候我们需要生成随机数和数字摘要。在Poco库中,也提供了上述功能,下面我们一一叙述:

1. 随机数生成
Poco中生成随机数的类为Poco::Random类。它根据PRNG(pseudo random number generator )算法设计,采用了一个累加的非线性反馈算法。PRNG算法可以产生0 ~ 2^31之间的随机数整数。
在接口上Poco::Random提供了一些函数,可以使使用者直接得到其他形式的随机数。如char, bool, float 和 double 类型。另外Poco库中还提供了RandomInputStream类,用于Poco::Random类的流操作。


成员函数:
1. void seed(Poco::UInt32 seed)
根据给定的种子值生成随机数。


2. void seed()
使用任意值(从RandomInputStream类中获取)生成随机数。


3. 默认的构造时,Poco::Random类采用当前的时间和日期生成随机数。如果想要更好的随机效果,需要显式的调用seed()方法


4. UInt32 next()
返回0 ~ 2^31之间的随机整数


5. UInt32 next(UInt32 n)
返回0 ~ n之间的随机整数


6. char nextChar()
返回随机Char值


7. bool nextBool()
返回随机bool值


8. float nextFloat()
返回随机float值,范围0 ~ 1


9. double nextDouble()
返回随机double值,范围0 ~ 1


下面是关于Random的一个例子:

[cpp]
#include "Poco/Random.h"
#include "Poco/RandomStream.h"
#include
using Poco::Random;
using Poco::RandomInputStream;
int main(int argc, char** argv)
{
Random rnd;
rnd.seed();
std::cout << "Random integer: " << rnd.next() << std::endl;
std::cout << "Random digit: " << rnd.next(10) << std::endl;
std::cout << "Random char: " << rnd.nextChar() << std::endl;
std::cout << "Random bool: " << rnd.nextBool() << std::endl;
std::cout << "Random double: " << rnd.nextDouble() << std::endl;
RandomInputStream ri;
std::string rs;
ri >> rs;
return 0;
}

#include "Poco/Random.h"
#include "Poco/RandomStream.h"
#include
using Poco::Random;
using Poco::RandomInputStream;
int main(int argc, char** argv)
{
Random rnd;
rnd.seed();
std::cout << "Random integer: " << rnd.next() << std::endl;
std::cout << "Random digit: " << rnd.next(10) << std::endl;
std::cout << "Random char: " << rnd.nextChar() << std::endl;
std::cout << "Random bool: " << rnd.nextBool() << std::endl;
std::cout << "Random double: " << rnd.nextDouble() << std::endl;
RandomInputStream ri;
std::string rs;
ri >> rs;
return 0;
}

2. 密码散列
下面这段是Wiki上关于密码散列的介绍:
A cryptographic hash function is a hash function with certain additional security properties to make it suitable for use as a primitive in various information security applications, such as authentication and message integrity. A hash function takes a long string (or message) of any length as input and produces a fixed length string as output, sometimes termed a message digest or a digital fingerprint. Wikipedia


2.1 概述
密码散列(cryptographic hash)是将目标文本转换成具有相同长度的、不可逆的杂凑字符串(或叫做消息摘要)。它有两个特点:
1、哈希算法往往被设计成生成具有相同长度的文本
2、哈希算法是不可逆的。(因为如果可逆,那么哈希就是世界上最强悍的压缩方式——能将任意大小的文件压缩成固定大小)

密码散列是一个多对一映射,好的哈希算法应该对于输入的改变极其敏感。Poco中实现了被广泛使用的密码散列函数(cryptographic hash functions), 包括了MD4, MD5
和SHA1。另外还提供了HMACEngine类实现了HMAC功能。HMAC全称为Hash-based Message Authentication Code,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。

2.2 DigestEngine类
Poco::DigestEngine类为所有的消息摘要类定义了通用接口。
1. unsigned digestLength()
用于获取不同消息摘要算法生成消息摘要的长度。
2. const Digest& digest()
获取消息摘要内容
3. update(const void* data, unsigned length)
更新消息摘要内容


让我们来看一下DigestEngine类的类图。

\


下面是Poco中相关类的一些例子:

[cpp]
#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 D

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目