Skip to content

Commit

Permalink
Hashing secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault committed Mar 25, 2021
1 parent 9a23cbc commit aa0a411
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 7 additions & 1 deletion x/beam/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ func CmdOpenBeam() *cobra.Command {
// Generate the random id
id := types.GenerateSecureToken(10)

// Encode the secret
hashedSecret, err := types.GenerateHashFromString(argsSecret)
if err != nil {
return err
}

// Construct the message and validate
msg := types.NewMsgOpenBeam(id, clientCtx.GetFromAddress().String(), int64(argsAmount), argsSecret)
msg := types.NewMsgOpenBeam(id, clientCtx.GetFromAddress().String(), int64(argsAmount), hashedSecret)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions x/beam/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ func (k Keeper) ClaimBeam(ctx sdk.Context, msg types.MsgClaimBeam) error {
beam := k.GetBeam(ctx, msg.Id)

// Make sure transaction signer is authorized
//TODO: find a cryptographic way for this
if beam.Secret != msg.Secret {
if types.CompareHashAndString(beam.Secret, msg.Secret) == false {
return types.ErrBeamNotAuthorized
}

Expand Down
17 changes: 17 additions & 0 deletions x/beam/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/hex"
"golang.org/x/crypto/bcrypt"
"math/rand"
)

Expand All @@ -13,3 +14,19 @@ func GenerateSecureToken(length int) string {
}
return hex.EncodeToString(b)
}

// GenerateHashFromString is used to generate a hashed version of the argument passed string
func GenerateHashFromString(secret string) (string, error) {
hashed, err := bcrypt.GenerateFromPassword([]byte(secret), bcrypt.DefaultCost)
if err != nil {
return "", err
}

return string(hashed), nil
}

// CompareHashAndString is used to verify that a string matches a provided hash
func CompareHashAndString(hash string, str string) bool {
res := bcrypt.CompareHashAndPassword([]byte(hash), []byte(str))
return res == nil
}

0 comments on commit aa0a411

Please sign in to comment.