⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entropy.cpp

📁 伯克利做的SFTP安全文件传输协议
💻 CPP
字号:
//  $Archive:: /SafeTP/entropy.cpp                                     $//     $Date: 1999/07/14 13:16:40 $// $Revision: 1.12 $// Description: function that returns some entropy; platform-specific implementation// copyright SafeTP Development Group, Inc., 2000  Terms of use are as specified in license.txt#include "entropy.h"#include "crc.h"#include "nonport.h"      // {has,get}SystemCryptoRandom//---------------------------------------------------------------------------#ifdef WIN32  // Win32 code#ifndef USE_MINWIN_H#  include <windows.h>#else#  include "minwin.h"#endif#include <process.h>#ifndef __BORLANDC__#  define getpid _getpid#endif#ifdef __BORLANDC__#  pragma warn -pia    // possibly incorrect assignment#endiflong getEntropy() {  // sm: if someone discovers that Windows has a system RNG,  //     we should put calls to it here (like the unix section)  #define ENT_BUF_SIZ   4096  unsigned char buffer[ENT_BUF_SIZ];  unsigned char* p = buffer;  unsigned long *a = (unsigned long*)p;  *a = GetTickCount();  // Tick count since startup  p += sizeof(unsigned long);  // pid  unsigned long *b = (unsigned long*)p;  *b = getpid();  p += sizeof(unsigned long);  // current time  SYSTEMTIME* st = (SYSTEMTIME*)p;  GetSystemTime(st);  p += sizeof(SYSTEMTIME);  // memory status  MEMORYSTATUS* ms = (MEMORYSTATUS*)p;  ms->dwLength = sizeof(MEMORYSTATUS);  GlobalMemoryStatus(ms);  p += sizeof(MEMORYSTATUS);  // Zorder and window placement  HWND hwnd = GetTopWindow(NULL);  WINDOWPLACEMENT wp;  wp.length = sizeof(WINDOWPLACEMENT);  do {    HWND* h = (HWND *)p;    *h = hwnd;    p += sizeof(HWND);  // getmousepos would be nice, but difficult to do without a window  // SM: idea: peek next message, and examine 'pt' member of MSG;  //           may require posting a message    GetWindowPlacement(hwnd, &wp);    LONG* lng = (LONG *)p;    *(lng++) = wp.rcNormalPosition.left;    *(lng++) = wp.rcNormalPosition.top;    *(lng++) = wp.rcNormalPosition.right;    *lng = wp.rcNormalPosition.bottom;    p += 4 * sizeof(LONG);    } while ((hwnd = GetNextWindow(hwnd,GW_HWNDNEXT)) &&             (p-buffer) < (ENT_BUF_SIZ-sizeof(RECT)-sizeof(HWND))); // need 20 bytes/window  // crc it to really scramble stuff up  return crc32(buffer,(p-buffer));  }//---------------------------------------------------------------------------#else  // UNIX code#include <time.h>#include <unistd.h>long getEntropy() {  if (hasSystemCryptoRandom()) {    // thread safety: worst case is we do a few too many or a few    // too few calls to getSystemCryptoRandom, so I'm not going    // to do anything special here    static int ct = 0;    if (ct++ % 10 == 0) {      // every 10th call, get it from the system (instead of every      // time, just to reduce load on system's RNG)      return getSystemCryptoRandom();    }  }  // SM: I rewrote this because I was getting alignment violations  //     on DEC alphas  struct S {    unsigned long pid;    unsigned long clks;    clock_t c;    time_t t;  };  union {    struct S s;    unsigned char buffer[sizeof(S)];  };  s.c = clock();      // approx time since process startup, in units 1/CLK_TCK     s.pid = getpid();   // pid  gmtime(&s.t);       // realtime clock  // get some entropy from process scheduler  unsigned long i=0;  clock_t clk = clock();  if (clk == (clock_t)(-1)) {    // clock() doesn't want to give useful info..     // let's try something else    s.clks = (clock_t)getMilliseconds();  }  else {    // count how many iterations it takes for a change..    while (clk == clock()) i++;  }  s.clks = i;  long ret = crc32(buffer, sizeof(buffer));  return ret;  }#endif

⌨️ 快捷键说明

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