Python使用pbkdf2进行密码哈希处理。

huangapple go评论85阅读模式
英文:

Python hashing password with pbkdf2

问题

以下是GoLang代码的翻译:

我有一个在GoLang上实现的代码片段它运行良好

    package main
    
    import (
        "crypto/sha256"
        "encoding/hex"
        "fmt"
    
        "golang.org/x/crypto/pbkdf2"
    )
    
    func main() {
        newPasswd := pbkdf2.Key([]byte("test"), []byte("Gare5vgHIo"), 10000, 50, sha256.New)
        fmt.Println(hex.EncodeToString(newPasswd), nil)
    }

我正在尝试在Python上做同样的事情。

def main():

    password = b'test'
    salt = b'Gare5vgHIo'
    iterations = 1000

    key = pbkdf2_hmac("sha256", password, salt, iterations, 50)

    print(key)
    print(key.decode())

但是 key.decode() 方法抛出了一个错误:

UnicodeDecodeError('utf-8', b'\xd9\xb2;\x0f$\x9a\x9c\t\x91\x16\x81\xb8a\x00\xd8\xdd{e.\xa9\x7f\xe9\x92dH\xa6\x05\x16\xd8\xbb\xfdy\x13\xc5D\x1c\xa2\x93e\xbf{\\\x19\xc1\x8df\xf4\xbft\xe2', 5, 6, 'invalid start byte')

对于Python代码,我做错了什么?

英文:

I have the snippet of code implemented on GoLang that works fine

package main

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"

	"golang.org/x/crypto/pbkdf2"
)

func main() {
	newPasswd := pbkdf2.Key([]byte("test"), []byte("Gare5vgHIo"), 10000, 50, sha256.New)
	fmt.Println(hex.EncodeToString(newPasswd), nil)
}

I am trying to do the same on Python

def main():

    password = b'test'
    salt = b'Gare5vgHIo'
    iterations = 1000

    key = pbkdf2_hmac("sha256", password, salt, iterations, 50)

    print(key)
    print(key.decode())

But key.decode() method throws an error:

UnicodeDecodeError('utf-8', b'\xd9\xb2;\x0f$\x9a\x9c\t\x91\x16\x81\xb8a\x00\xd8\xdd{e.\xa9\x7f\xe9\x92dH\xa6\x05\x16\xd8\xbb\xfdy\x13\xc5D\x1c\xa2\x93e\xbf{\\\x19\xc1\x8df\xf4\xbft\xe2', 5, 6, 'invalid start byte')

What am I doing wrong for the Python code?

答案1

得分: 3

.decode()试图通过将字节解释为UTF-8编码的数据来将其从bytes转换为str(您可以传递一个参数来使用不同的编解码器,但它基本上是用于将编码的文本数据转换回str,而随机的原始字节不是文本的编码表示)。UTF-8是一种自检编码;随机字节很少通过自检。

如果您想将字节显示为十六进制字符串,请使用:

print(key.hex())

其中bytes.hex是一个bytes方法,直接将其转换为长度为两倍的str,每个字节表示为两个十六进制字符。

在非常旧的(3.5之前)Python版本中,bytes没有.hex()方法,所以您需要导入 binascii模块并执行

print(binascii.hexlify(key))

但在当前支持的任何Python版本上都不需要这样做。

英文:

.decode() is trying to convert from bytes to str by interpreting the bytes as UTF-8 encoded data (you can pass it an argument to use a different codec, but it's fundamentally intended for converting encoded representations of text data back to str, and random raw bytes are not an encoded representation of text). UTF-8 is a self-checking encoding; random bytes rarely pass the self-checks.

If you want to display the bytes as a hexadecimal string, use:

print(key.hex())

where bytes.hex is a bytes method that converts directly to a str of twice the length, representing each byte as two hexadecimal characters.

On very old (pre-3.5) version of Python, bytes does not have a .hex() method, so you'd import the binascii module and do:

print(binascii.hexlify(key))

but that's not necessary on any currently supported version of Python.

huangapple
  • 本文由 发表于 2022年6月18日 00:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/72661995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定