Download this file
| 1 | package idear |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | ) |
| 8 | |
| 9 | // newToken mints an invitation credential: 32 bytes of crypto/rand, |
| 10 | // hex-encoded (64 characters). The plaintext this returns exists only |
| 11 | // in Invite's return value and the emitted link — only its hash is |
| 12 | // ever persisted; see hashToken. |
| 13 | func newToken() (string, error) { |
| 14 | b := make([]byte, 32) |
| 15 | if _, err := rand.Read(b); err != nil { |
| 16 | return "", err |
| 17 | } |
| 18 | return hex.EncodeToString(b), nil |
| 19 | } |
| 20 | |
| 21 | // hashToken digests token with SHA-256, hex-encoded, lowercase. This |
| 22 | // is the only form of the token idear ever writes to the database — |
| 23 | // sessions already holds nothing but digests, and an addon must not |
| 24 | // be laxer than the core it rides on. |
| 25 | func hashToken(token string) string { |
| 26 | sum := sha256.Sum256([]byte(token)) |
| 27 | return hex.EncodeToString(sum[:]) |
| 28 | } |
| 29 | |