14378fbf39
* Checkpoint: it builds The config, db, model, network, os, and public packages have had some fixes to glaringly obvious flaws, dead code removed, and stylistic changes. * Style changes and old code removal in router Router needs a lot of work done to its (lack of) error handling. * Dead code removal and style changes Now up to util/email/email.go. After I'm finished with the initial sweep I'll go back and fix error handling and security issues. Then I'll fix the broken API. Then I'll go through to add documentation and fix code visibility. * Finish dead code removal and style changes Vendored libraries not touched. Everything still needs security fixes and documentation. There's also one case of broken functionality. * Fix accidental find-and-replace * Style, error checking, saftey, bug fix changes * Redo error checking erased during merge * Re-add merge-erased fix. Make Safe safe.
34 lignes
674 o
Go
34 lignes
674 o
Go
package crypto
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func GenerateMD5Hash(str string) (string, error) {
|
|
str = strings.ToLower(strings.TrimSpace(str))
|
|
hash := md5.New()
|
|
_, err := hash.Write([]byte(str))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
|
}
|
|
|
|
func GenerateRandomToken16() (string, error) {
|
|
return GenerateRandomToken(16)
|
|
}
|
|
|
|
func GenerateRandomToken32() (string, error) {
|
|
return GenerateRandomToken(32)
|
|
}
|
|
|
|
func GenerateRandomToken(n int) (string, error) {
|
|
token := make([]byte, n)
|
|
_, err := rand.Read(token)
|
|
// %x base 16, lower-case, two characters per byte
|
|
return fmt.Sprintf("%x", token), err
|
|
|
|
}
|