import (
"encoding/binary"
"golang.org/x/crypto/bcrypt"
+ "io"
+ "log"
+ "slices"
)
const GuestAccount = "guest" // default account used when no login is provided for a connection
// Read implements io.Reader interface for Account
func (a *Account) Read(p []byte) (n int, err error) {
fields := []Field{
- NewField(fieldUserName, []byte(a.Name)),
- NewField(fieldUserLogin, negateString([]byte(a.Login))),
- NewField(fieldUserAccess, a.Access[:]),
+ NewField(FieldUserName, []byte(a.Name)),
+ NewField(FieldUserLogin, encodeString([]byte(a.Login))),
+ NewField(FieldUserAccess, a.Access[:]),
}
if bcrypt.CompareHashAndPassword([]byte(a.Password), []byte("")) != nil {
- fields = append(fields, NewField(fieldUserPassword, []byte("x")))
+ fields = append(fields, NewField(FieldUserPassword, []byte("x")))
}
fieldCount := make([]byte, 2)
binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
- p = append(p, fieldCount...)
-
+ var fieldBytes []byte
for _, field := range fields {
- p = append(p, field.Payload()...)
+ fieldBytes = append(fieldBytes, field.Payload()...)
+ }
+
+ return copy(p, slices.Concat(fieldCount, fieldBytes)), io.EOF
+}
+
+// hashAndSalt generates a password hash from a users obfuscated plaintext password
+func hashAndSalt(pwd []byte) string {
+ hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
+ if err != nil {
+ log.Println(err)
}
- return len(p), nil
+ return string(hash)
}