Golang:crypto Sign Cookie for security

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

You can improve the security of your Go Web application by cryptographically signing cookies using Sugarcookie. A small library used to for signing cookie values the can verify and trust on a user ' s return visit. This gives your strong trust and allows you to decouple the cookie from a session on a specific server.

The signed cookie consists of a secret value, a timestamp, and a unique identifier; Which is one-way hashed, then concatenated with the Timestam and unique identifer. Then everything was Base64 encoded making it suitable for storing in a cookie.

hash := sha256.Sum256([]byte(secret + strconv.FormatInt(t, 10) + uniqueUserId))value := hex.EncodeToString(hash[:]) + "-" + strconv.FormatInt(t, 10) + "-" + uniqueUserIdsignature := base64.StdEncoding.EncodeToString([]byte(value))

You verify the cookies are valid by replaying the hashes with the secret and the supplied timestamp and unique ID.

Including the time as part of the hash means you can use it as a secondary mechanism to invalidate the cookie. If It's too old, maybe an half a hour, you know it's no longer good, no need to test it; Just invalidate it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.