如何使用安全随机创建一个大型int

时间:2017-08-01 02:43:13

标签: go amadeus

我在java中有这个代码,我需要在Go中重现。

String nonce = new BigInteger(130, new SecureRandom()).toString(32);

是为GDS amadeus soap header 4生成nonce的唯一方法。

由于

1 个答案:

答案 0 :(得分:3)

使用包math/bigcrypto/rand。该片段看起来像:

//Max random value, a 130-bits integer, i.e 2^130 - 1
max := new(big.Int)
max.Exp(big.NewInt(2), big.NewInt(130), nil).Sub(max, big.NewInt(1))

//Generate cryptographically strong pseudo-random between 0 - max
n, err := rand.Int(rand.Reader, max)
if err != nil {
    //error handling
}

//String representation of n in base 32
nonce := n.Text(32)

可以在The Go Playground找到一个工作示例。