douquanjie9326
douquanjie9326
2014-01-16 11:17

Golang生成一个32字节的密钥

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

I am using this library for sessions.

https://github.com/codegangsta/martini-contrib/tree/master/sessions

It says that:

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

How do I generate a 64 byte key, is it as straightforward as []byte"64characterslongstring", I thought it is not always so straight forward?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douke1942 douke1942 7年前

    To generate a slice of 64 random bytes:

    package main
    
    import "crypto/rand"
    
    func main() {
        key := make([]byte, 64)
    
        _, err := rand.Read(key)
        if err != nil {
            // handle error here
        }
    }
    

    Demo here.

    点赞 评论 复制链接分享

为你推荐