【问题标题】:Difference in the outputs of OpenSSL SHA1 API and command line utilityOpenSSL SHA1 API 和命令行实用程序的输出差异
【发布时间】:2013-01-11 05:15:18
【问题描述】:

我对 OpenSSL 库和他们提供的命令行实用程序有这个小问题。

下面是我的 C 程序,用于从字符串 "hello, world" 生成 SHA1 哈希:

/* test sha1 sample program */
#include <openssl/sha.h>
#include <stdio.h>

int main()
{
   char input[] = "hello, world";
   unsigned char output[20];

   int i = 0;

   SHA1(input,strlen(input) ,output);

   for( i =0;i<20;i++)
     printf(" %x " , output[i]);
 // value printed --  b7  e2  3e  c2  9a  f2  2b  b  4e  41  da  31  e8  68  d5  72  26  12  1c  84

 // value from CMD
 // utility           cd  50  d1  97  84  89  70  85  a8  d0  e3  e4  13  f8  61  2b  09  7c  03  f1

   return 0;
 }

我使用的命令行工具是:

>echo "hello, world" | openssl sha1 
(stdin)= cd50d19784897085a8d0e3e413f8612b097c03f1

价值观不同,谁能指出为什么两者的行为会有所不同?

【问题讨论】:

    标签: openssl sha1


    【解决方案1】:

    echo "hello, world" 在末尾添加了一个尾随换行符。

    要产生相同的结果,请尝试 echo -n "hello, world" | openssl sha1

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2015-02-21
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多