首页 > 其他分享 >OpenSSL 介绍(2)--消息摘要

OpenSSL 介绍(2)--消息摘要

时间:2023-02-12 10:11:56浏览次数:39  
标签:dgst val -- 摘要 OpenSSL openssl 算法

本文主要介绍如何使用 OpenSSL 来进行消息摘要计算,文中所使用到的软件版本:OpenSSL 1.1.1s、CentOS 7.9.2009。

1、摘要算法

摘要算法是一种能产生特殊输出格式的算法,这种算法的特点是:无论用户输入什么长度的原始数据,经过计算后输出的密文都是固定长度的,这种算法的原理是根据一定的运算规则对原数据进行某种形式的提取,这种提取就是摘要,被摘要的数据内容与原数据有密切联系,只要原数据稍有改变,输出的“摘要”便完全不同,因此,基于这种原理的算法便能对数据完整性提供较为健全的保障。但是,由于输出的密文是提取原数据经过处理的定长值,所以它已经不能还原为原数据,即消息摘要算法是不可逆的,理论上无法通过反向运算取得原数据内容,因此它通常只能被用来做数据完整性验证。

常用的消息摘要算法包括:MD5、SHA-1、SHA-224、SHA-256、SHA-384、SHA-512等。

2、用法

可以使用 OpenSSL 来进行消息摘要计算,通过 openssl dgst -help 命令查看用法:

shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl dgst -help        
Usage: dgst [options] [file...]
  file... files to digest (default is stdin)
 -help               Display this summary
 -list               List digests
 -c                  Print the digest with separating colons
 -r                  Print the digest in coreutils format
 -out outfile        Output to filename rather than stdout
 -passin val         Input file pass phrase source
 -sign val           Sign digest using private key
 -verify val         Verify a signature using public key
 -prverify val       Verify a signature using private key
 -signature infile   File with signature to verify
 -keyform format     Key file format (PEM or ENGINE)
 -hex                Print as hex dump
 -binary             Print in binary form
 -d                  Print debug info
 -debug              Print debug info
 -fips-fingerprint   Compute HMAC with the key used in OpenSSL-FIPS fingerprint
 -hmac val           Create hashed MAC with key
 -mac val            Create MAC (not necessarily HMAC)
 -sigopt val         Signature parameter in n:v form
 -macopt val         MAC algorithm parameters in n:v form or key
 -*                  Any supported digest
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file
 -engine val         Use engine e, possibly a hardware device
 -engine_impl        Also use engine given by -engine for digest operations

参数说明:

参数 说明
-hex 以十六进制的格式输出结果,这是默认的输出格式
-* 指定算法,可以使用 openssl dgst -list 查看支持的算法
-out filename 指定输入文件,如果不知道则输出到标准输出
-d,-debug 打印调试信息

3、查看支持的算法

可以使用 openssl dgst -list 查看当前版本 OpenSSL 支持的算法:

shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl dgst -list
Supported digests:
-blake2b512                -blake2s256                -md4                      
-md5                       -md5-sha1                  -mdc2                     
-ripemd                    -ripemd160                 -rmd160                   
-sha1                      -sha224                    -sha256                   
-sha3-224                  -sha3-256                  -sha3-384                 
-sha3-512                  -sha384                    -sha512                   
-sha512-224                -sha512-256                -shake128                 
-shake256                  -sm3                       -ssl3-md5                 
-ssl3-sha1                 -whirlpool

4、具体使用

4.1、使用 md5 算法进行消息摘要

shell> echo -n 123456|/home/mongo/soft/openssl-1.1.1s/bin/openssl dgst -md5 -hex   
(stdin)= e10adc3949ba59abbe56e057f20f883e

该命令等效于如下的命令:

shell> echo -n 123456|/home/mongo/soft/openssl-1.1.1s/bin/openssl md5 -hex   
(stdin)= e10adc3949ba59abbe56e057f20f883e

4.2、使用 sha256 算法进行消息摘要

shell> echo -n 123456|/home/mongo/soft/openssl-1.1.1s/bin/openssl dgst -sha256 -hex
(stdin)= 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

4.3、使用文件作为输入

新建文件 a.txt,在文件中输入内容:

123456

使用 a.txt 作为输入:

shell> /home/mongo/soft/openssl-1.1.1s/bin/openssl dgst -sha256 a.txt
SHA256(a.txt)= e150a1ec81e8e93e1eae2c3a77e66ec6dbd6a3b460f89c1d08aecf422ee401a0

 

注:标准输入(结束方法:先输入回车,然后输入 ctrl+D)会把换行符也带入计算;使用文件输入时,也会在文件内容最后增加换行作为 OpenSSL 的计算输入;这会导致计算结果与其他消息摘要工具(如:Java)的计算结果不一致,所以使用 echo -n 来去除换行。

 

标签:dgst,val,--,摘要,OpenSSL,openssl,算法
From: https://www.cnblogs.com/wuyongyin/p/17029847.html

相关文章