#Bcrypt hash code to SHA256
12 messages · Page 1 of 1 (latest)
That's too much code. I can't easily find where you're ashing using SHA256
oh i sent the wrong code
This is the right user.go:
https://pastebin.com/fCpRgcXf
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
That's just plain hashing, you need to use salted hashing.
The kind of hashing done in this use case is different from just plain hashing.
The $ in that string that you want are delimiters.
So it says SHA (SHA-1?) and then there's a salt e1be2894058bc459 and then the correct result at the end.
Why do you want that format in particular?
Beacuse the server where im creating this website stores passwords in this format.
Now i tried something to do with this, but its still wrong.
func (user *User) HashPassword(password string) error {
salt := make([]byte, 16)
_, err := rand.Read(salt)
if err != nil {
return err
}
hash := sha256.New()
hash.Write([]byte(password + fmt.Sprintf("%x", salt)))
hashBytes := hash.Sum(nil)
user.Password = fmt.Sprintf("$SHA$%x$%x", salt, hashBytes)
return nil
}
func (user *User) CheckPassword(providedPassword string) error {
storedParts := strings.Split(user.Password, "$")
if len(storedParts) != 4 {
return errors.New("invalid stored password format")
}
storedSalt, err := hex.DecodeString(storedParts[2])
if err != nil {
return err
}
storedHash := storedParts[3]
hash := sha256.New()
hash.Write([]byte(providedPassword + fmt.Sprintf("%x", storedSalt)))
providedHashBytes := hash.Sum(nil)
providedHash := fmt.Sprintf("$SHA$%x$%x", storedSalt, providedHashBytes)
fmt.Println("Provided Hash:", providedHash)
fmt.Println("Stored Hash:", user.Password)
if storedHash != providedHash {
return errors.New("passwords do not match")
}
return nil
}
Provided Hash: $SHA$b0e416fde0e3ec2f$b8831aaf32aa3feb99fd4f4b01dbc8028efc3ea9874e960388893dfa7cfe4972
Stored Hash: $SHA$b0e416fde0e3ec2f$72ad6790c6d11ea9c765dfc701c41d9aef86489be5bf5a43d0414cea2fd5d614
now the start is good, but the end is still wrong.