第十五章 摘要与HMAC / 15.4 编程示例

              以下示例了MD2MD4MD5SHASHA1函数的使用方法:

              #include <stdio.h>

              #include <string.h>

              #include <openssl/md2.h>

              #include <openssl/md4.h>

              #include <openssl/md5.h>

              #include <openssl/sha.h>

             

              int    main()

              {

                     unsigned charin[]="3dsferyewyrtetegvbzVEgarhaggavxcv";

                     unsigned charout[20];

                     size_t                    n;

                     int                         i;

             

                     n=strlen((const char*)in);

                     #ifdef             OPENSSL_NO_MDC2

                     printf("默认openssl安装配置无MDC2\n");

#else             

                     MDC2(in,n,out);

                     printf("MDC2 digest result :\n");

                     for(i=0;i<16;i++)

                     printf("%x ",out[i]);

              #endif

                     RIPEMD160(in,n,out);

                     printf("RIPEMD160 digest result :\n");

                     for(i=0;i<20;i++)

                            printf("%x ",out[i]);

                     MD2(in,n,out);

                     printf("MD2 digest result :\n");

                     for(i=0;i<16;i++)

                            printf("%x ",out[i]);

                    

                     MD4(in,n,out);

                     printf("\n\nMD4 digest result :\n");

                     for(i=0;i<16;i++)

                            printf("%x ",out[i]);

             

                     MD5(in,n,out);

                     printf("\n\nMD5 digest result :\n");

                     for(i=0;i<16;i++)

                            printf("%x ",out[i]);

             

                     SHA(in,n,out);

                     printf("\n\nSHA digest result :\n");

                     for(i=0;i<20;i++)

                            printf("%x ",out[i]);

             

                     SHA1(in,n,out);

                     printf("\n\nSHA1 digest result :\n");

                     for(i=0;i<20;i++)

                            printf("%x ",out[i]);

                    

                     SHA256(in,n,out);

                     printf("\n\nSHA256 digest result :\n");

                     for(i=0;i<32;i++)

                            printf("%x ",out[i]);

                    

                     SHA512(in,n,out);

                     printf("\n\nSHA512 digest result :\n");

                     for(i=0;i<64;i++)

                            printf("%x ",out[i]);

                     printf("\n");

                     return 0;

              }

              以上示例中演示了各种摘要计算函数的使用方法。对输入数据in进行摘要计算,结果存放在out缓冲区中。其中:

              mdc2md4md5摘要结果为16字节,128比特;

              ripemd160shasha1摘要结果为20字节,160bit

              sha256摘要结果为32字节,256bit

              sha512摘要结果为64字节,512bit