Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang generating a 32 byte key

Tags:

go

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?

like image 239
Lee Avatar asked Jan 16 '14 11:01

Lee


1 Answers

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.

like image 106
Agis Avatar answered Sep 21 '22 12:09

Agis