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

Replace Buffer with b4a #1

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
package-lock.json
sandbox.js
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const sodium = require('sodium-universal')
const b4a = require('b4a')

const ABYTES = sodium.crypto_secretstream_xchacha20poly1305_ABYTES
const TAG_MESSAGE = sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
Expand All @@ -7,51 +8,48 @@ const STATEBYTES = sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES
const HEADERBYTES = sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES
const KEYBYTES = sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES

if (!TAG_FINAL) throw new Error('JavaScript sodium version needs to support crypto_secretstream_xchacha20poly')
kasperisager marked this conversation as resolved.
Show resolved Hide resolved

const FINAL = TAG_FINAL[0]
const EMPTY = Buffer.alloc(0)
const TMP = Buffer.alloc(1)
const EMPTY = b4a.alloc(0)
const TAG = b4a.alloc(1)

class Push {
constructor (key, state = Buffer.allocUnsafe(STATEBYTES), header = Buffer.allocUnsafe(HEADERBYTES)) {
constructor (key, state = b4a.allocUnsafe(STATEBYTES), header = b4a.allocUnsafe(HEADERBYTES)) {
this.key = key
this.state = state
this.header = header

sodium.crypto_secretstream_xchacha20poly1305_init_push(this.state, this.header, this.key)
}

next (message, cipher = Buffer.allocUnsafe(message.length + ABYTES)) {
next (message, cipher = b4a.allocUnsafe(message.byteLength + ABYTES)) {
sodium.crypto_secretstream_xchacha20poly1305_push(this.state, cipher, message, null, TAG_MESSAGE)
return cipher
}

final (message = EMPTY, cipher = Buffer.allocUnsafe(ABYTES)) {
final (message = EMPTY, cipher = b4a.allocUnsafe(ABYTES)) {
sodium.crypto_secretstream_xchacha20poly1305_push(this.state, cipher, message, null, TAG_FINAL)
return cipher
}
}

class Pull {
constructor (key, state = Buffer.allocUnsafe(STATEBYTES)) {
constructor (key, state = b4a.allocUnsafe(STATEBYTES)) {
this.key = key
this.state = Buffer.allocUnsafe(STATEBYTES)
this.state = state
this.final = false
}

init (header) {
sodium.crypto_secretstream_xchacha20poly1305_init_pull(this.state, header, this.key)
}

next (cipher, message = Buffer.allocUnsafe(cipher.length - ABYTES)) {
sodium.crypto_secretstream_xchacha20poly1305_pull(this.state, message, TMP, cipher, null)
this.final = TMP[0] === FINAL
next (cipher, message = b4a.allocUnsafe(cipher.byteLength - ABYTES)) {
sodium.crypto_secretstream_xchacha20poly1305_pull(this.state, message, TAG, cipher, null)
this.final = b4a.equals(TAG, TAG_FINAL)
return message
}
}

function keygen (buf = Buffer.alloc(KEYBYTES)) {
function keygen (buf = b4a.alloc(KEYBYTES)) {
sodium.crypto_secretstream_xchacha20poly1305_keygen(buf)
return buf
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Wraps libsodiums secretstream in a higher level abstraction",
"main": "index.js",
"dependencies": {
"b4a": "^1.1.1",
"sodium-universal": "^3.0.4"
},
"devDependencies": {},
kasperisager marked this conversation as resolved.
Show resolved Hide resolved
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/sodium-secretstream.git"
Expand Down