交付双端基础、实时联系人和隔离浏览器
This commit is contained in:
1 parent
43687d5e7e
commit
67fef3ff87
59 files changed
+5094
-1
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const deviceProof = "XHChat device authentication v1\n"
|
||||
|
||||
var Encoding = base64.RawURLEncoding
|
||||
|
||||
func Secret() string {
|
||||
var bytes [32]byte
|
||||
rand.Read(bytes[:])
|
||||
return Encoding.EncodeToString(bytes[:])
|
||||
}
|
||||
|
||||
func Proof(publicKey, challenge, signature string) ([]byte, bool) {
|
||||
key, keyErr := Encoding.DecodeString(publicKey)
|
||||
sig, sigErr := Encoding.DecodeString(signature)
|
||||
nonce, nonceErr := Encoding.DecodeString(challenge)
|
||||
if keyErr != nil || sigErr != nil || nonceErr != nil || len(key) != ed25519.PublicKeySize || len(nonce) != 32 {
|
||||
return nil, false
|
||||
}
|
||||
return key, ed25519.Verify(key, []byte(deviceProof+challenge), sig)
|
||||
}
|
||||
|
||||
func PasswordHash(password string) string {
|
||||
var salt [16]byte
|
||||
rand.Read(salt[:])
|
||||
key := derive(password, salt[:])
|
||||
return fmt.Sprintf("$argon2id$v=19$m=65536,t=3,p=2$%s$%s",
|
||||
base64.RawStdEncoding.EncodeToString(salt[:]), base64.RawStdEncoding.EncodeToString(key))
|
||||
}
|
||||
|
||||
func VerifyPassword(password, hash string) bool {
|
||||
fields := strings.Split(hash, "$")
|
||||
if len(fields) != 6 || strings.Join(fields[:4], "$") != "$argon2id$v=19$m=65536,t=3,p=2" {
|
||||
return false
|
||||
}
|
||||
salt, saltErr := base64.RawStdEncoding.DecodeString(fields[4])
|
||||
key, keyErr := base64.RawStdEncoding.DecodeString(fields[5])
|
||||
if saltErr != nil || keyErr != nil || len(salt) != 16 || len(key) != 32 {
|
||||
return false
|
||||
}
|
||||
return subtle.ConstantTimeCompare(derive(password, salt), key) == 1
|
||||
}
|
||||
|
||||
func derive(password string, salt []byte) []byte {
|
||||
return argon2.IDKey([]byte(password), salt, 3, 64*1024, 2, 32)
|
||||
}
|
||||
Reference in new issue
Block a user