使用单个命令转储 md5 和 sha1 校验和!

M S*_*mar 10 md5sum

我正在寻找用于通过一个命令计算md5、sha1 哈希值的命令或实用程序。
眼下Ubuntu已经sha1summd5sum计算指令hash值。

ket*_*til 10

您可以使用一些适当的 bash ninja-fu 来实现这一点。:)

您知道一次计算一个的过程:

$ echo abc | md5sum
0bee89b07a248e27c83fc3d5951213c1  -
$ echo abc | sha1sum
03cfd743661f07975fa2f1220c5194cbaff48451  -
Run Code Online (Sandbox Code Playgroud)

编辑:正如@gertvdijk 所建议的,并多阅读信息页面,这可以直接使用现代 shell 支持的 tee 和 Process Substitution 来完成,无需重定向。通过这种方式,您可以使用 tee 将数据传递给两个进程和一个文件:

$ echo abc | tee >(md5sum) >(sha1sum) > output.txt
Run Code Online (Sandbox Code Playgroud)

如果您需要更多,也可以链接,但您必须处理所有子流程的 STDOUT。这不会给您预期的结果,而是将前两个校验和与 output.txt 中的数据混合在一起:

$ echo abc | tee >(md5sum) >(sha1sum) | tee >(sha256sum) >(sha512sum) > output.txt
Run Code Online (Sandbox Code Playgroud)

如果您将校验和重定向到替换进程内的文件,您可以根据需要链接这些:

$ echo abc | tee >(md5sum > /tmp/md5.txt) >(sha1sum > /tmp/sha1.txt) | tee >(sha256sum > /tmp/sha256.txt) >(sha512sum > /tmp/sha512.txt) > output.txt
Run Code Online (Sandbox Code Playgroud)

这是我没有进程替换的初步建议,但它允许在不混合数据和输出的情况下链接/递归使用:

$ echo abc | tee -a /proc/self/fd/2 2> >(sha1sum) > >(md5sum)
0bee89b07a248e27c83fc3d5951213c1  -
03cfd743661f07975fa2f1220c5194cbaff48451  -
Run Code Online (Sandbox Code Playgroud)

这里的技巧是使用tee,它将数据复制到 STDOUT 和一个文件。我们很聪明地告诉它将数据写入文件 /proc/self/fd/2,它恰好总是当前进程的 STDERR 文件描述符。使用> >(program)语法,我们可以将每个文件描述符重定向到程序的 STDIN 而不是文件。就像|,但具有更多控制权。> >(md5sum)将 STDOUT 重定向到md5sum程序,而2> >(sha1sum)将 STDERR 重定向到sha1sum程序。

需要注意的是秩序2>>似乎无所谓,我已经把2>第一个命令行上。这些是从右到左评估的,但我不确定为什么会有所不同。

要在文件或硬盘驱动器上执行此操作,您应该将“echo abc”替换为 cat 或 dd,例如:

dd if=/dev/sda bs=8k | tee -a /proc/self/fd/2 2> >(sha1sum) > >(md5sum)
Run Code Online (Sandbox Code Playgroud)

这样做的妙处在于,您实际上可以递归并同时运行多个,而不仅仅是两个。语法变得毛茸茸的,但这有效:

echo abc | tee -a /proc/self/fd/2 2> >(tee -a /proc/self/fd/2 2> >(sha256sum) > >(sha384sum) ) > >(sha512sum)
Run Code Online (Sandbox Code Playgroud)

如果您想捕获结果并在脚本中使用它,那也可以:

A=$(echo abc | tee -a /proc/self/fd/2 2> >(sha1sum) > >(md5sum))
Run Code Online (Sandbox Code Playgroud)

现在$A是一个包含所有输出的字符串,包括换行符。您也可以稍后解析这些值:

echo "checksum=[$(echo "$A" | head -1 | cut -d " " -f 1)]"
Run Code Online (Sandbox Code Playgroud)

