es_ftw.cpp

来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 125 行

CPP
125
字号
/************************************************** FTW EntropySource Source File                  ** (C) 1999-2002 The Botan Project                **************************************************/#ifndef _XOPEN_SOURCE  #define _XOPEN_SOURCE 500#endif#ifndef _XOPEN_SOURCE_EXTENDED  #define _XOPEN_SOURCE_EXTENDED 1#endif#include <botan/es_ftw.h>#include <botan/util.h>#include <fstream>#include <cstring>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <dirent.h>namespace Botan {/************************************************** FTW_EntropySource Constructor                  **************************************************/FTW_EntropySource::FTW_EntropySource(const std::string& p) : path(p)   {   do_poll(64);   }/************************************************** Fast Poll                                      **************************************************/u32bit FTW_EntropySource::fast_poll(byte out[], u32bit length)   {   if(position == buffer.size())      do_poll(128);   u32bit copied = std::min(length, buffer.size() - position);   copy_mem(out, buffer + position, copied);   position += copied;   return copied;   }/************************************************** Slow Poll                                      **************************************************/u32bit FTW_EntropySource::slow_poll(byte out[], u32bit length)   {   do_poll(2*1024);   u32bit copied = std::min(length, buffer.size());   copy_mem(out, buffer + position, copied);   position += copied;   return copied;   }/************************************************** Gather Entropy                                 **************************************************/void FTW_EntropySource::do_poll(u32bit limit)   {   files_read = 0;   gather_from_dir(path, limit);   position = 0;   }/************************************************** Gather Entropy From Directory Tree             **************************************************/void FTW_EntropySource::gather_from_dir(const std::string& dirname,                                        u32bit limit)   {   if(dirname == "")      return;   DIR* dir = opendir(dirname.c_str());   if(dir == 0)      return;   dirent* entry = readdir(dir);   while(entry && (files_read < limit))      {      if((std::strcmp(entry->d_name, ".") == 0) ||         (std::strcmp(entry->d_name, "..") == 0))         { entry = readdir(dir); continue; }      std::string filename = dirname + '/' + entry->d_name;      struct stat stat_buf;      if(lstat(filename.c_str(), &stat_buf) == -1)         { entry = readdir(dir); continue; }      if(S_ISREG(stat_buf.st_mode))         gather_from_file(filename);      if(S_ISDIR(stat_buf.st_mode))         gather_from_dir(filename, limit - files_read);      entry = readdir(dir);      }   closedir(dir);   }/************************************************** Gather Entropy From A File                     **************************************************/void FTW_EntropySource::gather_from_file(const std::string& filename)   {   int fd = open(filename.c_str(), O_RDONLY);   if(fd == -1) return;   files_read++;   SecureVector<byte> read_buf(buffer.size());   ssize_t got = read(fd, read_buf, buffer.size());   if(got < 0) got = 0;   xor_buf(buffer, read_buf, got);   close(fd);   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?