比较Python中的salt哈希密码以获得授权

2024-10-02 00:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在公司里,我们将系统的一些部分从.NET迁移到python。其中一个部分是日志/授权模块。我需要用python CryptographyManager.CompareHash Method实现。它从数据库中获取经过salt和hash处理的密码,并将其与用户输入(明文)进行比较。要做到这一点,我必须对用户输入进行加密和散列,然后使用数据库中的散列密码进行检查

数据库中的密码由Microsoft Enterprise Library.Security.Cryptography哈希处理。详情如下:

Symmetric Crypto Providers: algorithmType="System.Security.Cryptography.AesManaged name="AES"

Hash Providers: algorithmType="System.Security.Cryptography.SHA256Managed saltEnabled="true" name="SHA256"

我发现密码是这样写的:Base64(salt + SHA256(salt + password))。salt是散列密码的前16个字节

我试图做的是从数据库中的散列密码中获取salt,然后将下一个散列和salt用户输入(纯文本)进行比较

不幸的是,最终没有成功。散列是不同的。可能我的de/编码有问题

我的代码如下所示:

import base64
import hashlib

hash = 'EwxBhfN0fM5Puv8/z+3/L50QvdU6BHFb4XQU9xtye/mOXJ8tBPc3tIyW7dEiZrvA'
password = 'some_plain_password' 

#password = password.encode()
password = base64.b64decode(password)

#hash to byte
b_hash = base64.b64decode (hash)
print(b_hash)

#get salt, first 16 bytes
salt = b_hash[:15]
salt=base64.b64encode(salt)
print(salt)

m = hashlib.sha256()
m.update(salt)
m.update(password)

print(m.hexdigest())

Tags: 用户name数据库密码passwordhashsystemsalt
2条回答

如果不使用示例明文和哈希来验证结果,则执行此操作有点棘手,但您的代码应该实现您在问题中列出的算法:

Base64(salt + SHA256(salt + password))

我想你想要这样的东西:

import base64
import hashlib

hash = 'EwxBhfN0fM5Puv8/z+3/L50QvdU6BHFb4XQU9xtye/mOXJ8tBPc3tIyW7dEiZrvA'
salt = base64.b64decode(hash)[:16]  # Use 16 here, not 15

password = 'some_plain_password'

# First let's do the SHA256(salt + password) part
m = hashlib.sha256()
m.update(salt)
m.update(password.encode('utf-8'))

# Now let's feed that into the Base64 part
new_hash = base64.encode(salt + m.digest())

我的帖子不是100%的答案,而是故事的结尾。 问题在于,没有官方来源CryptographyManager.Compare/Create Hash的工作原理 我尝试了很多与Base64(salt+SHA256(salt+password))的组合,但都没有成功。 最后,我决定重写C代码。 如果有人正在寻找对密码进行盐析和散列的python代码,并比较两种散列,下面是我的工作示例:

import base64
import hashlib
import os


class Hashing(object):

    # base64( SHA256(password + salt) + salt)

    # generate new salt (default 16 bytes_
    def generate_new_salt(self, salt_ln=16):

        self.new_salt = os.urandom(salt_ln)

        print(f'new salt: {self.new_salt}')

        return self.new_salt

    # get salt from hash
    def get_old_salt(self, input_hash, salt_ln=16):

        self.old_salt = base64.b64decode(input_hash)[-salt_ln:]

        print(f'old salt: {self.old_salt}')

        return self.old_salt

    # compute hash using parameters
    def compute_hash(self, password, salt):

        self.salt = salt
        self.enc_password = password.encode()

        # hashing SHA256(password + salt)
        hash_object = hashlib.sha256(self.enc_password + salt)

        # add salt to hash and encode to base64
        hash_b64 = base64.b64encode(hash_object.digest() + salt)

        print(f'new_hash: {hash_b64}')
        return hash_b64

    # create hash from new or old salt
    def create_hash(self, password, salt_ln=16,old_salt=None):

        if old_salt:    #if old salt then use it
            self.salt = old_salt
        else:           #else generate new salt
            self.salt = Hashing().generate_new_salt(salt_ln)


        hash = Hashing().compute_hash(password, self.salt)

        return hash

    # compare input hash with created using salt get from input
    def compare_hashes(self, password, old_hash, salt_ln=16):

        self.enc_password = password.encode()

        #get salt from input hash
        self.old_salt = Hashing().get_old_salt(old_hash, salt_ln)

        #recreat input hash
        re_hash = Hashing().create_new_hash(password,salt_ln, self.old_salt)

        print(f'Compare: old_hash: {old_hash}')
        print(f'Compare: new_hash: {re_hash}')

        #compare
        if re_hash.decode() == old_hash.decode():
            return True
        else:
            return False


#code below is just for testing

NewSalt = Hashing().generate_new_salt()

Hash = Hashing().create_new_hash('pass')

OldSalt = Hashing().get_old_salt(Hash)

CompareHash = Hashing().compare_hashes('pass', Hash)

if CompareHash:
    print('HASHES THE SAME')
else:
    print('NOT THE SAME')

print(CompareHash)

相关问题 更多 >

    热门问题