Skip to content

Commit

Permalink
feat: add salt hash to blake2b
Browse files Browse the repository at this point in the history
  • Loading branch information
namle133 committed Nov 13, 2023
1 parent 3a790d0 commit aadf46d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 17 additions & 4 deletions cmd/chainbridge/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ func importPrivKey(ctx *cli.Context, keytype, datadir, key string, password []by
}

var kp crypto.Keypair
hshPwd := hash.HashPasswordIteratively(string(password))
hshPwd, salt, err := hash.HashPasswordIteratively(string(password))
if err != nil {
return "", err
}

if keytype == crypto.Sr25519Type {
// generate sr25519 keys
Expand Down Expand Up @@ -237,7 +240,7 @@ func importPrivKey(ctx *cli.Context, keytype, datadir, key string, password []by
}
defer keyBuf.Destroy()

err = keystore.EncryptAndWriteToFile(file, kp, keyBuf.Bytes())
err = keystore.EncryptAndWriteToFile(file, kp, keyBuf.Bytes(), salt)
if err != nil {
return "", fmt.Errorf("could not write key to file: %w", err)
}
Expand Down Expand Up @@ -291,7 +294,12 @@ func importEthKey(filename, datadir string, password, newPassword []byte) (strin
newPassword = keystore.GetPassword("Enter password to encrypt new keystore file:")
}

err = keystore.EncryptAndWriteToFile(file, kp, newPassword)
hshPwd, salt, err := hash.HashPasswordIteratively(string(newPassword))
if err != nil {
return "", err
}

err = keystore.EncryptAndWriteToFile(file, kp, hshPwd, salt)
if err != nil {
return "", fmt.Errorf("could not write key to file: %w", err)
}
Expand Down Expand Up @@ -390,6 +398,11 @@ func generateKeypair(keytype, datadir string, password []byte, subNetwork string
var kp crypto.Keypair
var err error

hshPwd, salt, err := hash.HashPasswordIteratively(string(password))
if err != nil {
return "", err
}

if keytype == crypto.Sr25519Type {
// generate sr25519 keys
kp, err = sr25519.GenerateKeypair(subNetwork)
Expand Down Expand Up @@ -428,7 +441,7 @@ func generateKeypair(keytype, datadir string, password []byte, subNetwork string
}
}()

err = keystore.EncryptAndWriteToFile(file, kp, password)
err = keystore.EncryptAndWriteToFile(file, kp, hshPwd, salt)
if err != nil {
return "", fmt.Errorf("could not write key to file: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532
github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e/go.mod h1:H5fNH57wn/j1oLifOnWEqYbfJZcOWzr7jZjKKrUckSQ=
github.com/ChainSafe/log15 v1.0.0 h1:vRDVtWtVwIH5uSCBvgTTZh6FA58UBJ6+QiiypaZfBf8=
github.com/ChainSafe/log15 v1.0.0/go.mod h1:5v1+ALHtdW0NfAeeoYyKmzCAMcAeqkdhIg4uxXWIgOg=
github.com/ChainVerse-Team/chainbridge-utils v0.0.2-beta h1:/silo4Dc3koOGgKKQ1gug5FQs/ljAy/o/kc/HrbhK+I=
github.com/ChainVerse-Team/chainbridge-utils v0.0.2-beta/go.mod h1:xTogecjpOKlZLDF5xpIq8oFmcW8Oq412nxwCWpnuGOI=
github.com/ChainVerse-Team/chainbridge-utils v0.0.3-alpha h1:dy8nw4WlF/nPIn6V69I8H4JXtL1kdExF7tnDu2S3gjg=
github.com/ChainVerse-Team/chainbridge-utils v0.0.3-alpha/go.mod h1:xTogecjpOKlZLDF5xpIq8oFmcW8Oq412nxwCWpnuGOI=
github.com/ChainVerse-Team/memguard v0.0.1-beta h1:LtBBW7nLTtuFPLITWdGm1bbE57V3SOPD0Pq4JX2kGx0=
github.com/ChainVerse-Team/memguard v0.0.1-beta/go.mod h1:kD0Rh6/N7KMOhHOFwCTbzNvVlf/0tInVm7FJueXK4Wg=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
Expand Down Expand Up @@ -596,7 +596,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down

0 comments on commit aadf46d

Please sign in to comment.