Skip to content

Commit

Permalink
Update ESLint rules (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue authored Dec 14, 2023
1 parent 5b4b5e9 commit ff56a47
Show file tree
Hide file tree
Showing 16 changed files with 1,708 additions and 1,190 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 16
node-version: 20.x
- uses: actions/cache@v2
id: yarn-cache
with:
Expand Down
5 changes: 1 addition & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@
"printWidth": 90,
"semi": false,
"trailingComma": "all",
"singleQuote": true,
"bracketSpacing": false,
"jsxBracketSameLine": false,
"arrowParens": "always"
"singleQuote": true
}
26 changes: 26 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"plugins": [
"prettier-plugin-svelte",
"prettier-plugin-astro",
"prettier-plugin-tailwindcss"
],
"semi": false,
"singleQuote": true,
"htmlWhitespaceSensitivity": "ignore",
"overrides": [
{
"files": "*.svelte",
"options": {
"parser": "svelte"
}
},
{
"files": "*.astro",
"options": {
"parser": "astro"
}
}
],
"tailwindFunctions": ["clsx"],
"svelteIndentScriptAndStyle": false
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Easily encrypt and decrypt messages. All the complexity is hidden behind the sce
## Usage

```typescript
import {encrypt, decrypt, generateEncryptionKey} from 'kiss-crypto'
import { encrypt, decrypt, generateEncryptionKey } from 'kiss-crypto'

const key = await generateEncryptionKey()

Expand All @@ -29,7 +29,7 @@ And to turn passwords into encryption keys
```typescript
const password = 'password1'
const salt = await generateSalt()
const hash = await hashPassword({password, salt})
const hash = await hashPassword({ password, salt })

