forked from External/ergo
draft/resume-0.2 implementation, message history support
This commit is contained in:
parent
70364f5f67
commit
a0bf548fc5
28 changed files with 1294 additions and 317 deletions
30
irc/utils/crypto.go
Normal file
30
irc/utils/crypto.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// generate a secret token that cannot be brute-forced via online attacks
|
||||
func GenerateSecretToken() string {
|
||||
// 128 bits of entropy are enough to resist any online attack:
|
||||
var buf [16]byte
|
||||
rand.Read(buf[:])
|
||||
// 32 ASCII characters, should be fine for most purposes
|
||||
return hex.EncodeToString(buf[:])
|
||||
}
|
||||
|
||||
// securely check if a supplied token matches a stored token
|
||||
func SecretTokensMatch(storedToken string, suppliedToken string) bool {
|
||||
// XXX fix a potential gotcha: if the stored token is uninitialized,
|
||||
// then nothing should match it, not even supplying an empty token.
|
||||
if len(storedToken) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return subtle.ConstantTimeCompare([]byte(storedToken), []byte(suppliedToken)) == 1
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue