v0.0.171
This commit is contained in:
@@ -3,6 +3,7 @@ package cryptext
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
@@ -14,14 +15,15 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LatestPassHashVersion = 4
|
||||
const LatestPassHashVersion = 5
|
||||
|
||||
// PassHash
|
||||
// - [v0]: plaintext password ( `0|...` )
|
||||
// - [v1]: sha256(plaintext)
|
||||
// - [v2]: seed | sha256<seed>(plaintext)
|
||||
// - [v3]: seed | sha256<seed>(plaintext) | [hex(totp)]
|
||||
// - [v4]: bcrypt(plaintext) | [hex(totp)]
|
||||
// - [v0]: plaintext password ( `0|...` ) // simple, used to write PW's directly in DB
|
||||
// - [v1]: sha256(plaintext) // simple hashing
|
||||
// - [v2]: seed | sha256<seed>(plaintext) // add seed
|
||||
// - [v3]: seed | sha256<seed>(plaintext) | [hex(totp)] // add TOTP support
|
||||
// - [v4]: bcrypt(plaintext) | [hex(totp)] // use proper bcrypt
|
||||
// - [v5]: bcrypt(sha512(plaintext)) | [hex(totp)] // hash pw before bcrypt (otherwise max pw-len = 72)
|
||||
type PassHash string
|
||||
|
||||
func (ph PassHash) Valid() bool {
|
||||
@@ -109,7 +111,21 @@ func (ph PassHash) Data() (_version int, _seed []byte, _payload []byte, _totp bo
|
||||
totp := false
|
||||
totpsecret := make([]byte, 0)
|
||||
if split[2] != "0" {
|
||||
totpsecret, err = hex.DecodeString(split[3])
|
||||
totpsecret, err = hex.DecodeString(split[2])
|
||||
totp = true
|
||||
}
|
||||
return int(version), nil, payload, totp, totpsecret, true
|
||||
}
|
||||
|
||||
if version == 5 {
|
||||
if len(split) != 3 {
|
||||
return -1, nil, nil, false, nil, false
|
||||
}
|
||||
payload := []byte(split[1])
|
||||
totp := false
|
||||
totpsecret := make([]byte, 0)
|
||||
if split[2] != "0" {
|
||||
totpsecret, err = hex.DecodeString(split[2])
|
||||
totp = true
|
||||
}
|
||||
return int(version), nil, payload, totp, totpsecret, true
|
||||
@@ -156,6 +172,14 @@ func (ph PassHash) Verify(plainpass string, totp *string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
if version == 5 {
|
||||
if !hastotp {
|
||||
return bcrypt.CompareHashAndPassword(payload, hash512(plainpass)) == nil
|
||||
} else {
|
||||
return bcrypt.CompareHashAndPassword(payload, hash512(plainpass)) == nil && totpext.Validate(totpsecret, *totp)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -209,6 +233,12 @@ func (ph PassHash) ClearTOTP() (PassHash, error) {
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
if version == 5 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[2] = "0"
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
return "", errors.New("unknown version")
|
||||
}
|
||||
|
||||
@@ -242,6 +272,12 @@ func (ph PassHash) WithTOTP(totpSecret []byte) (PassHash, error) {
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
if version == 5 {
|
||||
split := strings.Split(string(ph), "|")
|
||||
split[2] = hex.EncodeToString(totpSecret)
|
||||
return PassHash(strings.Join(split, "|")), nil
|
||||
}
|
||||
|
||||
return "", errors.New("unknown version")
|
||||
}
|
||||
|
||||
@@ -271,6 +307,10 @@ func (ph PassHash) Change(newPlainPass string) (PassHash, error) {
|
||||
return HashPasswordV4(newPlainPass, langext.Conditional(hastotp, totpsecret, nil))
|
||||
}
|
||||
|
||||
if version == 5 {
|
||||
return HashPasswordV5(newPlainPass, langext.Conditional(hastotp, totpsecret, nil))
|
||||
}
|
||||
|
||||
return "", errors.New("unknown version")
|
||||
}
|
||||
|
||||
@@ -279,7 +319,24 @@ func (ph PassHash) String() string {
|
||||
}
|
||||
|
||||
func HashPassword(plainpass string, totpSecret []byte) (PassHash, error) {
|
||||
return HashPasswordV4(plainpass, totpSecret)
|
||||
return HashPasswordV5(plainpass, totpSecret)
|
||||
}
|
||||
|
||||
func HashPasswordV5(plainpass string, totpSecret []byte) (PassHash, error) {
|
||||
var strtotp string
|
||||
|
||||
if totpSecret == nil {
|
||||
strtotp = "0"
|
||||
} else {
|
||||
strtotp = hex.EncodeToString(totpSecret)
|
||||
}
|
||||
|
||||
payload, err := bcrypt.GenerateFromPassword(hash512(plainpass), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return PassHash(fmt.Sprintf("5|%s|%s", string(payload), strtotp)), nil
|
||||
}
|
||||
|
||||
func HashPasswordV4(plainpass string, totpSecret []byte) (PassHash, error) {
|
||||
@@ -340,6 +397,13 @@ func HashPasswordV0(plainpass string) (PassHash, error) {
|
||||
return PassHash(fmt.Sprintf("0|%s", plainpass)), nil
|
||||
}
|
||||
|
||||
func hash512(s string) []byte {
|
||||
h := sha512.New()
|
||||
h.Write([]byte(s))
|
||||
bs := h.Sum(nil)
|
||||
return bs
|
||||
}
|
||||
|
||||
func hash256(s string) []byte {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(s))
|
||||
|
Reference in New Issue
Block a user