expect(hash.length).toEqual(512)
```
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { basic } from '@ocavue/eslint-config'

export default [
...basic(),
{
ignores: ['eslint.config.js', 'vite.config.ts'],
},
]
32 changes: 17 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "kiss-crypto",
"type": "module",
"version": "0.3.0-beta.0",
"description": "",
"type": "module",
"author": "",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -30,11 +29,15 @@
]
}
},
"files": [
"dist/**/*"
],
"scripts": {
"test": "TZ=UTC vitest",
"build": "mkdir -p dist && rm -rf dist && tsc --build tsconfig.build.json",
"prepack": "yarn build",
"lint": "eslint src/**/*.ts",
"fix": "eslint . --fix && prettier --write .",
"lint": "eslint . && tsc --noEmit",
"size": "size-limit",
"analyze": "size-limit --why"
},
Expand All @@ -44,24 +47,23 @@
"limit": "950 kB"
}
],
"author": "",
"license": "MIT",
"dependencies": {
"@noble/ciphers": "0.4.1",
"@noble/hashes": "1.3.3",
"@scure/base": "1.1.5",
"hash-wasm": "4.11.0"
},
"devDependencies": {
"@babel/preset-env": "^7.22.14",
"@babel/preset-typescript": "^7.22.11",
"@ocavue/eslint-config": "^1.4.0",
"@size-limit/preset-small-lib": "^8.2.6",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"eslint": "^8.48.0",
"eslint": "^8.55.0",
"eslint-config-google": "^0.14.0",
"prettier": "^3.1.0",
"size-limit": "^8.2.6",
"typescript": "^5.2.2",
"vitest": "^0.34.3"
},
"dependencies": {
"@noble/ciphers": "0.4.1",
"@noble/hashes": "1.3.3",
"@scure/base": "1.1.5",
"hash-wasm": "4.11.0"
}
}
18 changes: 8 additions & 10 deletions src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export enum Defaults {
Version = '001',
ArgonLength = 32,
ArgonSaltLength = 16,
ArgonIterations = 5,
ArgonMemLimit = 67108864,
ArgonOutputKeyBytes = 64,
EncryptionKeyLength = 32,
EncryptionNonceLength = 24,
}
export const Version = '001'
export const ArgonLength = 32
export const ArgonSaltLength = 16
export const ArgonIterations = 5
export const ArgonMemLimit = 67108864
export const ArgonOutputKeyBytes = 64
export const EncryptionKeyLength = 32
export const EncryptionNonceLength = 24
9 changes: 5 additions & 4 deletions src/hash-password.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {hexToBytes} from '@noble/hashes/utils'
import {argon2id} from 'hash-wasm'
import {Defaults} from './defaults.js'
import type {HexString, Utf8String} from './types.js'
import { hexToBytes } from '@noble/hashes/utils'
import { argon2id } from 'hash-wasm'

import * as Defaults from './defaults.js'
import type { HexString, Utf8String } from './types.js'

/**
* Derives a key from a password and salt using
Expand Down
31 changes: 16 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {utf8ToBytes} from '@noble/hashes/utils'
import { utf8ToBytes } from '@noble/hashes/utils'

import { hashPassword } from './hash-password.js'
import { arrayBufferToString } from './utils.js'

import {
decrypt,
decryptBlob,
Expand All @@ -10,9 +14,6 @@ import {
generateSalt,
hash,
} from './index.js'
import {hashPassword} from './hash-password.js'

import {arrayBufferToString} from './utils.js'

it('encrypts/decrypts plaintext', function () {
const key = generateEncryptionKey()
Expand Down Expand Up @@ -77,18 +78,18 @@ it.fails('decrypts Node.js Buffer', function () {
it('hashes a password', async function () {
const password = 'password1'
const salt = generateSalt()
const hash1 = await hashPassword({password, salt})
const hash1 = await hashPassword({ password, salt })

expect(hash1.length).toEqual(64)

const hash2 = await hashPassword({password, salt})
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})
const hash1 = await hashPassword({ password, salt })

expect(hash1).toMatchInlineSnapshot(
'"29057df69d9940bab07be1afb4c9f1867addff3f092591f23b50152b63bcdf86"',
Expand All @@ -98,16 +99,16 @@ it('sanity checks password hashes', async function () {
it('hashes a key', function () {
const key = 'key1'
const salt = generateSalt()
const hash1 = hash({key, salt})
const hash1 = hash({ key, salt })

const hash2 = 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})
const hash1 = hash({ key, salt })

expect(hash1).toMatchInlineSnapshot(
'"c61ba75858ee4507e940d18a00d05d655919e1d71b6166e5e86c405828cda2de"',
Expand All @@ -117,7 +118,7 @@ it('sanity checks hashes', function () {
it('encrypts with hashed password', async function () {
const password = 'password1'
const salt = generateSalt()
const key = await hashPassword({password, salt})
const key = await hashPassword({ password, salt })

const plaintext = 'hello world'

Expand Down Expand Up @@ -148,14 +149,14 @@ it('sanity checks v001 ciphertext', function () {
expect(decrypted).toEqual(plaintext)
})

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

const plaintext = 'hello world'

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

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

expect(decrypted).toEqual(plaintext)
})
41 changes: 25 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {xchacha20poly1305} from '@noble/ciphers/chacha'
import {hkdf} from '@noble/hashes/hkdf'
import {sha256} from '@noble/hashes/sha256'
import {bytesToHex, concatBytes, hexToBytes, utf8ToBytes} from '@noble/hashes/utils'
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
import { hkdf } from '@noble/hashes/hkdf'
import { sha256 } from '@noble/hashes/sha256'
import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils'

import * as Defaults from './defaults.js'
import type {
Base64String,
EncryptedBlobMessage,
Expand All @@ -15,9 +17,14 @@ import {
base64ToArrayBuffer,
generateRandomKey,
} from './utils.js'
import {Defaults} from './defaults.js'

export type {Base64String, EncryptedBlobMessage, EncryptedMessage, HexString, Utf8String}
export type {
Base64String,
EncryptedBlobMessage,
EncryptedMessage,
HexString,
Utf8String,
}

const STRING_PARTITION = ':'

Expand Down Expand Up @@ -70,16 +77,16 @@ export function encrypt({
* @param plaintext - The plaintext string to encrypt.
* @returns A Promise that resolves to the encrypted message as a Uint8Array.
*/
export const encryptStringAsBlob = ({
export function encryptStringAsBlob({
key,
plaintext,
}: {
key: HexString
plaintext: Utf8String
}): EncryptedBlobMessage => {
}): EncryptedBlobMessage {
const plainblob = utf8ToBytes(plaintext)

return encryptBlob({key, plainblob})
return encryptBlob({ key, plainblob })
}

/**
Expand Down Expand Up @@ -128,7 +135,7 @@ export function decrypt({
throw new Error(`Invalid version: ${version}`)
}

return xChaChaDecrypt({key, ciphertext, nonce})
return xChaChaDecrypt({ key, ciphertext, nonce })
}

/**
Expand All @@ -137,14 +144,14 @@ export function decrypt({
* @param cipherblob - The encrypted message as a Uint8Array.
* @returns A Promise that resolves to the decrypted plaintext string or null if decryption fails.
*/
export const decryptBlobAsString = ({
export function decryptBlobAsString({
key,
cipherblob,
}: {
key: HexString
cipherblob: EncryptedBlobMessage
}): Utf8String | null => {
const decryptedBlob = decryptBlob({key, cipherblob})
}): Utf8String | null {
const decryptedBlob = decryptBlob({ key, cipherblob })

if (!decryptedBlob) return null

Expand Down Expand Up @@ -180,7 +187,7 @@ export function decryptBlob({
Defaults.Version.length + Defaults.EncryptionNonceLength,
)

return xChaChaDecryptBlob({key, cipherblob, nonceBuffer})
return xChaChaDecryptBlob({ key, cipherblob, nonceBuffer })
}

/**
Expand Down Expand Up @@ -213,7 +220,9 @@ function xChaChaEncrypt({
plaintext: Utf8String
nonce: HexString
}): Base64String {
if (nonce.length !== 48) throw Error('Nonce must be 24 bytes')
if (nonce.length !== 48) {
throw new Error('Nonce must be 24 bytes')
}
const plainblob = new TextEncoder().encode(plaintext)
const arrayBuffer = xchacha20poly1305(hexToBytes(key), hexToBytes(nonce)).encrypt(
plainblob,
Expand All @@ -231,7 +240,7 @@ function xChaChaEncryptBlob({
nonceBuffer: Uint8Array
}): Uint8Array {
if (nonceBuffer.length !== 24) {
throw Error('nonceBuffer must be 24 bytes')
throw new Error('nonceBuffer must be 24 bytes')
}
return xchacha20poly1305(hexToBytes(key), nonceBuffer).encrypt(plainblob)
}
Expand Down
Loading

0 comments on commit ff56a47

Please sign in to comment.