将C#的SHA1代码转换为Golang。

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

Converting C# SHA1 code to Golang

问题

我需要将一些旧的C#代码转换为Golang,但我卡在某个地方。C#代码如下所示:

byte[] bytes = Encoding.Unicode.GetBytes(password);
byte[] src = Encoding.Unicode.GetBytes(salt);
byte[] dst = new byte[src.Length + bytes.Length];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
byte[] inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);

所以我逐行检查了代码,我理解他是将盐和密码转换为字节数组,然后将这些数组复制到'dst'数组中。然后他使用了SHA1算法,并将该数组转换为base64字符串。

我的Golang代码如下,但它没有创建与数据库中存储的相同的字符串。

s := "fish123"
salt := "227EA7ABD26E40608A6EDEB209058D93A632D1D1A52246D0A27F6E447B16AEBF"

h1 := sha1.New()
h1.Write([]byte(salt))
h1.Write([]byte(s))

hashedPassword := base64.StdEncoding.EncodeToString(h1.Sum(nil))

有人能找到我的错误吗?谢谢。

英文:

I need to convert some old C# code to Golang and I stuck somewhere. C# code looks like this
`

 byte[] bytes = Encoding.Unicode.GetBytes(password);
 byte[] src = Encoding.Unicode.GetBytes(salt);
 byte[] dst = new byte[src.Length + bytes.Length];
 Buffer.BlockCopy(src, 0, dst, 0, src.Length);
 Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
 HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
 byte[] inArray = algorithm.ComputeHash(dst);
 return Convert.ToBase64String(inArray);

so I examined the code line by line and as I understand he used convert the salt and password byte array then he copied these arrays to 'dst' array. Then he used SHA1 algorithm and convert this array to base64string.

My Golang code looks like this but It doesn't create the same string which stores on database.

s := "fish123"
salt := "227EA7ABD26E40608A6EDEB209058D93A632D1D1A52246D0A27F6E447B16AEBF"

h1 := sha1.New()
h1.Write([]byte(salt))
h1.Write([]byte(s))

hashedPassword := base64.StdEncoding.EncodeToString(h1.Sum(nil))

Can anyone find my fault? Thanks

答案1

得分: 4

问题在于C#代码使用了Encoding.Unicode。在Go语言中应该这样写:

package main

import (
	"crypto/sha1"
	"encoding/base64"
	"encoding/binary"
	"fmt"
	"unicode/utf16"
)

func main() {
	s := "fish123"
	salt := "227EA7ABD26E40608A6EDEB209058D93A632D1D1A52246D0A27F6E447B16AEBF"

	h1 := sha1.New()
	h1.Write(convertUTF16ToLittleEndianBytes(salt))
	h1.Write(convertUTF16ToLittleEndianBytes(s))

	b64 := base64.StdEncoding.EncodeToString(h1.Sum(nil))
	fmt.Println(b64)
}

func convertUTF16ToLittleEndianBytes(s string) []byte {
	u := utf16.Encode([]rune(s))
	b := make([]byte, 2*len(u))
	for index, value := range u {
		binary.LittleEndian.PutUint16(b[index*2:], value)
	}
	return b
}

convertUTF16ToLittleEndianBytes 函数是从 Stack Overflow 上的另一个回答中获取的。

英文:

The problem is that the C# code is using the Encoding.Unicode. In Go it should be:

package main

import (
	"crypto/sha1"
	"encoding/base64"
	"encoding/binary"
	"fmt"
	"unicode/utf16"
)

func main() {
	s := "fish123"
	salt := "227EA7ABD26E40608A6EDEB209058D93A632D1D1A52246D0A27F6E447B16AEBF"

	h1 := sha1.New()
	h1.Write(convertUTF16ToLittleEndianBytes(salt))
	h1.Write(convertUTF16ToLittleEndianBytes(s))

	b64 := base64.StdEncoding.EncodeToString(h1.Sum(nil))
	fmt.Println(b64)
}

func convertUTF16ToLittleEndianBytes(s string) []byte {
	u := utf16.Encode([]rune(s))
	b := make([]byte, 2*len(u))
	for index, value := range u {
		binary.LittleEndian.PutUint16(b[index*2:], value)
	}
	return b
}

The convertUTF16ToLittleEndianBytes was taken from another response on SO.

huangapple
  • 本文由 发表于 2017年3月7日 20:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/42647857.html
匿名

发表评论

匿名网友

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

确定