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

📄 bigint.cpp

📁 含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种CheckSum校验、多种MAC校验等几十种加密算法的程序
💻 CPP
字号:
#include <vector>#include <string>#include <fstream>#include <iostream>#include <cstdlib>#include <botan/bigint.h>using namespace Botan;#define DEBUG 0std::vector<std::string> parse(const std::string&);void strip(std::string&);u32bit check_add(const std::vector<std::string>&);u32bit check_sub(const std::vector<std::string>&);u32bit check_mul(const std::vector<std::string>&);u32bit check_div(const std::vector<std::string>&);u32bit check_mod(const std::vector<std::string>&);u32bit check_shr(const std::vector<std::string>&);u32bit check_shl(const std::vector<std::string>&);u32bit do_bigint_tests(const std::string& filename)   {   std::ifstream test_data(filename.c_str());   if(!test_data)       {       std::cout << "Couldn't open test file " << filename << std::endl;       std::exit(1);       }   u32bit errors = 0, alg_count = 0;   std::string algorithm;   bool first = true;   u32bit counter = 0;   while(!test_data.eof())      {      if(test_data.bad() || test_data.fail())         {         std::cout << "File I/O error." << std::endl;         std::exit(1);         }      std::string line;      std::getline(test_data, line);      strip(line);      if(line.size() == 0) continue;      // Do line continuation      while(line[line.size()-1] == '\\' && !test_data.eof())         {         line.replace(line.size()-1, 1, "");         std::string nextline;         std::getline(test_data, nextline);         strip(nextline);         if(nextline.size() == 0) continue;         line += nextline;         }      if(line[0] == '[' && line[line.size() - 1] == ']')         {         algorithm = line.substr(1, line.size() - 2);         alg_count = 0;         counter = 0;         if(!first)            std::cout << std::endl;         std::cout << "Testing BigInt " << algorithm << ": ";         std::cout.flush();         first = false;         continue;         }      std::vector<std::string> substr = parse(line);#if DEBUG      std::cout << "Testing: " << algorithm << std::endl;#endif      u32bit new_errors = 0;      if(algorithm.find("Addition") != std::string::npos)         new_errors = check_add(substr);      if(algorithm.find("Subtraction") != std::string::npos)         new_errors = check_sub(substr);      if(algorithm.find("Multiplication") != std::string::npos)         new_errors = check_mul(substr);      if(algorithm.find("Division") != std::string::npos)         new_errors = check_div(substr);      if(algorithm.find("Modulo") != std::string::npos)         new_errors = check_mod(substr);      if(algorithm.find("LeftShift") != std::string::npos)         new_errors = check_shl(substr);      if(algorithm.find("RightShift") != std::string::npos)         new_errors = check_shr(substr);      if(counter % 3 == 0)         {         std::cout << '.';         std::cout.flush();         }      counter++;      alg_count++;      errors += new_errors;      if(new_errors)         std::cout << "ERROR: BigInt " << algorithm << " failed test #"                   << std::dec << alg_count << std::endl;      }   std::cout << std::endl;   return errors;   }// c==expected, d==a op b, e==a op= bu32bit results(std::string op,               const BigInt& a, const BigInt& b,               const BigInt& c, const BigInt& d, const BigInt& e)   {   std::string op1 = "operator" + op;   std::string op2 = op1 + "=";   if(c == d && d == e)      return 0;   else      {      std::cout << std::endl;#if 1      std::cout << "a = " << a << std::endl;      std::cout << "b = " << b << std::endl;      std::cout << "c = " << c << std::endl;      std::cout << "d = " << d << std::endl;      std::cout << "e = " << e << std::endl;#else      std::cout << "a = " << std::hex << a << std::endl;      std::cout << "b = " << std::hex << b << std::endl;      std::cout << "c = " << std::hex << c << std::endl;      std::cout << "d = " << std::hex << d << std::endl;      std::cout << "e = " << std::hex << e << std::endl;#endif      if(d != e)         {         std::cout << "ERROR: " << op1 << " | " << op2                   << " mismatch" << std::endl;         }      return 1;      }   }u32bit check_add(const std::vector<std::string>& args)   {   BigInt a(args[0]);   BigInt b(args[1]);   BigInt c(args[2]);   BigInt d = a + b;   BigInt e = a;   e += b;   if(results("+", a, b, c, d, e))      return 1;   d = b + a;   e = b;   e += a;   return results("+", a, b, c, d, e);   }u32bit check_sub(const std::vector<std::string>& args)   {   BigInt a(args[0]);   BigInt b(args[1]);   BigInt c(args[2]);   BigInt d = a - b;   BigInt e = a;   e -= b;   return results("-", a, b, c, d, e);   }u32bit check_mul(const std::vector<std::string>& args)   {   BigInt a(args[0]);   BigInt b(args[1]);   BigInt c(args[2]);   BigInt d = a * b;   BigInt e = a;   e *= b;   if(results("*", a, b, c, d, e))      return 1;   d = b * a;   e = b;   e *= a;   return results("*", a, b, c, d, e);   }u32bit check_div(const std::vector<std::string>& args)   {   BigInt a(args[0]);   BigInt b(args[1]);   BigInt c(args[2]);   BigInt d = a / b;   BigInt e = a;   e /= b;   return results("/", a, b, c, d, e);   }u32bit check_mod(const std::vector<std::string>& args)   {   BigInt a(args[0]);   BigInt b(args[1]);   BigInt c(args[2]);   BigInt d = a % b;   BigInt e = a;   e %= b;   return results("%", a, b, c, d, e);   }u32bit check_shl(const std::vector<std::string>& args)   {   BigInt a(args[0]);   u32bit b = std::atoi(args[1].c_str());   BigInt c(args[2]);   BigInt d = a << b;   BigInt e = a;   e <<= b;   return results("<<", a, b, c, d, e);   }u32bit check_shr(const std::vector<std::string>& args)   {   BigInt a(args[0]);   u32bit b = atoi(args[1].c_str());   BigInt c(args[2]);   BigInt d = a >> b;   BigInt e = a;   e >>= b;   return results(">>", a, b, c, d, e);   }

⌨️ 快捷键说明

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