MD5 哈希转到 Python
MD5 hash Go to Python
我有以下代码:
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
)
type Payload struct {
HVIN []byte `json:"hvin"`
}
func main() {
vin := "1GBJC34R1VF063154"
md5 := md5.New()
md5Vin := md5.Sum([]byte(vin))
payload := &Payload{
HVIN: md5Vin,
}
b, _ := json.Marshal(payload)
fmt.Printf("%s", string(b))
}
如果我 运行 代码位于:https://play.golang.org/ 我得到以下输出:
{"hvin":"MUdCSkMzNFIxVkYwNjMxNTTUHYzZjwCyBOmACZjs+EJ+"}
如何在 Python 3 中复制它?
我试过以下方法:
import hashlib
result = hashlib.md5(b'1GBJC34R1VF063154')
print(result.hexdigest())
得到以下与 Go 给出的输出不匹配的输出:
a40f771ea430ae32dbc5e818387549d3
谢谢。
您使用的散列不正确:
vin := "1GBJC34R1VF063154"
md5 := md5.New()
md5.Write([]byte(vin))
md5Vin := md5.Sum(nil)
// This should give a40f771ea430ae32dbc5e818387549d3
fmt.Printf("%x",md5Vin)
payload := &Payload{
HVIN: md5Vin,
}
b, _ := json.Marshal(payload)
// This will print the base64-encoded version of the hash
fmt.Printf("%s", string(b))
另一个答案中的评论指出目标是匹配 Go 代码,即使 Go 代码不计算 VIN 的哈希值。
这里的 python3 代码与 Go 代码匹配。此代码 base64 对 VIN 和 MD5 初始值的串联进行编码。
vin := "1GBJC34R1VF063154"
b0 = vin.encode('utf-8')
b1 = hashlib.md5(b'').digest()
s = base64.b64encode(b0 + b1).decode('ascii') // to match Go's encoding/json
print(f'{{"hvin":"{s}"}}')
Go代码的作者大概是这样写的:
vin := "1GBJC34R1VF063154"
md5Vin := md5.Sum([]byte(vin))
payload := &Payload{
HVIN: md5Vin[:],
}
b, _ := json.Marshal(payload)
fmt.Printf("%s", string(b))
您好,您只需按照 https://golang.org/pkg/crypto/md5/#example_New
中的示例进行操作即可
Golang
package main
import (
"crypto/md5"
"fmt"
"io"
)
func main() {
h := md5.New()
vin := "1GBJC34R1VF063154"
io.WriteString(h, vin)
fmt.Printf("%x", h.Sum(nil)) // a40f771ea430ae32dbc5e818387549d3
}
Python
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>>
>>> result = hashlib.md5(b'1GBJC34R1VF063154')
>>> print(result.hexdigest())
a40f771ea430ae32dbc5e818387549d3
Golang 的 %x
在 fmt
中打印“... base 16, with lower-case letters for a-f”。
更多:https://golang.org/pkg/fmt/
我有以下代码:
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
)
type Payload struct {
HVIN []byte `json:"hvin"`
}
func main() {
vin := "1GBJC34R1VF063154"
md5 := md5.New()
md5Vin := md5.Sum([]byte(vin))
payload := &Payload{
HVIN: md5Vin,
}
b, _ := json.Marshal(payload)
fmt.Printf("%s", string(b))
}
如果我 运行 代码位于:https://play.golang.org/ 我得到以下输出:
{"hvin":"MUdCSkMzNFIxVkYwNjMxNTTUHYzZjwCyBOmACZjs+EJ+"}
如何在 Python 3 中复制它?
我试过以下方法:
import hashlib
result = hashlib.md5(b'1GBJC34R1VF063154')
print(result.hexdigest())
得到以下与 Go 给出的输出不匹配的输出:
a40f771ea430ae32dbc5e818387549d3
谢谢。
您使用的散列不正确:
vin := "1GBJC34R1VF063154"
md5 := md5.New()
md5.Write([]byte(vin))
md5Vin := md5.Sum(nil)
// This should give a40f771ea430ae32dbc5e818387549d3
fmt.Printf("%x",md5Vin)
payload := &Payload{
HVIN: md5Vin,
}
b, _ := json.Marshal(payload)
// This will print the base64-encoded version of the hash
fmt.Printf("%s", string(b))
另一个答案中的评论指出目标是匹配 Go 代码,即使 Go 代码不计算 VIN 的哈希值。
这里的 python3 代码与 Go 代码匹配。此代码 base64 对 VIN 和 MD5 初始值的串联进行编码。
vin := "1GBJC34R1VF063154"
b0 = vin.encode('utf-8')
b1 = hashlib.md5(b'').digest()
s = base64.b64encode(b0 + b1).decode('ascii') // to match Go's encoding/json
print(f'{{"hvin":"{s}"}}')
Go代码的作者大概是这样写的:
vin := "1GBJC34R1VF063154"
md5Vin := md5.Sum([]byte(vin))
payload := &Payload{
HVIN: md5Vin[:],
}
b, _ := json.Marshal(payload)
fmt.Printf("%s", string(b))
您好,您只需按照 https://golang.org/pkg/crypto/md5/#example_New
中的示例进行操作即可Golang
package main
import (
"crypto/md5"
"fmt"
"io"
)
func main() {
h := md5.New()
vin := "1GBJC34R1VF063154"
io.WriteString(h, vin)
fmt.Printf("%x", h.Sum(nil)) // a40f771ea430ae32dbc5e818387549d3
}
Python
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>>
>>> result = hashlib.md5(b'1GBJC34R1VF063154')
>>> print(result.hexdigest())
a40f771ea430ae32dbc5e818387549d3
Golang 的 %x
在 fmt
中打印“... base 16, with lower-case letters for a-f”。
更多:https://golang.org/pkg/fmt/