Skip to content

Commit

Permalink
refactor: remove one usage of our bcrypt fork (#15154)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Feb 24, 2023
1 parent 4169053 commit 8e01d3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
7 changes: 3 additions & 4 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (

"github.com/99designs/keyring"
"github.com/cockroachdb/errors"
cmtcrypto "github.com/cometbft/cometbft/crypto"

"github.com/cosmos/go-bip39"

errorsmod "cosmossdk.io/errors"

"golang.org/x/crypto/bcrypt"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keys/bcrypt"
"github.com/cosmos/cosmos-sdk/crypto/ledger"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -751,8 +751,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
continue
}

saltBytes := cmtcrypto.CRandBytes(16)
passwordHash, err := bcrypt.GenerateFromPassword(saltBytes, []byte(pass), 2)
passwordHash, err := bcrypt.GenerateFromPassword([]byte(pass), 2)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
Expand Down
29 changes: 29 additions & 0 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"testing"

"github.com/99designs/keyring"
cmtcrypto "github.com/cometbft/cometbft/crypto"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cosmosbcrypt "github.com/cosmos/cosmos-sdk/crypto/keys/bcrypt"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -1949,6 +1952,32 @@ func TestRenameKey(t *testing.T) {
}
}

// TestChangeBcrypt tests the compatibility from upstream Bcrypt and our own
func TestChangeBcrypt(t *testing.T) {
pw := []byte("somepasswword!")

saltBytes := cmtcrypto.CRandBytes(16)
cosmosHash, err := cosmosbcrypt.GenerateFromPassword(saltBytes, pw, 2)
require.NoError(t, err)

bcryptHash, err := bcrypt.GenerateFromPassword(pw, 2)
require.NoError(t, err)

// Check the new hash with the old bcrypt, vice-versa and with the same
// bcrypt version just because.
err = cosmosbcrypt.CompareHashAndPassword(bcryptHash, pw)
require.NoError(t, err)

err = cosmosbcrypt.CompareHashAndPassword(cosmosHash, pw)
require.NoError(t, err)

err = bcrypt.CompareHashAndPassword(cosmosHash, pw)
require.NoError(t, err)

err = bcrypt.CompareHashAndPassword(bcryptHash, pw)
require.NoError(t, err)
}

func requireEqualRenamedKey(t *testing.T, key *Record, mnemonic *Record, nameMatch bool) {
if nameMatch {
require.Equal(t, key.Name, mnemonic.Name)
Expand Down

0 comments on commit 8e01d3f

Please sign in to comment.