dongyang9813 2014-10-03 19:06
浏览 72
已采纳

大猩猩会话无效密钥大小

When I create new cookie store and do like:

var store = sessions.NewCookieStore(securecookie.GenerateRandomKey(1), securecookie.GenerateRandomKey(2))

I've got the the error message

crypto/aes: invalid key size 2

Why what do I wrong? When I look at the function definition

// NewCookieStore returns a new CookieStore.
//
// Keys are defined in pairs to allow key rotation, but the common case is
// to set a single authentication key and optionally an encryption key.
//
// The first key in a pair is used for authentication and the second for
// encryption. The encryption key can be set to nil or omitted in the last
// pair, but the authentication key is required in all pairs.
//
// 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.
//
// Use the convenience function securecookie.GenerateRandomKey() to create
// strong keys.
func NewCookieStore(keyPairs ...[]byte) *CookieStore {
    return &CookieStore{
        Codecs: securecookie.CodecsFromPairs(keyPairs...),
        Options: &Options{
            Path:   "/",
            MaxAge: 86400 * 30,
        },
    }
}

I think pass the right parameter.

  • 写回答

1条回答 默认 最新

  • doujia2386 2014-10-03 19:12
    关注

    From the documentation you linked:

    // 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.

    So you could use something like this:

    //replace 16 with 24 for 192bit or 32 for 256bit.
    var store = sessions.NewCookieStore(securecookie.GenerateRandomKey(16), 
                                        securecookie.GenerateRandomKey(16))
    

    // edit

    Also @elithrar made a very valid point in comments, so keep it in mind:

    Also note that restarting your application means that it cannot read existing sessions (as new keys are generated every time) when using this method.

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

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类