简体   繁体   English

为什么包 object-hash 和 crypto / hashlib 为 sha1 返回不同的值?

[英]Why the packages object-hash and crypto / hashlib return different values for sha1?

I have a javascript frontend which compares two object-hash sha1 hashes in order to determine if an input has changed (in which case a processing pipeline needs to be reran).我有一个 javascript 前端,它比较两个object-hash sha1 哈希以确定输入是否已更改(在这种情况下需要重新运行处理管道)。

I started building a python interface to interact with the same backend which uses hashlib for the sha1 generation, but unfortunately the two functions return different hash values even though the inputs are the same.我开始构建一个 python 接口来与使用hashlib生成 sha1 的相同后端进行交互,但不幸的是,即使输入相同,这两个函数也会返回不同的 hash 值。

I managed to produce the same hash values as hashlib using crypto , which means that the issue arises from object-hash .我设法使用crypto生成与hashlib相同的 hash 值,这意味着问题出自object-hash

hashlib哈希库

import json
import hashlib

data = {
    'key1': 'value1',
    'key2': 'value2',
    'key3': 'value3',
}; 

json_data = json.dumps(data, separators=(',', ':')).encode('utf-8')

hash = hashlib.sha1()
hash.update(json_data)
print(hash.hexdigest())

# outputs f692755b3c38bc6b0dc376d775db8b07d6d5f256

crypto密码

const crypto = require('crypto');

const data = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3',
};

const stringData = JSON.stringify(data)

const shasum = crypto.createHash('sha1')
shasum.update(stringData)
console.log(shasum.digest('hex'));

// (same as hashlib) outputs f692755b3c38bc6b0dc376d775db8b07d6d5f256

object-hash (Tested with and without stringifying with no success) object-hash (经过和不经过字符串化测试均未成功)

const data = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3',
};

const stringData = JSON.stringify(data)


const objectHash = require('object-hash');
console.log(objectHash.sha1(stringData));

// outputs b5b0a100d7852748fe2e35bf00eeb536ad2d17d1

I saw in object-hash docs that the package is using crypto so it doesn't make sense for the two outputs to be different.我在object-hash文档中看到 package 正在使用crypto ,因此两个输出不同是没有意义的。

How can I make object-hash and hashlib / crypto all produce the same sha1 value?如何使object-hashhashlib / crypto都产生相同的 sha1 值?

It turns out that object-hash prefixes the variable for hashing with its type.事实证明, object-hash为散列变量加上了它的类型前缀。 In the case of strings I needed to add string:{string_length}: to the hash stream.对于字符串,我需要将string:{string_length}:添加到 hash stream。

hash = hashlib.sha1()

hash.update(f'string:{len(json_data)}:'.encode('utf-8')) # The line in question

hash.update(json_data)
res = hash.hexdigest()
print(res)

Having done that, the hashes produced by hashlib and crypto are the same as those of object-hash .完成后, hashlibcrypto生成的哈希值与object-hash相同。

Note: This is not documented and I had to look through the source code to find exactly how to prefix strings in particular.注意:这没有记录在案,我不得不查看源代码以找到具体如何为字符串添加前缀。 Other types have different prefixes.其他类型有不同的前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM