Skip to content

Commit

Permalink
Merge pull request #86 from qgustavor/replacing-request
Browse files Browse the repository at this point in the history
Replace request with fetch (and fix other bugs)
  • Loading branch information
qgustavor authored Feb 19, 2022
2 parents a627db0 + f1a34fc commit 9c1d395
Show file tree
Hide file tree
Showing 45 changed files with 13,011 additions and 1,680 deletions.
18 changes: 0 additions & 18 deletions .babelrc

This file was deleted.

66 changes: 66 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This workflow will do a clean install of node dependencies, cache/restore them, run tests across different versions of node and deno

name: Test library

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
pre_check:
runs-on: ubuntu-latest
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master

test-node:
needs: pre_check
if: ${{ needs.pre_check.outputs.should_skip != 'true' }}
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm run lint-js
- run: npm run lint-ts
if: ${{ matrix.node-version != '14.x' }}
- run: npm run test-runner node

test-deno:
needs: pre_check
if: ${{ needs.pre_check.outputs.should_skip != 'true' }}
runs-on: ubuntu-latest

strategy:
matrix:
deno-version: [v1.x]

steps:
- uses: actions/checkout@v2
# Testing code still depends on Node
# (mostly because mega-mock: no plan to migrate it to Deno)
- uses: actions/setup-node@v2
with:
node-version: 16.x
cache: 'npm'
- name: Use Deno ${{ matrix.deno-version }}
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.deno-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test deno
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,3 @@ jspm_packages

# Dist files are available via NPM, in order to avoid git noise
dist

# Using package-lock.json introduces cross-platform issues
/package-lock.json
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

11 changes: 11 additions & 0 deletions ava.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
files: [
'test/**/*.test.mjs'
],
timeout: '20s',
failFast: true,
extensions: {
js: true,
mjs: true
}
}
90 changes: 55 additions & 35 deletions browser/aes.js → browser/aes.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AES as SjclAES } from './sjcl'
import { AES as SjclAES } from './sjcl.mjs'

// convert user-supplied password array
export function prepareKey (password) {
Expand Down Expand Up @@ -39,7 +39,7 @@ export function prepareKeyV2 (password, info, cb) {
name: 'PBKDF2',
salt,
iterations,
hash: {name: digest}
hash: { name: digest }
}, key, 256)
}).then(result => {
cb(null, Buffer.from(result))
Expand All @@ -56,7 +56,7 @@ class AES {

encryptCBC (buffer) {
let iv = [0, 0, 0, 0]
let d = Array(4)
const d = Array(4)
let i, j

for (i = 0; i < buffer.length; i += 16) {
Expand Down Expand Up @@ -160,45 +160,21 @@ class CTR {
this.ctr = Buffer.alloc(16)
this.nonce.copy(this.ctr, 0)

this.mac = Buffer.alloc(16)
this.nonce.copy(this.mac, 0)
this.nonce.copy(this.mac, 8)
this.macs = []

this.incrementCTR(start / 16)
}

condensedMac () {
if (this.mac) {
this.macs.push(this.mac)
this.mac = undefined
}

let mac = Buffer.alloc(16)

for (let i = 0; i < this.macs.length; i++) {
for (let j = 0; j < 16; j++) mac[j] ^= this.macs[i][j]
this.aes.encryptECB(mac)
}

const macBuffer = Buffer.allocUnsafe(8)
macBuffer.writeInt32BE(mac.readInt32BE(0) ^ mac.readInt32BE(4), 0)
macBuffer.writeInt32BE(mac.readInt32BE(8) ^ mac.readInt32BE(12), 4)
return macBuffer
}

encrypt (buffer) {
for (let i = 0; i < buffer.length; i += 16) {
const enc = this.aes.encryptECB(Buffer.from(this.ctr))

for (let j = 0; j < 16; j++) {
this.mac[j] ^= buffer[i + j]
buffer[i + j] ^= enc[j]
}

this.aes.encryptECB(this.mac)
this.incrementCTR()
}

return buffer
}

decrypt (buffer) {
Expand All @@ -207,17 +183,15 @@ class CTR {

for (let j = 0; j < 16; j++) {
buffer[i + j] ^= enc[j]
this.mac[j] ^= buffer[i + j]
}

this.aes.encryptECB(this.mac)
this.incrementCTR()
}

return buffer
}

incrementCTR (cnt = 1) {
for (let i = 0; i < cnt; i++) this.checkMacBounding()

const buf = this.ctr
let i = 15
let mod
Expand All @@ -229,8 +203,54 @@ class CTR {
if (i < 0) i = 15
}
}
}

class MAC {
constructor (aes, nonce, start = 0) {
this.aes = aes

this.nonce = nonce.slice(0, 8)
this.increment = 131072 // 2**17
this.posNext = this.increment
this.pos = 0

this.mac = Buffer.alloc(16)
this.nonce.copy(this.mac, 0)
this.nonce.copy(this.mac, 8)
this.macs = []
}

condense () {
if (this.mac) {
this.macs.push(this.mac)
this.mac = undefined
}

const mac = Buffer.alloc(16)

for (let i = 0; i < this.macs.length; i++) {
for (let j = 0; j < 16; j++) mac[j] ^= this.macs[i][j]
this.aes.encryptECB(mac)
}

const macBuffer = Buffer.allocUnsafe(8)
macBuffer.writeInt32BE(mac.readInt32BE(0) ^ mac.readInt32BE(4), 0)
macBuffer.writeInt32BE(mac.readInt32BE(8) ^ mac.readInt32BE(12), 4)
return macBuffer
}

update (buffer) {
for (let i = 0; i < buffer.length; i += 16) {
for (let j = 0; j < 16; j++) {
this.mac[j] ^= buffer[i + j]
}

this.aes.encryptECB(this.mac)
this.checkBounding()
}
}

checkMacBounding () {
checkBounding () {
this.pos += 16
if (this.pos >= this.posNext) {
this.macs.push(Buffer.from(this.mac))
Expand All @@ -245,4 +265,4 @@ class CTR {
}
}

export {AES, CTR}
export { AES, CTR, MAC }
2 changes: 2 additions & 0 deletions browser/fetch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const fetch = globalThis.fetch
export default fetch
2 changes: 2 additions & 0 deletions browser/noop.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const Agent = null
export default Agent
8 changes: 8 additions & 0 deletions browser/process-shim.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { Buffer } from 'buffer'
export const process = {
env: {},
nextTick: (fn, ...argv) => {
globalThis.setTimeout(fn, 0, ...argv)
}
}
export const global = globalThis
Loading

0 comments on commit 9c1d395

Please sign in to comment.