Skip to content

Commit

Permalink
feat: replace sha256 with blake2b
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Apr 12, 2023
1 parent 25fbf49 commit 0e137ba
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 28 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@stitches/react": "^1.2.8",
"bech32": "^2.0.0",
"blake2b": "^2.1.4",
"buffer": "^6.0.3",
"lodash.chunk": "^4.2.0",
"loglevel": "^1.8.0",
Expand All @@ -58,6 +59,7 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/blake2b": "^2.1.0",
"@types/chai": "^4.3.3",
"@types/chrome": "^0.0.224",
"@types/jest": "^29.1.2",
Expand Down
10 changes: 5 additions & 5 deletions src/connector/helpers/data-chunking.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { config } from 'config'
import { sha256 } from 'crypto/sha256'
import { err, ok, Result } from 'neverthrow'
import { bufferToChunks } from 'utils'
import { Buffer } from 'buffer'
import { blake2b } from 'crypto/blake2b'

export type MetaData = {
packageType: 'metaData'
Expand Down Expand Up @@ -57,7 +57,7 @@ export const messageToChunked = (
)
)
.asyncAndThen((chunks) =>
sha256(message)
blake2b(message)
.map(
(buffer): MetaData => ({
packageType: 'metaData',
Expand Down Expand Up @@ -104,9 +104,9 @@ export const Chunked = (metaData: MetaData) => {

const validate = () =>
concatChunks()
.asyncAndThen(sha256)
.andThen((sha256Hash) => {
const expectedHash = sha256Hash.toString('hex')
.asyncAndThen((value) => blake2b(Buffer.from(value, 'utf-8')))
.andThen((hash) => {
const expectedHash = hash.toString('hex')
return expectedHash === metaData.hashOfMessage
? ok(undefined)
: err(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sha256 } from 'crypto/sha256'
import { blake2b } from 'crypto/blake2b'

export const deriveSecretsFromPassword = (password: Buffer) =>
sha256(password).map((connectionId) => ({
blake2b(password).map((connectionId) => ({
connectionId,
encryptionKey: password,
}))
14 changes: 14 additions & 0 deletions src/crypto/blake2b.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { blake2b } from './blake2b'
import { Buffer } from 'buffer'

describe('blake2b', () => {
it('should hash a string', async () => {
const result = await blake2b(Buffer.from('test'))

if (result.isErr()) throw result.error

expect(result.value.toString('hex')).toBe(
'928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202'
)
})
})
14 changes: 14 additions & 0 deletions src/crypto/blake2b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Buffer } from 'buffer'
import blake2bHash from 'blake2b'
import { errAsync, okAsync, ResultAsync } from 'neverthrow'

export const blake2b = (input: Buffer): ResultAsync<Buffer, Error> => {
const output = new Uint8Array(32)
try {
return okAsync(
blake2bHash(output.length).update(new Uint8Array(input)).digest('hex')
).map((hex) => Buffer.from(hex, 'hex'))
} catch (error) {
return errAsync(error as Error)
}
}
11 changes: 0 additions & 11 deletions src/crypto/sha256.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/crypto/sha256.ts

This file was deleted.

31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/blake2b@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/blake2b/-/blake2b-2.1.0.tgz#d906320255d84f9c05fc6f3a28db81c952ee989b"
integrity sha512-Mv8N0qtdb72iW1XJR/4Yp01frtibnv8BpFUZDROnzd7hVEX4cEN/xHgU2ZfsnOnTDt+fHJVc0uRIWcNz6uIB2A==

"@types/chai@^4.3.3":
version "4.3.3"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.3.tgz#3c90752792660c4b562ad73b3fbd68bf3bc7ae07"
Expand Down Expand Up @@ -3253,6 +3258,11 @@ axe-core@^4.4.3:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.3.tgz#11c74d23d5013c0fa5d183796729bc3482bd2f6f"
integrity sha512-32+ub6kkdhhWick/UjvEwRchgoetXqTK14INLqbGm5U2TzBkBNF3nQtLYm8ovxSkQWArjEQvftCKryjZaATu3w==

b4a@^1.0.1:
version "1.6.3"
resolved "https://registry.yarnpkg.com/b4a/-/b4a-1.6.3.tgz#cce6e8a2f0d0774e5fe8062086827e979970266d"
integrity sha512-aX6/FqpWQve8VN9kyTExy7GlmwNShvxcCWWD5QVR3ZbRlyBGtCrG5Autu95xxSPH4CRs+5PSV4d7PRnWpmqFlA==

babel-jest@^29.1.2:
version "29.1.2"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.1.2.tgz#540d3241925c55240fb0c742e3ffc5f33a501978"
Expand Down Expand Up @@ -3413,6 +3423,22 @@ binary-extensions@^2.0.0, binary-extensions@^2.2.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

blake2b-wasm@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/blake2b-wasm/-/blake2b-wasm-2.4.0.tgz#9115649111edbbd87eb24ce7c04b427e4e2be5be"
integrity sha512-S1kwmW2ZhZFFFOghcx73+ZajEfKBqhP82JMssxtLVMxlaPea1p9uoLiUZ5WYyHn0KddwbLc+0vh4wR0KBNoT5w==
dependencies:
b4a "^1.0.1"
nanoassert "^2.0.0"

blake2b@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/blake2b/-/blake2b-2.1.4.tgz#817d278526ddb4cd673bfb1af16d1ad61e393ba3"
integrity sha512-AyBuuJNI64gIvwx13qiICz6H6hpmjvYS5DGkG6jbXMOT8Z3WUJ3V1X0FlhIoT1b/5JtHE3ki+xjtMvu1nn+t9A==
dependencies:
blake2b-wasm "^2.4.0"
nanoassert "^2.0.0"

body-parser@1.20.0:
version "1.20.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.20.0.tgz#3de69bd89011c11573d7bfee6a64f11b6bd27cc5"
Expand Down Expand Up @@ -7456,6 +7482,11 @@ nano-css@^5.3.1:
stacktrace-js "^2.0.2"
stylis "^4.0.6"

nanoassert@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/nanoassert/-/nanoassert-2.0.0.tgz#a05f86de6c7a51618038a620f88878ed1e490c09"
integrity sha512-7vO7n28+aYO4J+8w96AzhmU8G+Y/xpPDJz/se19ICsqj/momRbb9mh9ZUtkoJ5X3nTnPdhEJyc0qnM6yAsHBaA==

nanoid@^3.3.4:
version "3.3.4"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
Expand Down

0 comments on commit 0e137ba

Please sign in to comment.