#Bcrypt hash code to SHA256

12 messages · Page 1 of 1 (latest)

heady badge
#

I tried to change it, but the hashed password is always differend from the database, and i dont know why.

I always get something like: ||4763a5720219b4dbad5cdbdd4efb548433983f2f50f9dbeb21b46a3f1196d3cc||
And i have to get like this: ||$SHA$e1be2894058bc459$73864766fc62cf681865e6823eb21a9fe0de6f99b5493745c046e6a9928a282f||

steady raven
#

That's too much code. I can't easily find where you're ashing using SHA256

heady badge
#

oh i sent the wrong code

steady raven
#

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.

steady raven
#

Why do you want that format in particular?

heady badge
heady badge
#

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.