Skip to content

Remove Buffer dependency #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function keygen (key) {
}

function encrypt (plaintext, key, ciphertext) {
assert(Buffer.isBuffer(plaintext))
assert(plaintext instanceof Uint8Array)
var clen = encryptLength(plaintext)
if (ciphertext == null) ciphertext = Buffer.alloc(clen)
if (ciphertext == null) ciphertext = new Uint8Array(clen)
assert(key.byteLength === KEYBYTES, 'key must be KEYBYTES long')
assert(ciphertext.byteLength === clen, 'ciphertext must be plaintext + HEADERBYTES long')

Expand All @@ -34,12 +34,12 @@ function encrypt (plaintext, key, ciphertext) {
}

function encryptLength (plaintext) {
assert(Buffer.isBuffer(plaintext))
assert(plaintext instanceof Uint8Array)
return plaintext.byteLength + HEADERBYTES
}

function decrypt (ciphertext, key, plaintext) {
assert(Buffer.isBuffer(ciphertext))
assert(ciphertext instanceof Uint8Array)
var plen = decryptLength(ciphertext)
if (plaintext == null) plaintext = sodium.sodium_malloc(plen)
assert(key.byteLength === KEYBYTES, 'key must be KEYBYTES long')
Expand All @@ -54,7 +54,7 @@ function decrypt (ciphertext, key, plaintext) {
}

function decryptLength (ciphertext) {
assert(Buffer.isBuffer(ciphertext))
assert(ciphertext instanceof Uint8Array)
return ciphertext.byteLength - HEADERBYTES
}

Expand Down