douquanjie9326 2014-01-16 11:17
浏览 669
已采纳

Golang生成一个32字节的密钥

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 2014-01-16 11:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误