Skip to content

Commit

Permalink
Merge pull request #38 from planetarium/replace-keccak-256
Browse files Browse the repository at this point in the history
fix: replace keccak256 with @noble/hashes
  • Loading branch information
moreal authored May 16, 2024
2 parents f88f9d5 + 57484cd commit dc0453b
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "jest --coverage --detectOpenHandles"
},
"dependencies": {
"@noble/hashes": "^1.4.0",
"@planetarium/account": "~4.4.2",
"@planetarium/bencodex": "0.2.2",
"@planetarium/tx": "^4.4.2",
Expand All @@ -19,7 +20,6 @@
"core-js": "^3.6.5",
"decimal.js": "^10.4.3",
"ethers": "^5.5.1",
"keccak256": "^1.0.3",
"nanoid": "^5.0.7",
"web3": "^1.6.0"
},
Expand Down
6 changes: 3 additions & 3 deletions background/src/utils/aes256.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// const crypto = require('crypto')
import keccak256 from "keccak256"
import { keccak_256 } from "@noble/hashes/sha3"
const IV_LENGTH = 16
export default {
encrypt: async (text, passphrase) => {
const key = await window.crypto.subtle.importKey("raw", Buffer.from(keccak256(passphrase)), {name: 'AES-CBC'}, true, ['encrypt', 'decrypt']);
const key = await window.crypto.subtle.importKey("raw", Buffer.from(keccak_256(passphrase)), {name: 'AES-CBC'}, true, ['encrypt', 'decrypt']);
const iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH));
const encrypted = await window.crypto.subtle.encrypt({
name: 'AES-CBC',
Expand All @@ -21,7 +21,7 @@ export default {
const textParts = text.split(':')
const iv = Buffer.from(textParts.shift(), 'hex')
const encryptedText = Buffer.from(textParts.join(':'), 'hex')
const key = await window.crypto.subtle.importKey("raw", Buffer.from(keccak256(passphrase)), {name: 'AES-CBC'}, true, ['encrypt', 'decrypt']);
const key = await window.crypto.subtle.importKey("raw", Buffer.from(keccak_256(passphrase)), {name: 'AES-CBC'}, true, ['encrypt', 'decrypt']);
const decrypted = await window.crypto.subtle.decrypt({
name: 'AES-CBC',
iv,
Expand Down
21 changes: 6 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion popup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@mdi/font": "^5.9.55",
"@noble/hashes": "^1.4.0",
"@vue/vue3-jest": "27",
"axios": "^0.21.4",
"bencodex": "^0.1.2",
Expand All @@ -18,7 +19,6 @@
"countup.js": "^2.8.0",
"eccrypto": "^1.1.6",
"ethers": "^6.12.1",
"keccak256": "^1.0.6",
"moment": "^2.30.1",
"underscore": "^1.13.6",
"vue": "3",
Expand Down
2 changes: 1 addition & 1 deletion popup/src/api/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const callBackground = function(action, method, params = []) {
if (res.error == 'NotSignedIn') {
location.reload()
} else {
console.log('error callBackground', res)
console.log('error callBackground', action, method, params, res)
reject(res.error)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions popup/src/components/AccountManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import AccountSelector from "@/components/buttons/AccountSelector.vue";
import CopyBtn from "@/components/buttons/CopyBtn.vue";
import rule from "@/utils/rules"
import keccak256 from "keccak256";
import { keccak_256 } from "@noble/hashes/sha3";
import t from "@/utils/i18n"
import utils from "@/utils/utils";
import { Wallet } from "ethers";
Expand Down Expand Up @@ -261,7 +261,7 @@ export default {
if (this.pkview.dialog && this.pkview.password) {
try {
this.pkview.loading = true
let passphrase = keccak256(this.pkview.password).toString('hex')
let passphrase = Buffer.from(keccak_256(this.pkview.password)).toString('hex')
let pk = await this.$store.dispatch('Account/getPrivateKey', {
address: this.account.address,
passphrase: passphrase
Expand Down
6 changes: 3 additions & 3 deletions popup/src/utils/aes256.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const crypto = require('crypto')
import keccak256 from "keccak256"
import { keccak_256 } from "@noble/hashes/sha3"
const IV_LENGTH = 16
export default {
encrypt: (text, passphrase) => {
const iv = crypto.randomBytes(IV_LENGTH)
const cipher = crypto.createCipheriv(
'aes-256-cbc',
Buffer.from(keccak256(passphrase)),
Buffer.from(keccak_256(passphrase)),
iv,
)
const encrypted = cipher.update(text)
Expand All @@ -23,7 +23,7 @@ export default {
const encryptedText = Buffer.from(textParts.join(':'), 'hex')
const decipher = crypto.createDecipheriv(
'aes-256-cbc',
Buffer.from(keccak256(passphrase)),
Buffer.from(keccak_256(passphrase)),
iv,
)
const decrypted = decipher.update(encryptedText)
Expand Down
4 changes: 2 additions & 2 deletions popup/src/views/Initiate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<script>
import rules from "@/utils/rules"
import t from "@/utils/i18n"
import keccak256 from "keccak256"
import { keccak_256 } from "@noble/hashes/sha3"
import _ from "underscore"
import InitiateHeader from "@/components/InitiateHeader.vue";
Expand Down Expand Up @@ -98,7 +98,7 @@ export default {
t,
async initiate() {
if ((await this.$refs['form'].validate()).valid && this.isValidPassword) {
let passphrase = keccak256(this.pass1).toString('hex')
let passphrase = Buffer.from(keccak_256(this.pass1)).toString('hex')
this.$router.replace({
name: 'initiateMnemonic',
params: {
Expand Down
4 changes: 2 additions & 2 deletions popup/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<script>
import { mapGetters } from "vuex";
import keccak256 from "keccak256"
import { keccak_256 } from "@noble/hashes/sha3"
import InitiateHeader from "@/components/InitiateHeader.vue";
import t from "@/utils/i18n";
Expand Down Expand Up @@ -68,7 +68,7 @@ export default {
this.loginError = null
try {
if (this.password) {
let passphrase = keccak256(this.password).toString('hex')
let passphrase = Buffer.from(keccak_256(this.password)).toString('hex')
if (await this.$store.dispatch('Account/isValidPassphrase', passphrase)) {
await this.$store.dispatch('Account/setPassphrase', passphrase)
await this.$store.dispatch('Account/loadAccounts')
Expand Down

0 comments on commit dc0453b

Please sign in to comment.