Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kiss-crypto/noble #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
module.exports = {
'env': {
'browser': true,
'es2021': true,
'node': true,
env: {
browser: true,
es2021: true,
node: true,
},
'extends': [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
'parser': '@typescript-eslint/parser',
'parserOptions': {
'ecmaVersion': 12,
'sourceType': 'module',
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
'plugins': [
'@typescript-eslint',
],
'rules': {
'quotes': [
'error',
'single',
],
'semi': [
'error',
'never',
],
plugins: ['@typescript-eslint'],
rules: {
quotes: ['error', 'single'],
semi: ['error', 'never'],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-duplicate-enum-values': 'off',
},
}
40 changes: 32 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
{
"name": "kiss-crypto",
"version": "0.2.1",
"version": "0.3.0-beta0",
"description": "",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./noble": {
"types": "./dist/noble/index.d.ts",
"default": "./dist/noble/index.js"
}
},
"typesVersions": {
"*": {
".": [
"./dist/index.d.ts"
],
"noble": [
"./dist/noble/index.d.ts"
]
}
},
"scripts": {
"test": "TZ=UTC vitest",
"build": "tsc --build tsconfig.build.json",
Expand All @@ -24,18 +44,22 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@babel/preset-env": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@babel/preset-env": "^7.22.14",
"@babel/preset-typescript": "^7.22.11",
"@size-limit/preset-small-lib": "^8.2.6",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"eslint": "^8.43.0",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"eslint": "^8.48.0",
"eslint-config-google": "^0.14.0",
"size-limit": "^8.2.6",
"typescript": "^5.1.6",
"vitest": "^0.32.2"
"typescript": "^5.2.2",
"vitest": "^0.34.3"
},
"dependencies": {
"@noble/ciphers": "0.3.0",
"@noble/hashes": "1.3.2",
"@scure/base": "1.1.2",
"hash-wasm": "4.9.0",
"@types/libsodium-wrappers-sumo": "^0.7.5",
"fast-sha256": "^1.3.0",
"get-random-values": "^2.1.0",
Expand Down
139 changes: 139 additions & 0 deletions src/noble/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {utf8ToBytes} from '@noble/hashes/utils'
import {
decrypt,
decryptBlob,
encrypt,
encryptBlob,
generateEncryptionKey,
generateSalt,
hash,
hashPassword,
decryptBlobAsString,
encryptStringAsBlob,
} from '.'

import {arrayBufferToString} from './utils'

it('encrypts/decrypts plaintext', function () {
const key = generateEncryptionKey()

const plaintext = 'hello world'

const ciphertext = encrypt({
plaintext,
key,
})

const decrypted = decrypt({
ciphertext,
key,
})

expect(decrypted).toEqual(plaintext)
})

it('encrypts/decrypts blobs', function () {
const key = generateEncryptionKey()

const plaintext = 'hello world'

const plainblob = utf8ToBytes(plaintext)

const cipherblob = encryptBlob({
plainblob,
key,
})

const decrypted = decryptBlob({
cipherblob,
key,
})

expect(arrayBufferToString(decrypted!)).toEqual(plaintext)
})

it('hashes a password', async function () {
const password = 'password1'
const salt = generateSalt()
const hash1 = await hashPassword({password, salt})

expect(hash1.length).toEqual(64)

const hash2 = await hashPassword({password, salt})
expect(hash1).toEqual(hash2)
})

it('sanity checks password hashes', async function () {
const password = 'password1'
const salt = 'cfebdb6da2d9167786c83cce87963692'
const hash1 = await hashPassword({password, salt})

expect(hash1).toMatchInlineSnapshot(
'"29057df69d9940bab07be1afb4c9f1867addff3f092591f23b50152b63bcdf86"',
)
})

it('hashes a key', function () {
const key = 'key1'
const salt = generateSalt()
const hash1 = hash({key, salt})

const hash2 = hash({key, salt})
expect(hash1).toEqual(hash2)
})

it('sanity checks hashes', function () {
const key = 'key1'
const salt = '04450d2470c9d3e63259da24f4cddb7e'
const hash1 = hash({key, salt})

expect(hash1).toMatchInlineSnapshot(
'"c61ba75858ee4507e940d18a00d05d655919e1d71b6166e5e86c405828cda2de"',
)
})

it('encrypts with hashed password', async function () {
const password = 'password1'
const salt = generateSalt()
const key = await hashPassword({password, salt})

const plaintext = 'hello world'

const ciphertext = encrypt({
plaintext,
key,
})

const decrypted = decrypt({
ciphertext,
key,
})

expect(decrypted).toEqual(plaintext)
})

it('sanity checks v001 ciphertext', function () {
const plaintext = 'hello world'
const ciphertext =
'001:a1caba8a0ff627c4f85d1134a8b69b7e4ede04c0a11c0fe6:6DIAXgI6sFceb8UZWDY7HYcRQ44opn3LTmQo'
const key = '513fe9f410236db712a710ae8ab13bd94178fe645f3525756ebf92232a7906cf'

const decrypted = decrypt({
ciphertext,
key,
})

expect(decrypted).toEqual(plaintext)
})

it('encrypts/decrypts blobs as strings', async function () {
const key = await generateEncryptionKey()

const plaintext = 'hello world'

const cipherblob = await encryptStringAsBlob({plaintext, key})

const decrypted = await decryptBlobAsString({cipherblob, key})

expect(decrypted).toEqual(plaintext)
})
Loading
Loading