gpt4 book ai didi

c# - 使用 Go 编码 UTF-8

转载 作者:IT王子 更新时间:2023-10-29 01:54:43 24 4
gpt4 key购买 nike

我正在尝试在 Go 中找到与以下 C# 代码等效的代码。

pwd = "abc123";
encoding = Encoding.UTF8;
SHA1 sha1 = SHA1.Create();
byte[] hash = sha1.ComputeHash(encoding.GetBytes(text));

我知道有一个 crypto/sha1 Go 中的包。我知道我可以跑:

pwd := "abc123"
hasher := sha1.New() // SHA1.Create();
hasher.Write([]byte(pwd)) // sha1.ComputeHash but without encoding in UTF8 ?

我不确定在散列时如何获得正确的编码。我想知道我是否可以得到一些帮助来转换它

最佳答案

根据文档:

A string literal, absent byte-level escapes, always holds valid UTF-8 sequences.

因此,如果字符串在 Golang 源代码中,则无需将其编码为 utf8。但是,如果字符串来自输入,则 utf8包是你的 friend 。

关于c# - 使用 Go 编码 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40383431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com