Skip to content

Commit

Permalink
Import libsodium-wrappers-sumo via the default export
Browse files Browse the repository at this point in the history
Fixes Node module resolution.

Partially based on #11 by @lgarron.

Fixes #9
  • Loading branch information
FiloSottile committed Nov 3, 2023
1 parent 7fe1494 commit 16bc44e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { base64_variants, from_base64, from_string, to_base64, to_string } from "libsodium-wrappers-sumo"
import sodium from "libsodium-wrappers-sumo"
const { base64_variants, from_base64, from_string, to_base64, to_string } = sodium

export const decodeBase64 = (s: string) => from_base64(s, base64_variants.ORIGINAL_NO_PADDING)
export const encodeBase64 = (s: Uint8Array) => to_base64(s, base64_variants.ORIGINAL_NO_PADDING)
Expand Down
5 changes: 2 additions & 3 deletions lib/hkdf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as sodium from "libsodium-wrappers-sumo"
import { from_string } from "libsodium-wrappers-sumo"
import sodium from "libsodium-wrappers-sumo"

// @types/libsodium-wrappers-sumo is missing these definitions.
declare module "libsodium-wrappers-sumo" {
Expand All @@ -17,7 +16,7 @@ export function HKDF(ikm: Uint8Array, salt: Uint8Array | null, info: string): Ui
const prk = sodium.crypto_auth_hmacsha256_final(h)

const infoAndCounter = new Uint8Array(info.length + 1)
infoAndCounter.set(from_string(info))
infoAndCounter.set(sodium.from_string(info))
infoAndCounter[info.length] = 1

return sodium.crypto_auth_hmacsha256(infoAndCounter, prk)
Expand Down
7 changes: 3 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as sodium from "libsodium-wrappers-sumo"
import { from_string, to_string } from "libsodium-wrappers-sumo"
import sodium from "libsodium-wrappers-sumo"
import { decode as decodeBech32, encode as encodeBech32 } from "bech32-buffer"
import { scryptUnwrap, scryptWrap, x25519Identity, x25519Unwrap, x25519Wrap } from "./recipients.js"
import { encodeHeader, encodeHeaderNoMAC, parseHeader, Stanza } from "./format.js"
Expand Down Expand Up @@ -74,7 +73,7 @@ class Encrypter {

encrypt(file: Uint8Array | string): Uint8Array {
if (typeof file === "string") {
file = from_string(file)
file = sodium.from_string(file)
}

const fileKey = sodium.randombytes_buf(16)
Expand Down Expand Up @@ -142,7 +141,7 @@ class Decrypter {
const payload = h.rest.subarray(16)

const out = decryptSTREAM(streamKey, payload)
if (outputFormat === "text") return to_string(out)
if (outputFormat === "text") return sodium.to_string(out)
return out
}

Expand Down
2 changes: 1 addition & 1 deletion lib/recipients.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as sodium from "libsodium-wrappers-sumo"
import sodium from "libsodium-wrappers-sumo"
import { decodeBase64, encodeBase64, Stanza } from "./format.js"
import { HKDF } from "./hkdf.js"

Expand Down
2 changes: 1 addition & 1 deletion lib/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as sodium from "libsodium-wrappers-sumo"
import sodium from "libsodium-wrappers-sumo"

// We can't use sodium.crypto_aead_chacha20poly1305_IETF_ABYTES here before
// sodium.ready, or it will make the constant be silently NaN, and nothing will
Expand Down

0 comments on commit 16bc44e

Please sign in to comment.