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

📄 test.cpp

📁 MySQL数据库开发源码 值得一看哦
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// test.cpp// test taocrypt functionality#include <string.h>#include <stdio.h>#include "runtime.hpp"#include "sha.hpp"#include "md5.hpp"#include "md2.hpp"#include "md4.hpp"#include "ripemd.hpp"#include "hmac.hpp"#include "arc4.hpp"#include "des.hpp"#include "rsa.hpp"#include "dsa.hpp"#include "aes.hpp"#include "twofish.hpp"#include "blowfish.hpp"#include "asn.hpp"#include "dh.hpp"#include "coding.hpp"#include "random.hpp"#include "pwdbased.hpp"using TaoCrypt::byte;using TaoCrypt::word32;using TaoCrypt::SHA;using TaoCrypt::MD5;using TaoCrypt::MD2;using TaoCrypt::MD4;using TaoCrypt::RIPEMD160;using TaoCrypt::HMAC;using TaoCrypt::ARC4;using TaoCrypt::DES_EDE3_CBC_Encryption;using TaoCrypt::DES_EDE3_CBC_Decryption;using TaoCrypt::DES_CBC_Encryption;using TaoCrypt::DES_CBC_Decryption;using TaoCrypt::DES_ECB_Encryption;using TaoCrypt::DES_ECB_Decryption;using TaoCrypt::AES_CBC_Encryption;using TaoCrypt::AES_CBC_Decryption;using TaoCrypt::AES_ECB_Encryption;using TaoCrypt::AES_ECB_Decryption;using TaoCrypt::Twofish_CBC_Encryption;using TaoCrypt::Twofish_CBC_Decryption;using TaoCrypt::Twofish_ECB_Encryption;using TaoCrypt::Twofish_ECB_Decryption;using TaoCrypt::Blowfish_CBC_Encryption;using TaoCrypt::Blowfish_CBC_Decryption;using TaoCrypt::Blowfish_ECB_Encryption;using TaoCrypt::Blowfish_ECB_Decryption;using TaoCrypt::RSA_PrivateKey;using TaoCrypt::RSA_PublicKey;using TaoCrypt::DSA_PrivateKey;using TaoCrypt::DSA_PublicKey;using TaoCrypt::DSA_Signer;using TaoCrypt::DSA_Verifier;using TaoCrypt::RSAES_Encryptor;using TaoCrypt::RSAES_Decryptor;using TaoCrypt::Source;using TaoCrypt::FileSource;using TaoCrypt::FileSource;using TaoCrypt::HexDecoder;using TaoCrypt::HexEncoder;using TaoCrypt::Base64Decoder;using TaoCrypt::Base64Encoder;using TaoCrypt::CertDecoder;using TaoCrypt::DH;using TaoCrypt::EncodeDSA_Signature;using TaoCrypt::DecodeDSA_Signature;using TaoCrypt::PBKDF2_HMAC;using TaoCrypt::tcArrayDelete;struct testVector {    byte*  input_;    byte*  output_;     size_t inLen_;    size_t outLen_;    testVector(const char* in, const char* out) : input_((byte*)in),               output_((byte*)out), inLen_(strlen(in)), outLen_(strlen(out)) {}};void file_test(int, char**);int  sha_test();int  md5_test();int  md2_test();int  md4_test();int  ripemd_test();int  hmac_test();int  arc4_test();int  des_test();int  aes_test();int  twofish_test();int  blowfish_test();int  rsa_test();int  dsa_test();int  dh_test();int  pwdbased_test();TaoCrypt::RandomNumberGenerator rng;void err_sys(const char* msg, int es){    printf("%s", msg);    exit(es);    }// func_args from test.hpp, so don't have to pull in other junkstruct func_args {    int    argc;    char** argv;    int    return_code;};/*    DES, AES, Blowfish, and Twofish need aligned (4 byte) input/output for   processing, can turn this off by setting gpBlock(assumeAligned = false)   but would hurt performance.  yaSSL always uses dynamic memory so we have   at least 8 byte alignment.  This test tried to force alignment for stack   variables (for convenience) but some compiler versions and optimizations   seemed to be off.  So we have msgTmp variable which we copy into dynamic   memory at runtime to ensure proper alignment, along with plain/cipher.   Whew!*/const byte msgTmp[] = { // "now is the time for all " w/o trailing 0    0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,    0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,    0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};byte* msg    = 0;   // for block cipher inputbyte* plain  = 0;   // for cipher decrypt comparison byte* cipher = 0;   // block outputvoid taocrypt_test(void* args){    ((func_args*)args)->return_code = -1; // error state        msg    = NEW_TC byte[24];    plain  = NEW_TC byte[24];    cipher = NEW_TC byte[24];    memcpy(msg, msgTmp, 24);    int ret = 0;    if ( (ret = sha_test()) )         err_sys("SHA      test failed!\n", ret);    else        printf( "SHA      test passed!\n");    if ( (ret = md5_test()) )         err_sys("MD5      test failed!\n", ret);    else        printf( "MD5      test passed!\n");    if ( (ret = md2_test()) )         err_sys("MD2      test failed!\n", ret);    else        printf( "MD2      test passed!\n");    if ( (ret = md4_test()) )         err_sys("MD4      test failed!\n", ret);    else        printf( "MD4      test passed!\n");    if ( (ret = ripemd_test()) )        err_sys("RIPEMD   test failed!\n", ret);    else        printf( "RIPEMD   test passed!\n");    if ( ( ret = hmac_test()) )        err_sys("HMAC     test failed!\n", ret);    else        printf( "HMAC     test passed!\n");    if ( (ret = arc4_test()) )        err_sys("ARC4     test failed!\n", ret);    else        printf( "ARC4     test passed!\n");    if ( (ret = des_test()) )        err_sys("DES      test failed!\n", ret);    else        printf( "DES      test passed!\n");    if ( (ret = aes_test()) )        err_sys("AES      test failed!\n", ret);    else        printf( "AES      test passed!\n");    if ( (ret = twofish_test()) )        err_sys("Twofish  test failed!\n", ret);    else        printf( "Twofish  test passed!\n");    if ( (ret = blowfish_test()) )        err_sys("Blowfish test failed!\n", ret);    else        printf( "Blowfish test passed!\n");    if ( (ret = rsa_test()) )        err_sys("RSA      test failed!\n", ret);    else        printf( "RSA      test passed!\n");    if ( (ret = dh_test()) )        err_sys("DH       test failed!\n", ret);    else        printf( "DH       test passed!\n");    if ( (ret = dsa_test()) )        err_sys("DSA      test failed!\n", ret);    else        printf( "DSA      test passed!\n");    if ( (ret = pwdbased_test()) )        err_sys("PBKDF2   test failed!\n", ret);    else        printf( "PBKDF2   test passed!\n");    tcArrayDelete(cipher);    tcArrayDelete(plain);    tcArrayDelete(msg);    ((func_args*)args)->return_code = ret;}// so overall tests can pull in test function #ifndef NO_MAIN_DRIVER    int main(int argc, char** argv)    {        func_args args;        args.argc = argc;        args.argv = argv;        taocrypt_test(&args);        return args.return_code;    }#endif // NO_MAIN_DRIVERvoid file_test(char* file, byte* check){    FILE* f;    int   i(0);    MD5   md5;    byte  buf[1024];    byte  md5sum[MD5::DIGEST_SIZE];        if( !( f = fopen( file, "rb" ) )) {        printf("Can't open %s\n", file);        return;    }    while( ( i = fread(buf, 1, sizeof(buf), f )) > 0 )        md5.Update(buf, i);        md5.Final(md5sum);    memcpy(check, md5sum, sizeof(md5sum));    for(int j = 0; j < MD5::DIGEST_SIZE; ++j )         printf( "%02x", md5sum[j] );       printf("  %s\n", file);    fclose(f);}int sha_test(){    SHA  sha;    byte hash[SHA::DIGEST_SIZE];    testVector test_sha[] =    {        testVector("abc",                  "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"                 "\x6C\x9C\xD0\xD8\x9D"),        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",                 "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"                 "\xE5\xE5\x46\x70\xF1"),        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"                 "aaaaaa",                  "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"                 "\x2A\x25\xEC\x64\x4D"),        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"                 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"                 "aaaaaaaaaa",                 "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"                 "\x53\x99\x5E\x26\xA0")      };    int times( sizeof(test_sha) / sizeof(testVector) );    for (int i = 0; i < times; ++i) {        sha.Update(test_sha[i].input_, test_sha[i].inLen_);        sha.Final(hash);        if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)            return -1 - i;    }    return 0;}int md5_test(){    MD5  md5;    byte hash[MD5::DIGEST_SIZE];    testVector test_md5[] =    {        testVector("abc",                  "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"                 "\x72"),        testVector("message digest",                  "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"                 "\xd0"),        testVector("abcdefghijklmnopqrstuvwxyz",                 "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"                 "\x3b"),        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"                 "6789",                 "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"                 "\x9f"),        testVector("1234567890123456789012345678901234567890123456789012345678"                 "9012345678901234567890",                 "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"                 "\x7a")    };    int times( sizeof(test_md5) / sizeof(testVector) );    for (int i = 0; i < times; ++i) {        md5.Update(test_md5[i].input_, test_md5[i].inLen_);        md5.Final(hash);        if (memcmp(hash, test_md5[i].output_, MD5::DIGEST_SIZE) != 0)            return -5 - i;    }    return 0;}int md4_test(){    MD4  md4;    byte hash[MD4::DIGEST_SIZE];    testVector test_md4[] =    {        testVector("",                 "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"                 "\xc0"),        testVector("a",                 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"                 "\x24"),        testVector("abc",                  "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"                 "\x9d"),        testVector("message digest",                  "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"                 "\x4b"),        testVector("abcdefghijklmnopqrstuvwxyz",                 "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"                 "\xa9"),        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"                 "6789",                 "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"                 "\xe4"),        testVector("1234567890123456789012345678901234567890123456789012345678"                 "9012345678901234567890",                 "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"                 "\x36")    };    int times( sizeof(test_md4) / sizeof(testVector) );    for (int i = 0; i < times; ++i) {        md4.Update(test_md4[i].input_, test_md4[i].inLen_);        md4.Final(hash);        if (memcmp(hash, test_md4[i].output_, MD4::DIGEST_SIZE) != 0)            return -5 - i;    }    return 0;}int md2_test(){    MD2  md5;    byte hash[MD2::DIGEST_SIZE];    testVector test_md2[] =    {        testVector("",                   "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"                   "\x27\x73"),        testVector("a",                   "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"                   "\xb5\xd1"),        testVector("abc",                   "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"                   "\xd6\xbb"),        testVector("message digest",                   "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"                   "\x06\xb0"),        testVector("abcdefghijklmnopqrstuvwxyz",                   "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"                   "\x94\x0b"),        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"                   "0123456789",                   "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"                   "\x38\xcd"),        testVector("12345678901234567890123456789012345678901234567890123456"                   "789012345678901234567890",                   "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"                   "\xef\xd8")    };    int times( sizeof(test_md2) / sizeof(testVector) );    for (int i = 0; i < times; ++i) {        md5.Update(test_md2[i].input_, test_md2[i].inLen_);        md5.Final(hash);        if (memcmp(hash, test_md2[i].output_, MD2::DIGEST_SIZE) != 0)            return -10 - i;    }    return 0;}int ripemd_test(){    RIPEMD160  ripe160;    byte hash[RIPEMD160::DIGEST_SIZE];    testVector test_ripemd[] =    {        testVector("",                   "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8"                   "\xf5\x48\xb2\x25\x8d\x31"),        testVector("a",                   "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4"                   "\xdc\x83\x5a\x46\x7f\xfe"),        testVector("abc",                   "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"                   "\xb0\x87\xf1\x5a\x0b\xfc"),        testVector("message digest",                   "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"                   "\x5f\xfa\x21\x59\x5f\x36"),        testVector("abcdefghijklmnopqrstuvwxyz",                   "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d"                   "\x28\x65\xb3\x70\x8d\xbc"),        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",                   "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"                   "\xf4\x9a\xda\x62\xeb\x2b"),        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"                   "456789",                   "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71"                   "\x30\x79\xb2\x1f\x51\x89"),        testVector("12345678901234567890123456789012345678901234567890123456"                   "789012345678901234567890",                   "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"                   "\x82\xbf\x63\x32\x6b\xfb"),    };    int times( sizeof(test_ripemd) / sizeof(testVector) );    for (int i = 0; i < times; ++i) {        ripe160.Update(test_ripemd[i].input_, test_ripemd[i].inLen_);        ripe160.Final(hash);        if (memcmp(hash, test_ripemd[i].output_, RIPEMD160::DIGEST_SIZE) != 0)            return -100 - i;    }    return 0;}int hmac_test(){

⌨️ 快捷键说明

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