Here is an example code (playground: https://play.golang.org/p/izBIq97-0S ):
package main
import (
"crypto/sha1"
"encoding/base32"
"fmt"
"strings"
)
func main() {
exampleString := "example"
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(hashBytes))
fmt.Println(base32str)
}
I tested it on this Ruby script and the output is the same:
require 'digest'
str = "example"
hex = Digest::SHA1.hexdigest(str).to_i(16)
puts hex.to_s(32)
Change: here is my original answer that reproduces every step from the ruby script, but two of them are not needed (site: https://play.golang.org/p/tyQt3ftb1j ):
package main
import (
"crypto/sha1"
"encoding/base32"
"encoding/hex"
"fmt"
"math/big"
"strings"
)
func main() {
exampleString := "example"
hash := sha1.New()
hash.Write([]byte(exampleString))
hashBytes := hash.Sum(nil)
hexSha1 := hex.EncodeToString(hashBytes)
intBase16, success := new(big.Int).SetString(hexSha1, 16)
if !success {
panic("Failed parsing big Int from hex")
}
base32str := strings.ToLower(base32.HexEncoding.EncodeToString(intBase16.Bytes()))
fmt.Println(base32str)
}
source
share