不过,我不确定您对输出的排序有任何保证。

  • +1。`tee` 和在 shell 中巧妙使用输出重定向是要走的路。这样可以节省大量资源,尤其是在读取大文件时。 (2认同)
  • 顺便说一句,我认为您不需要重定向到 stderr 来复制流的输出。使用 subshel​​l 也可以解决问题,维护 stderr。在 [博客文章](https://blog.g3rt.nl/luks-smartcard-or-token.html#enhance-security-avoid-temporary-key-storage) 中查看我的示例。 (2认同)

rɑː*_*dʒɑ 3

无法帮助您使用命令行,但我知道一个名为quickhash 的GUI 工具。

您可以从Quickhash下载该工具

描述:

Linux 和 Windows GUI,可快速选择文件(单独或在整个文件夹结构中递归)文本和(在 Linux 上)磁盘并进行后续哈希处理。专为 Linux 设计,但也可用于 Windows。MD5、SHA1、SHA256、SHA512 可用。输出复制到剪贴板或保存为 CSV\HTML 文件。


M S*_*mar 0

Here i have find one python script from source which calculate hash values. and also i find some statistics about hash value calculation.

 - `md5sum` takes 00:3:00 min to calculate 4GB USB.
 - `sha2sum` takes 00:3:01 min to calculate 4GB USB.
 - While phython script takes 3:16 min to calculate both MD5 and SHA1.
Run Code Online (Sandbox Code Playgroud)

//脚本从这里开始

def get_custom_checksum(input_file_name):
    from datetime import datetime
    starttime = datetime.now()
    # START: Actual checksum calculation
    from hashlib import md5, sha1, sha224, sha384, sha256, sha512
    #chunk_size = 1 # 1 byte -- NOT RECOMENDED -- USE AT LEAST 1KB. When 1KB takes 1 min to run, 1B takes 19 minutes to run
    #chunk_size = 1024 # 1 KB
    chunk_size = 1048576 # 1024 B * 1024 B = 1048576 B = 1 MB
    file_md5_checksum = md5()
    file_sha1_checksum = sha1()

    try:
        with open(input_file_name, "rb") as f:
            byte = f.read(chunk_size)
            previous_byte = byte
            byte_size = len(byte)
            file_read_iterations = 1
            while byte:
                file_md5_checksum.update(byte)
                file_sha1_checksum.update(byte)               
                previous_byte = byte
                byte = f.read(chunk_size)
                byte_size += len(byte)
                file_read_iterations += 1
    except IOError:
        print ('File could not be opened: %s' % (input_file_name))
        #exit()
        return
    except:
        raise
    # END: Actual checksum calculation
    # For storage purposes, 1024 bytes = 1 kilobyte
    # For data transfer purposes, 1000 bits = 1 kilobit
    kilo_byte_size = byte_size/1024
    mega_byte_size = kilo_byte_size/1024
    giga_byte_size = mega_byte_size/1024
    bit_size = byte_size*8
    kilo_bit_size = bit_size/1000
    mega_bit_size = kilo_bit_size/1000
    giga_bit_size = mega_bit_size/1000
    last_chunk_size = len(previous_byte)
    stoptime = datetime.now()
    processtime = stoptime-starttime
    custom_checksum_profile = {
        'starttime': starttime,
        'byte_size': byte_size,
        'kilo_byte_size': kilo_byte_size,
        'mega_byte_size': mega_byte_size,
        'giga_byte_size': giga_byte_size,
        'bit_size': bit_size,
        'kilo_bit_size': kilo_bit_size,
        'mega_bit_size': mega_bit_size,
        'giga_bit_size': giga_bit_size,
        'file_read_iterations': file_read_iterations,
        'last_chunk_size': last_chunk_size,
        'md5_checksum': file_md5_checksum.hexdigest(),
        'sha1_checksum': file_sha1_checksum.hexdigest(),        
        'stoptime': stoptime,
        'processtime': processtime,
        }
    return custom_checksum_profile



def print_custom_checksum(input_file_name):
    custom_checksum_profile = get_custom_checksum(input_file_name)
    try:
        print 'Start Time ::', custom_checksum_profile['starttime']
Run Code Online (Sandbox Code Playgroud)

custom_checksum_profile['file_read_iterations']) # print ('最后一块(字节):', custom_checksum_profile['last_chunk_size']) print 'MD5 ::', custom_checksum_profile['md5_checksum'] print 'SHA1 ::', custom_checksum_profile['sha1_checksum '] print 'Stop Time ::', custom_checksum_profile['stoptime'] print 'Processing Time ::', custom_checksum_profile['processtime'] except TypeError: # 'NoneType' 对象不可下标 --- 基本上这应该发生在无法打开输入文件#raise pass # csv 输出

import argparse
script_version='0.0.2'
parser = argparse.ArgumentParser(description='Determine and print various checksums of an input file and its size. Supported checksums are MD5, SHA1, SHA224, SHA256, SHA384, and SHA512.', version=script_version)
parser.add_argument('-f', '--file', metavar='in-file', action='store', dest='file_name', type=str, required=True, help='Name of file for which the checksum needs to be calculated')
args = parser.parse_args()
print 'Processing File ::', args.file_name
print_custom_checksum(args.file_name)
Run Code Online (Sandbox Code Playgroud)