📄 addent.cpp
字号:
// addent.cc// util to add entropy// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include <stdio.h> // printf#include <stdlib.h> // exit, abs#include "globrand.h" // global random pool#include "addent.h" // this module#include "nonport.h" // setRawMode, getConsoleChar, getMilliseconds#include "syserr.h" // xSysError::xsyserror#include "sftpver.h" // SFTP_version#ifdef __BORLANDC__# pragma warn -aus // (blah) assigned a value that is never used#endif//-----------------------------------------#define SCREEN_WIDTH 65 // # of dots printed when user is done//#define BITS_PER_KEYPRESS 5 // crude estimate#define BITS_PER_KEYPRESS 10 // crude estimate (reduce user frustration)#define REGULAR_INTERVAL 30 // two consec. intervals within this many ms are ignoredvoid getEntropyFromConsole(int numbits) { if (hasSystemCryptoRandom()) { getEntropyFromSystem(numbits); return; } printf("Please type some sentences to add entropy to the system.\n"); // to reduce user frustration, print a progress bar first printf("|"); for (int i=0; i<SCREEN_WIDTH-2; i++) { printf("-"); } printf("|\n"); // set console to raw mode, required for getConsoleChar setRawMode(true); //int bitspertick = numbits/SCREEN_WIDTH; // SM: this suffers from rounding errors int dotsprinted = 0; // # of dots printed so far int totalbits = numbits; // keep this value for later use int thrownAway = 0; // testing... int totalPressed = 0; char c, lc = '\0', llc = '\0'; //, lllc = '\0'; int time, lltime = 0, ltime = 0; //int bitsthistick = bitspertick; while (numbits > 0) { c = getConsoleChar(); //Sleep(c); // use what they typed as well // SM: the problem with using sleep is that it may significantly // reduce the time-based entropy by forcing time to be a // fixed time from the schedule boundary (this may also be // true with getchar, however) addEntropy(c); if (c == 3) { // user hit cntrl-c setRawMode(false); printf("\nControl-C: Entropy-gathering aborted by user.\n"); exit(1); } //addEntropy(); // add some entropy // SM: why was this being done twice? // approximate how much we added time = getMilliseconds(); if (c != lc && c != llc && abs((ltime - lltime) - (time - ltime)) > REGULAR_INTERVAL) { // they hit a different key, and the interval changed sufficiently numbits -= BITS_PER_KEYPRESS; // compute how many dots should have been printed int shoulddots = SCREEN_WIDTH * (totalbits - numbits) / totalbits; if (shoulddots > SCREEN_WIDTH) { shoulddots = SCREEN_WIDTH; } while (dotsprinted < shoulddots) { printf("."); dotsprinted++; } #if 0 // SM: rounding problems... bitsthistick -= BITS_PER_KEYPRESS; if (bitsthistick <= 0) { printf("."); bitsthistick += bitspertick; } #endif // 0 } else { thrownAway++; // testing... } totalPressed++; lltime = ltime; ltime = time; //lllc = llc; // warning: 'lllc' is assigned a value that is never used llc = lc; lc = c; } // print stats that are marginally useful for development printf("(%d,%d)\r\n", thrownAway, totalPressed); // eat the characters we don't need printf("\a\r\nThat's enough; please press Enter once:"); while (getConsoleChar() != '\r') {} printf("\r\n"); // back to normal console interaction setRawMode(false); return; }void getEntropyFromSystem(int bits){ printf("Now gathering entropy from system... If this appears to stall,\n" "try moving the mouse or typing into another console window.\n"); // print a progress meter of sorts printf(" Entropy progress: [ ]\b\b\b\b\b\b\b\b\b\b\b"); fflush(stdout); int gathered=0, dotsPrinted=0; while (gathered < bits) { addEntropy(getSystemCryptoRandom()); gathered += 32; int shouldPrinted = mymin(gathered * 10 / bits, 10); while (dotsPrinted < shouldPrinted) { printf("."); fflush(stdout); dotsPrinted++; } } while (dotsPrinted++ < 10) { printf("."); } printf("\n");}//-----------------------------------------#ifdef ADDENT_MAINint main(int argc, char *argv[]){ if (argc < 2) { printf("addent version %s\n" "usage:\n" " %s bits-of-entropy\n" " (1024 is a good value for bits-of-entropy)\n", SFTP_version, argv[0]); return 0; } int entropyBits = atoi(argv[1]); // setup nonport error handling nonportFail = xSysError::xsyserror; readRandomSeed(false /*complain*/); if (argc < 4) { getEntropyFromConsole(entropyBits); } // save random number saveRandomSeed(); return 0;}#endif // ADDENT_MAIN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -