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

refactor: convert to native esm #261

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
154 changes: 8 additions & 146 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,150 +1,12 @@
{
'env': { // https://eslint.org/docs/user-guide/configuring.html#specifying-environments
'node': true,
'browser': true,
'shared-node-browser': true
"env": {
"browser": true,
"es2022": true,
"node": true
},
'globals': {
'Uint8Array': true,
'Uint16Array': true,
'Uint32Array': true,
'Int32Array': true,
'Float64Array': true
},
'rules': {
/**
* Strict mode
*/
'strict': 0, // https://eslint.org/docs/rules/strict

/**
* Variables
*/
'no-shadow': 2, // https://eslint.org/docs/rules/no-shadow
'no-shadow-restricted-names': 2, // https://eslint.org/docs/rules/no-shadow-restricted-names
'no-undef': 2, // https://eslint.org/docs/rules/no-undef
'no-unused-vars': [2, { // https://eslint.org/docs/rules/no-unused-vars
'vars': 'local',
'args': 'after-used'
}],
'no-use-before-define': 2, // https://eslint.org/docs/rules/no-use-before-define

/**
* Possible errors
*/
'comma-dangle': [2, 'never'], // https://eslint.org/docs/rules/comma-dangle
'no-cond-assign': 0, // https://eslint.org/docs/rules/no-cond-assign
'no-console': 0, // https://eslint.org/docs/rules/no-console
'no-debugger': 1, // https://eslint.org/docs/rules/no-debugger
'no-alert': 1, // https://eslint.org/docs/rules/no-alert
'no-constant-condition': 1, // https://eslint.org/docs/rules/no-constant-condition
'no-dupe-keys': 2, // https://eslint.org/docs/rules/no-dupe-keys
'no-duplicate-case': 2, // https://eslint.org/docs/rules/no-duplicate-case
'no-empty': 2, // https://eslint.org/docs/rules/no-empty
'no-ex-assign': 2, // https://eslint.org/docs/rules/no-ex-assign
'no-extra-boolean-cast': 0, // https://eslint.org/docs/rules/no-extra-boolean-cast
'no-extra-semi': 2, // https://eslint.org/docs/rules/no-extra-semi
'no-func-assign': 2, // https://eslint.org/docs/rules/no-func-assign
'no-inner-declarations': 2, // https://eslint.org/docs/rules/no-inner-declarations
'no-invalid-regexp': 2, // https://eslint.org/docs/rules/no-invalid-regexp
'no-irregular-whitespace': 2, // https://eslint.org/docs/rules/no-irregular-whitespace
'no-obj-calls': 2, // https://eslint.org/docs/rules/no-obj-calls
'no-sparse-arrays': 2, // https://eslint.org/docs/rules/no-sparse-arrays
'no-unreachable': 2, // https://eslint.org/docs/rules/no-unreachable
'use-isnan': 2, // https://eslint.org/docs/rules/use-isnan
'block-scoped-var': 2, // https://eslint.org/docs/rules/block-scoped-var

/**
* Best practices
*/
'consistent-return': 2, // https://eslint.org/docs/rules/consistent-return
'curly': 0, // https://eslint.org/docs/rules/curly
'default-case': 2, // https://eslint.org/docs/rules/default-case
'dot-notation': [2, { // https://eslint.org/docs/rules/dot-notation
'allowKeywords': true
}],
'eqeqeq': 2, // https://eslint.org/docs/rules/eqeqeq
'guard-for-in': 2, // https://eslint.org/docs/rules/guard-for-in
'no-caller': 2, // https://eslint.org/docs/rules/no-caller
'no-else-return': 0, // https://eslint.org/docs/rules/no-else-return
'no-eq-null': 2, // https://eslint.org/docs/rules/no-eq-null
'no-eval': 2, // https://eslint.org/docs/rules/no-eval
'no-extend-native': 2, // https://eslint.org/docs/rules/no-extend-native
'no-extra-bind': 2, // https://eslint.org/docs/rules/no-extra-bind
'no-fallthrough': 2, // https://eslint.org/docs/rules/no-fallthrough
'no-floating-decimal': 2, // https://eslint.org/docs/rules/no-floating-decimal
'no-implied-eval': 2, // https://eslint.org/docs/rules/no-implied-eval
'no-lone-blocks': 2, // https://eslint.org/docs/rules/no-lone-blocks
'no-loop-func': 2, // https://eslint.org/docs/rules/no-loop-func
'no-multi-str': 2, // https://eslint.org/docs/rules/no-multi-str
'no-native-reassign': 2, // https://eslint.org/docs/rules/no-native-reassign
'no-new': 2, // https://eslint.org/docs/rules/no-new
'no-new-func': 2, // https://eslint.org/docs/rules/no-new-func
'no-new-wrappers': 2, // https://eslint.org/docs/rules/no-new-wrappers
'no-octal': 2, // https://eslint.org/docs/rules/no-octal
'no-octal-escape': 2, // https://eslint.org/docs/rules/no-octal-escape
'no-param-reassign': 0, // https://eslint.org/docs/rules/no-param-reassign
'no-proto': 2, // https://eslint.org/docs/rules/no-proto
'no-redeclare': 2, // https://eslint.org/docs/rules/no-redeclare
'no-return-assign': 2, // https://eslint.org/docs/rules/no-return-assign
'no-script-url': 2, // https://eslint.org/docs/rules/no-script-url
'no-self-compare': 2, // https://eslint.org/docs/rules/no-self-compare
'no-sequences': 2, // https://eslint.org/docs/rules/no-sequences
'no-throw-literal': 2, // https://eslint.org/docs/rules/no-throw-literal
'no-with': 2, // https://eslint.org/docs/rules/no-with
'radix': 2, // https://eslint.org/docs/rules/radix
'vars-on-top': 0, // https://eslint.org/docs/rules/vars-on-top
'wrap-iife': [2, 'any'], // https://eslint.org/docs/rules/wrap-iife
'yoda': 2, // https://eslint.org/docs/rules/yoda

/**
* Style
*/
'indent': 0, // https://eslint.org/docs/rules/indent
'brace-style': [
2, // https://eslint.org/docs/rules/brace-style
'1tbs', {
'allowSingleLine': true
}
],
'quotes': [
2, 'single', 'avoid-escape' // https://eslint.org/docs/rules/quotes
],
'camelcase': 0,
'comma-spacing': 0,
'comma-style': [2, 'last'], // https://eslint.org/docs/rules/comma-style
'eol-last': 2, // https://eslint.org/docs/rules/eol-last
'func-names': 0, // https://eslint.org/docs/rules/func-names
'key-spacing': [2, { // https://eslint.org/docs/rules/key-spacing
'beforeColon': false,
'afterColon': true
}],
'new-cap': [0, { // https://eslint.org/docs/rules/new-cap
'newIsCap': true
}],
'no-multiple-empty-lines': [2, { // https://eslint.org/docs/rules/no-multiple-empty-lines
'max': 2
}],
'no-nested-ternary': 2, // https://eslint.org/docs/rules/no-nested-ternary
'no-new-object': 2, // https://eslint.org/docs/rules/no-new-object
'no-spaced-func': 2, // https://eslint.org/docs/rules/no-spaced-func
'no-trailing-spaces': 2, // https://eslint.org/docs/rules/no-trailing-spaces
'no-extra-parens': [2, 'functions'], // https://eslint.org/docs/rules/no-extra-parens
'no-underscore-dangle': 0, // https://eslint.org/docs/rules/no-underscore-dangle
'one-var': 0, // https://eslint.org/docs/rules/one-var
'padded-blocks': 0, // https://eslint.org/docs/rules/padded-blocks
'semi': [2, 'always'], // https://eslint.org/docs/rules/semi
'semi-spacing': [2, { // https://eslint.org/docs/rules/semi-spacing
'before': false,
'after': true
}],
'space-after-keywords': 0, // https://eslint.org/docs/rules/space-after-keywords
'space-before-blocks': 2, // https://eslint.org/docs/rules/space-before-blocks
'space-before-function-paren': [2, 'never'], // https://eslint.org/docs/rules/space-before-function-paren
'space-infix-ops': 0, // https://eslint.org/docs/rules/space-infix-ops
'spaced-comment': [2, 'always', {// https://eslint.org/docs/rules/spaced-comment
'exceptions': ['-', '+'],
'markers': ['=', '!', 'global', 'TODO', 'XXX', 'BUG'] // space here to support sprockets directives
}],
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2022
}
}
56 changes: 56 additions & 0 deletions lib/box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {checkArrayTypes} from './utils.js';
import {
crypto_box_BEFORENMBYTES,
crypto_box_NONCEBYTES,
crypto_box_PUBLICKEYBYTES,
crypto_box_SECRETKEYBYTES
} from './constants.js';
import {crypto_box_beforenm, crypto_box_keypair, crypto_scalarmult_base} from './crypto.js';
import {secretbox} from './secretbox.js';
function checkBoxLengths(pk, sk) {
if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error('bad public key size');
if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');
}
export function box(msg, nonce, publicKey, secretKey) {
const k = box.before(publicKey, secretKey);
return secretbox(msg, nonce, k);
}

box.before = function(publicKey, secretKey) {
checkArrayTypes(publicKey, secretKey);
checkBoxLengths(publicKey, secretKey);
const k = new Uint8Array(crypto_box_BEFORENMBYTES);
crypto_box_beforenm(k, publicKey, secretKey);
return k;
};

box.after = secretbox;

box.open = function(msg, nonce, publicKey, secretKey) {
const k = box.before(publicKey, secretKey);
return secretbox.open(msg, nonce, k);
};

box.open.after = secretbox.open;

box.keyPair = function() {
const pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
const sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
crypto_box_keypair(pk, sk);
return {publicKey: pk, secretKey: sk};
};

box.keyPair.fromSecretKey = function(secretKey) {
checkArrayTypes(secretKey);
if (secretKey.length !== crypto_box_SECRETKEYBYTES)
throw new Error('bad secret key size');
const pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
crypto_scalarmult_base(pk, secretKey);
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
};

box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
box.secretKeyLength = crypto_box_SECRETKEYBYTES;
box.sharedKeyLength = crypto_box_BEFORENMBYTES;
box.nonceLength = crypto_box_NONCEBYTES;
box.overheadLength = secretbox.overheadLength;
17 changes: 17 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const crypto_secretbox_KEYBYTES = 32,
crypto_secretbox_NONCEBYTES = 24,
crypto_secretbox_ZEROBYTES = 32,
crypto_secretbox_BOXZEROBYTES = 16,
crypto_scalarmult_BYTES = 32,
crypto_scalarmult_SCALARBYTES = 32,
crypto_box_PUBLICKEYBYTES = 32,
crypto_box_SECRETKEYBYTES = 32,
crypto_box_BEFORENMBYTES = 32,
crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
crypto_sign_BYTES = 64,
crypto_sign_PUBLICKEYBYTES = 32,
crypto_sign_SECRETKEYBYTES = 64,
crypto_sign_SEEDBYTES = 32,
crypto_hash_BYTES = 64;
Loading