Skip to content

Commit

Permalink
chore(): All the linting
Browse files Browse the repository at this point in the history
  • Loading branch information
vipyne committed Jun 21, 2018
1 parent 596cb8c commit 198a6cc
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 212 deletions.
6 changes: 2 additions & 4 deletions base58.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const base58 = require('base-x')(
'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
)

const base58 = require('base-x')('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')

function encode(value) {
return base58.encode(value)
Expand Down
6 changes: 2 additions & 4 deletions base64.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const base64 = require('base-x')(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
)

const base64 = require('base-x')('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')

function encode(value) {
return base64.encode(value)
Expand Down
12 changes: 6 additions & 6 deletions blake2b.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
/* eslint-disable camelcase */

const { crypto_generichash_batch } = require('sodium-universal')
const isBuffer = require('is-buffer')
Expand All @@ -17,18 +17,18 @@ const { kDefaultBlake2bSize } = require('./constants')
* @throws TypeError
*/
function blake2b(buffer, size) {
if (null == size || 'number' != typeof size) {
size = kDefaultBlake2bSize
if (null == size || 'number' !== typeof size) {
size = kDefaultBlake2bSize // eslint-disable-line no-param-reassign
} else if (size <= 0) {
throw new TypeError("crypto.blake2b: Expecting size to be greater than 0.")
throw new TypeError('crypto.blake2b: Expecting size to be greater than 0.')
}

if (isBuffer(buffer)) {
buffer = [buffer]
buffer = [buffer] // eslint-disable-line no-param-reassign
}

if (false == Array.isArray(buffer)) {
throw new TypeError("crypto.blake2b: Expecting buffer as input.")
throw new TypeError('crypto.blake2b: Expecting buffer as input.')
}

for (let i = 0; i < buffer.length; ++i) {
Expand Down
5 changes: 3 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'
/* eslint-disable no-bitwise */
/* eslint-disable no-mixed-operators */

const { version } = require('./package')

const versions = version.split('.').map((v) => parseInt(v))
const versions = version.split('.').map(v => parseInt(v, 10))
const [
kVersionMajor,
kVersionMinor,
Expand Down
64 changes: 29 additions & 35 deletions decrypt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict'

const { createDecipheriv, createHmac } = require('crypto')
const isBuffer = require('is-buffer')
const uint64 = require('./uint64')
const pkg = require('./package')

const {
kVersion,
Expand All @@ -25,57 +22,55 @@ const {
*/
function decrypt(value, opts) {
if (null == value) {
throw new TypeError("crypto.decrypt: Encrypted value cannot be null.")
throw new TypeError('crypto.decrypt: Encrypted value cannot be null.')
} else if (isBuffer(value)) {
throw new TypeError("crypto.decrypt: Encrypted value cannot be a buffer.")
} else if (!value || 'object' != typeof value){
throw new TypeError("crypto.decrypt: Encrypted value to be an object.")
throw new TypeError('crypto.decrypt: Encrypted value cannot be a buffer.')
} else if (!value || 'object' !== typeof value) {
throw new TypeError('crypto.decrypt: Encrypted value to be an object.')
}

if (null == value.version || 'string' != typeof value.version) {
throw new TypeError("crypto.decrypt: Missing encryption version.")
if (null == value.version || 'string' !== typeof value.version) {
throw new TypeError('crypto.decrypt: Missing encryption version.')
}

if (null == value.id || 'string' != typeof value.id) {
throw new TypeError("crypto.decrypt: Missing encryption ID.")
if (null == value.id || 'string' !== typeof value.id) {
throw new TypeError('crypto.decrypt: Missing encryption ID.')
}

if (null == value.crypto || 'object' != typeof value.crypto) {
throw new TypeError(
"crypto.decrypt: Missing encryption crypto specification object.")
if (null == value.crypto || 'object' !== typeof value.crypto) {
throw new TypeError('crypto.decrypt: Missing encryption crypto specification object.')
}

if (!opts || 'object' != typeof opts) {
throw new TypeError("crypto.decrypt: Expecting options object.")
if (!opts || 'object' !== typeof opts) {
throw new TypeError('crypto.decrypt: Expecting options object.')
}

if (null == opts.key) {
throw new TypeError("crypto.decrypt: Expecting decryption key.")
} else if ('string' != typeof opts.key && false == isBuffer(opts.key)) {
throw new TypeError(
"crypto.decrypt: Expecting decryption key to be a string or buffer.")
throw new TypeError('crypto.decrypt: Expecting decryption key.')
} else if ('string' !== typeof opts.key && false == isBuffer(opts.key)) {
throw new TypeError('crypto.decrypt: Expecting decryption key to be a string or buffer.')
}

if (!opts.cipher || 'string' != typeof opts.cipher) {
opts.cipher = kDefaultCipher
if (!opts.cipher || 'string' !== typeof opts.cipher) {
opts.cipher = kDefaultCipher // eslint-disable-line no-param-reassign
}

if (!opts.digest || 'string' != typeof opts.digest) {
opts.digest = kDefaultDigest
if (!opts.digest || 'string' !== typeof opts.digest) {
opts.digest = kDefaultDigest // eslint-disable-line no-param-reassign
}

if ('string' != typeof opts.cipher) {
throw new TypeError("crypto.decrypt: Expecting cipher type to be a string.")
if ('string' !== typeof opts.cipher) {
throw new TypeError('crypto.decrypt: Expecting cipher type to be a string.')
}

if ('string' != typeof opts.digest) {
throw new TypeError("crypto.decrypt: Expecting digest type to be a string.")
if ('string' !== typeof opts.digest) {
throw new TypeError('crypto.decrypt: Expecting digest type to be a string.')
}

if (opts.strict) {
const version = uint64.encode(kVersion).toString('hex')
if (version != value.version) {
throw new TypeError("crypto.decrypt: Encryption version does not match (strict).")
throw new TypeError('crypto.decrypt: Encryption version does not match (strict).')
}
}

Expand All @@ -85,13 +80,12 @@ function decrypt(value, opts) {
const { key } = opts

if (null == iv) {
throw new TypeError("crypto.decrypt: Expecting decryption iv.")
} else if ('string' != typeof iv && false == isBuffer(iv)) {
throw new TypeError(
"crypto.decrypt: Expecting decryption iv to be a string or buffer.")
throw new TypeError('crypto.decrypt: Expecting decryption iv.')
} else if ('string' !== typeof iv && false == isBuffer(iv)) {
throw new TypeError('crypto.decrypt: Expecting decryption iv to be a string or buffer.')
}

if ('string' == typeof iv) {
if ('string' === typeof iv) {
iv = Buffer.from(iv, 'hex')
}

Expand All @@ -105,7 +99,7 @@ function decrypt(value, opts) {
const mac = hmac.read().toString('hex')

if (mac != value.crypto.mac) {
throw new TypeError("crypto.decrypt: HMAC digest does not match.")
throw new TypeError('crypto.decrypt: HMAC digest does not match.')
}

return Buffer.concat([
Expand Down
18 changes: 8 additions & 10 deletions discovery-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
/* eslint-disable camelcase */

const isBuffer = require('is-buffer')
const alloc = require('buffer-alloc-unsafe')
Expand All @@ -11,9 +11,7 @@ const {
} = require('sodium-universal')


const defaultDiscoveryKeyMessageKey = alloc(
crypto_generichash_KEYBYTES_MIN
).fill('ara')
const defaultDiscoveryKeyMessageKey = alloc(crypto_generichash_KEYBYTES_MIN).fill('ara')

/**
* Generate a discovery digest useful for network
Expand All @@ -25,20 +23,20 @@ const defaultDiscoveryKeyMessageKey = alloc(
* @throws TypeError
*/
function discoveryKey(buffer, size, key) {
if (null == size || 'number' != typeof size) {
size = kDefaultDiscoveryKeySize
if (null == size || 'number' !== typeof size) {
size = kDefaultDiscoveryKeySize // eslint-disable-line no-param-reassign
} else if (size <= 0) {
throw new TypeError("crypto.discoveryKey: Expecting size to be greater than 0.")
throw new TypeError('crypto.discoveryKey: Expecting size to be greater than 0.')
}

if (null == key) {
key = defaultDiscoveryKeyMessageKey
key = defaultDiscoveryKeyMessageKey // eslint-disable-line no-param-reassign
} else if (false == isBuffer(key)) {
throw new TypeError("crypto.discoveryKey: Expecting key to be a buffer")
throw new TypeError('crypto.discoveryKey: Expecting key to be a buffer')
}

if (false == isBuffer(buffer)) {
throw new TypeError("crypto.discoveryKey: Expecting buffer.")
throw new TypeError('crypto.discoveryKey: Expecting buffer.')
}

const digest = alloc(size)
Expand Down
44 changes: 20 additions & 24 deletions encrypt.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use strict'

const { createCipheriv, createHmac } = require('crypto')
const toBuffer = require('to-buffer')
const isBuffer = require('is-buffer')
const uint64 = require('./uint64')
const uuid = require('uuid/v4')
Expand All @@ -27,43 +24,42 @@ const {
*/
function encrypt(value, opts) {
if (null == value) {
throw new TypeError("crypto.encrypt: Expecting value. Got null.")
} else if ('string' != typeof value && false == isBuffer(value)) {
throw new TypeError(
"crypto.encrypt: Expecting value to be a string or buffer.")
throw new TypeError('crypto.encrypt: Expecting value. Got null.')
} else if ('string' !== typeof value && false == isBuffer(value)) {
throw new TypeError('crypto.encrypt: Expecting value to be a string or buffer.')
} else if (0 == value.length) {
throw new TypeError(
"crypto.encrypt: Cannot encrypt empty value.")
throw new TypeError('crypto.encrypt: Cannot encrypt empty value.')
}

if (!opts || 'object' != typeof opts) {
throw new TypeError("crypto.encrypt: Expecting options object.")
if (!opts || 'object' !== typeof opts) {
throw new TypeError('crypto.encrypt: Expecting options object.')
}

if (null == opts.key) {
throw new TypeError("crypto.encrypt: Expecting encryption key.")
} else if ('string' != typeof opts.key && false == isBuffer(opts.key)) {
throw new TypeError(
"crypto.encrypt: Expecting encryption key to be a string or buffer.")
throw new TypeError('crypto.encrypt: Expecting encryption key.')
} else if ('string' !== typeof opts.key && false == isBuffer(opts.key)) {
throw new TypeError('crypto.encrypt: Expecting encryption key to be a string or buffer.')
}

if (!opts.cipher || 'string' != typeof opts.cipher) {
opts.cipher = kDefaultCipher
if (!opts.cipher || 'string' !== typeof opts.cipher) {
opts.cipher = kDefaultCipher // eslint-disable-line no-param-reassign
}

if (!opts.digest || 'string' != typeof opts.digest) {
opts.digest = kDefaultDigest
if (!opts.digest || 'string' !== typeof opts.digest) {
opts.digest = kDefaultDigest // eslint-disable-line no-param-reassign
}

if ('string' != typeof opts.cipher) {
throw new TypeError("crypto.encrypt: Expecting cipher type to be a string.")
if ('string' !== typeof opts.cipher) {
throw new TypeError('crypto.encrypt: Expecting cipher type to be a string.')
}

if ('string' != typeof opts.digest) {
throw new TypeError("crypto.encrypt: Expecting digest type to be a string.")
if ('string' !== typeof opts.digest) {
throw new TypeError('crypto.encrypt: Expecting digest type to be a string.')
}

const { cipher, digest, key, iv } = opts
const {
cipher, digest, key, iv
} = opts
const cipheriv = createCipheriv(cipher, key, iv)
const hmac = createHmac(digest, key)

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'


const { discoveryKey } = require('./discovery-key')
const { randomBytes } = require('./random-bytes')
Expand Down
20 changes: 8 additions & 12 deletions key-pair.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
/* eslint-disable camelcase */

const isBuffer = require('is-buffer')
const alloc = require('buffer-alloc-unsafe')
Expand All @@ -22,22 +22,18 @@ const {
*/
function keyPair(seed) {
if (null === seed) {
throw new TypeError("crypto.keyPair: Seed cannot be null.")
throw new TypeError('crypto.keyPair: Seed cannot be null.')
} else if (null != seed) {
if (false == isBuffer(seed)) {
throw new TypeError("crypto.keyPair: Expecting seed to be a buffer.")
throw new TypeError('crypto.keyPair: Expecting seed to be a buffer.')
} else if (0 == seed.length) {
throw new TypeError("crypto.keyPair: Cannot use empty buffer as seed.")
throw new TypeError('crypto.keyPair: Cannot use empty buffer as seed.')
} else if (seed.length < crypto_sign_SEEDBYTES) {
throw new TypeError(
`crypto.keyPair: Seed buffer length too small. `+
`Expecting size ${crypto_sign_SEEDBYTES}.`
)
throw new TypeError('crypto.keyPair: Seed buffer length too small. ' +
`Expecting size ${crypto_sign_SEEDBYTES}.`)
} else if (seed.length > crypto_sign_SEEDBYTES) {
throw new TypeError(
`crypto.keyPair: Seed buffer length too large. `+
`Expecting size ${crypto_sign_SEEDBYTES}.`
)
throw new TypeError('crypto.keyPair: Seed buffer length too large. ' +
`Expecting size ${crypto_sign_SEEDBYTES}.`)
}
}

Expand Down
12 changes: 6 additions & 6 deletions random-bytes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
/* eslint-disable camelcase */

const { randombytes_buf } = require('sodium-universal')
const alloc = require('buffer-alloc-unsafe')
Expand All @@ -12,12 +12,12 @@ const alloc = require('buffer-alloc-unsafe')
* @throws TypeError
*/
function randomBytes(size) {
if (null == size || 'number' != typeof size) {
throw new TypeError("crypto.randomBytes: Expecting size to be a number.")
} else if (size != size) {
throw new TypeError("crypto.randomBytes: Size cannot be NaN.")
if (null == size || 'number' !== typeof size) {
throw new TypeError('crypto.randomBytes: Expecting size to be a number.')
} else if (size != size) { // eslint-disable-line no-self-compare
throw new TypeError('crypto.randomBytes: Size cannot be NaN.')
} else if (size <= 0) {
throw new TypeError("crypto.randomBytes: Size must be larger than 0.")
throw new TypeError('crypto.randomBytes: Size must be larger than 0.')
}

const buffer = alloc(size)
Expand Down
10 changes: 5 additions & 5 deletions sign.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
/* eslint-disable camelcase */

const isBuffer = require('is-buffer')
const alloc = require('buffer-alloc-unsafe')
Expand All @@ -19,15 +19,15 @@ const {
*/
function sign(message, secretKey) {
if (null == message || false == isBuffer(message)) {
throw new TypeError("crypto.sign: Expecting message to be a buffer.")
throw new TypeError('crypto.sign: Expecting message to be a buffer.')
} else if (0 == message.length) {
throw new TypeError("crypto.sign: Cannot sign an empty message.")
throw new TypeError('crypto.sign: Cannot sign an empty message.')
}

if (null == secretKey || false == isBuffer(secretKey)) {
throw new TypeError("crypto.sign: Expecting secretKey to be a buffer.")
throw new TypeError('crypto.sign: Expecting secretKey to be a buffer.')
} else if (0 == secretKey.length) {
throw new TypeError("crypto.sign: Cannot sign with an empty secretKey.")
throw new TypeError('crypto.sign: Cannot sign with an empty secretKey.')
}

const buffer = alloc(crypto_sign_BYTES)
Expand Down
Loading

0 comments on commit 198a6cc

Please sign in to comment.