diff --git a/main.go b/main.go index 36329a6..349b2ab 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,10 @@ package main import ( "crypto" - "crypto/rand" "encoding/base64" "fmt" - "io" "log" + mrand "math/rand" "net/http" "os" "path" @@ -186,17 +185,35 @@ func createPasswordEntry(email, password string) string { c := crypto.SHA512.New() c.Write([]byte(password)) // hash := c.Sum(nil) - salt := getSalt() - s := base64.StdEncoding.EncodeToString(salt) + salt := RandStringBytesMaskImprSrc(5) + str := base64.StdEncoding.EncodeToString([]byte(string(c.Sum(nil)) + string(salt))) - return fmt.Sprintf("%s|{SHA512-CRYPT}$6$%s$%s", email, s, str) + return fmt.Sprintf("%s|{SHA512-CRYPT}$6$%s$%s", email, salt, str) } -func getSalt() []byte { - salt := make([]byte, 10) - _, err := io.ReadFull(rand.Reader, salt) - if err != nil { - log.Fatal(err) +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +const ( + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = src.Int63(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- } - return salt + + return string(b) }