将SHA1十六进制转换为Base 16整数。

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

Convert SHA1 Hex to Base 16 Integer

问题

我需要帮助将一个算法从Ruby转换到Go。

在Ruby中,我有以下代码:

hex = Digest::SHA1.hexdigest(str).to_i(16)
hex.to_s(32)

它创建一个SHA1十六进制字符串,将其转换为十六进制的整数,然后再转换为基于32的字符串。

在Go中,你可以使用以下代码实现相同的功能:

package main

import (
    "crypto/sha1"
    "fmt"
    "strconv"
)

func main() {
    str := "your_string"

    hash := sha1.New()
    hash.Write([]byte(str))
    hex := hash.Sum(nil)

    intValue := new(big.Int)
    intValue.SetString(fmt.Sprintf("%x", hex), 16)

    base32String := strconv.FormatInt(intValue.Int64(), 32)

    fmt.Println(base32String)
}

请注意,你需要在代码中替换"your_string"为你想要计算SHA1的字符串。

英文:

I need some help porting an algorithm from Ruby to Go.

In Ruby I have:

hex = Digest::SHA1.hexdigest(str).to_i(16)
hex.to_s(32)

Which creates a SHA1 hex string, converts it to an integer in base 16 and then back to a string in base 32.

How do I achieve the same in Go?

答案1

得分: 9

以下是示例代码的中文翻译:

package main

import (
	"crypto/sha1"
	"encoding/base32"
	"fmt"
	"strings"
)

func main() {
	// 输入
	exampleString := "example"

	// SHA1 哈希
	hash := sha1.New()
	hash.Write([]byte(exampleString))
	hashBytes := hash.Sum(nil)

	// 转换为 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 脚本的每个步骤,但其中两个步骤是不必要的(playground: https://play.golang.org/p/tyQt3ftb1j):

package main

import (
    "crypto/sha1"
    "encoding/base32"
    "encoding/hex"
    "fmt"
    "math/big"
    "strings"
)

func main() {
    // 输入
    exampleString := "example"

    // SHA1 哈希
    hash := sha1.New()
    hash.Write([]byte(exampleString))
    hashBytes := hash.Sum(nil)

    // 十六进制转换
    hexSha1 := hex.EncodeToString(hashBytes)

    // 整数 base16 转换
    intBase16, success := new(big.Int).SetString(hexSha1, 16)
    if !success {
        panic("Failed parsing big Int from hex")
    }

    // 转换为 base32
    base32str := strings.ToLower(base32.HexEncoding.EncodeToString(intBase16.Bytes()))

    fmt.Println(base32str)
}

希望对你有帮助!

英文:

Here is an example code (playground : 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)
}

I tested it agaisnt this Ruby script and the ouput matches :

require 'digest'

str = "example"
hex = Digest::SHA1.hexdigest(str).to_i(16)

puts hex.to_s(32)

Edit : here is my original answer, which reproduces every step from the ruby script, but two of them are unnecessary (playground : 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)
}

答案2

得分: 3

请尝试这个 🙂

<!-- language: golang -->

h := sha1.New()
h.Write(content)
sha := h.Sum(nil)  // "sha" 是 uint8 类型,以 base16 编码

shaStr := hex.EncodeToString(sha)  // 字符串表示

fmt.Printf("%x\n", sha)
fmt.Println(shaStr)

示例输出...

<!-- language: shell -->

fcbc340d999e751840e17f862cc9eaf826cc6079
fcbc340d999e751840e17f862cc9eaf826cc6079
英文:

try this 🙂

<!-- language: golang -->

h := sha1.New()
h.Write(content)
sha := h.Sum(nil)  // &quot;sha&quot; is uint8 type, encoded in base16

shaStr := hex.EncodeToString(sha)  // String representation

fmt.Printf(&quot;%x\n&quot;, sha)
fmt.Println(shaStr)

Example Output...

<!-- language: shell -->

fcbc340d999e751840e17f862cc9eaf826cc6079
fcbc340d999e751840e17f862cc9eaf826cc6079

huangapple
  • 本文由 发表于 2015年9月13日 11:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/32545949.html
匿名

发表评论

匿名网友

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

确定