将 SHA1 十六进制转换为 Base 16 整数
Convert SHA1 Hex to Base 16 Integer
我需要一些帮助将算法从 Ruby 移植到 Go。
在Ruby我有:
hex = Digest::SHA1.hexdigest(str).to_i(16)
hex.to_s(32)
创建一个 SHA1 十六进制字符串,将其转换为 16 进制整数,然后再转换回 32 进制字符串。
我如何在 Go 中实现相同的目标?
这是一个示例代码(游乐场:https://play.golang.org/p/izBIq97-0S):
package main
import (
"crypto/sha1"
"encoding/base32"
"fmt"
"strings"
)
func main() {
// Input
exampleString := "example"
// SHA1 hash
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
// Conversion to base32
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(hashBytes))
fmt.Println(base32str)
}
我针对这个 Ruby 脚本进行了测试,输出匹配:
require 'digest'
str = "example"
hex = Digest::SHA1.hexdigest(str).to_i(16)
puts hex.to_s(32)
编辑:这是我的原始答案,它重现了 ruby 脚本中的每一步,但其中两个是不必要的(游乐场:https://play.golang.org/p/tyQt3ftb1j):
package main
import (
"crypto/sha1"
"encoding/base32"
"encoding/hex"
"fmt"
"math/big"
"strings"
)
func main() {
// Input
exampleString := "example"
// SHA1 hash
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
// Hexadecimal conversion
hexSha1 := hex.EncodeToString(hashBytes)
// Integer base16 conversion
intBase16, success := new(big.Int).SetString(hexSha1, 16)
if !success {
panic("Failed parsing big Int from hex")
}
// Conversion to base32
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(intBase16.Bytes()))
fmt.Println(base32str)
}
试试这个
h := sha1.New()
h.Write(content)
sha := h.Sum(nil) // "sha" is uint8 type, encoded in base16
shaStr := hex.EncodeToString(sha) // String representation
fmt.Printf("%x\n", sha)
fmt.Println(shaStr)
示例输出...
fcbc340d999e751840e17f862cc9eaf826cc6079
fcbc340d999e751840e17f862cc9eaf826cc6079
我需要一些帮助将算法从 Ruby 移植到 Go。
在Ruby我有:
hex = Digest::SHA1.hexdigest(str).to_i(16)
hex.to_s(32)
创建一个 SHA1 十六进制字符串,将其转换为 16 进制整数,然后再转换回 32 进制字符串。
我如何在 Go 中实现相同的目标?
这是一个示例代码(游乐场:https://play.golang.org/p/izBIq97-0S):
package main
import (
"crypto/sha1"
"encoding/base32"
"fmt"
"strings"
)
func main() {
// Input
exampleString := "example"
// SHA1 hash
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
// Conversion to base32
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(hashBytes))
fmt.Println(base32str)
}
我针对这个 Ruby 脚本进行了测试,输出匹配:
require 'digest'
str = "example"
hex = Digest::SHA1.hexdigest(str).to_i(16)
puts hex.to_s(32)
编辑:这是我的原始答案,它重现了 ruby 脚本中的每一步,但其中两个是不必要的(游乐场:https://play.golang.org/p/tyQt3ftb1j):
package main
import (
"crypto/sha1"
"encoding/base32"
"encoding/hex"
"fmt"
"math/big"
"strings"
)
func main() {
// Input
exampleString := "example"
// SHA1 hash
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
// Hexadecimal conversion
hexSha1 := hex.EncodeToString(hashBytes)
// Integer base16 conversion
intBase16, success := new(big.Int).SetString(hexSha1, 16)
if !success {
panic("Failed parsing big Int from hex")
}
// Conversion to base32
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(intBase16.Bytes()))
fmt.Println(base32str)
}
试试这个
h := sha1.New()
h.Write(content)
sha := h.Sum(nil) // "sha" is uint8 type, encoded in base16
shaStr := hex.EncodeToString(sha) // String representation
fmt.Printf("%x\n", sha)
fmt.Println(shaStr)
示例输出...
fcbc340d999e751840e17f862cc9eaf826cc6079
fcbc340d999e751840e17f862cc9eaf826cc6079