包 gorilla/securecookie 可对 Go Web 应用程序中的经过身份验证且可选加密的 Cookie 值进行编码和解码。
包 gorilla/securecookie 可对 Go Web 应用程序中的经过身份验证且可选加密的 Cookie 值进行编码和解码。
// Hash keys should be at least 32 bytes long var hashKey = []byte("very-secret") // Block keys should be 16 bytes (AES-128) or 32 bytes (AES-256) long. // Shorter keys may weaken the encryption used. var blockKey = []byte("a-lot-secret") var s = securecookie.New(hashKey, blockKey)
blockKey 为可选参数,用于加密 Cookie 值 - 将其设置为 nil 表示不使用加密。如果设置了该参数,长度必须与加密算法的块大小相对应。对于默认使用的 AES,有效的长度为 16、24 或 32 个字节,可选择 AES-128、AES-192 或 AES-256。可以使用便利函数 `GenerateRandomKey()` 生成强密钥。请注意,使用 `GenerateRandomKey()` 生成的密钥不会自动保存。应用程序重启时将创建新的密钥,而之前发布的 Cookie 将无法解码。
之后,使用相同的 SecureCookie 实例来解码和验证 Cookie 值:暂无开放 Issues,或尚未同步最近议题。