New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

age-encryption

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

age-encryption - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

README.md

62

index.test.ts
import { strict as assert } from 'assert'
import { base64_variants, from_base64, from_string, to_string } from "libsodium-wrappers-sumo"
import { AgeDecrypter, AgeEncrypter, generateX25519Identity, x25519IdentityToRecipient } from "."
import { base64_variants, from_base64, to_string } from "libsodium-wrappers-sumo"
import { Decrypter, Encrypter, generateIdentity, identityToRecipient } from "."

@@ -9,12 +9,12 @@ const fromBase64 = (s: string) => from_base64(s, base64_variants.ORIGINAL_NO_PADDING)

it('should decrypt a file with the right passphrase', async function () {
const d = new AgeDecrypter()
const d = new Decrypter()
d.addPassphrase("light-original-energy-average-wish-blind-vendor-pencil-illness-scorpion")
const file = fromBase64("YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNjcnlwdCB4Y2lkcXJQdmwwZzRROEZ5eXU4dHNnIDgKNnM2Ylp2Vlg2b0NBSVp2QkxCZEhJbEJrYUcreWRIZHVHWVpBaUJkUy9ZMAotLS0gZ280TkNGT05VTDEwZW5WRjVPMnkxem05eWQwdkM0S09hSU1nV05aYW5QSQom4WH7RYXsjlDm3HNKCe9gY2IfCjTY/2t6PF4bzUkeWZWkE7kd")
assert.deepEqual(to_string(await d.decrypt(file)), "test\n")
assert.equal(await d.decrypt(file, "text"), "test\n")
})
it('should decrypt a file with the right identity', async function () {
const d = new AgeDecrypter()
const d = new Decrypter()
d.addIdentity("AGE-SECRET-KEY-1L27NYJDYRNDSCCELNZE8C6JTSH22TLQJVPGD7289KDLMZA5HWN6SZPEHGF")
const file = fromBase64("YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOb280UHUyVWZwTllzY3Z5OU1tTjlscHV1Smt4Nng0MEZkdGZoQzd1dVFZCmk0VUNvVmoxbEhHalV0bVR2MHFyRGl0YzNtMXdoY1oyVUtvWDU3MUQwR1EKLS0tIGJ1RTZSYmR6ZlNHSk5tSGl3U2hqR1FFUDF4eEdjSGZtbXlYQUN4SnM4RDAKyqdZXpg65sTtmakjxLONtEgaSwXeS8t+7jAWvlleVEFO4/9QIQ")
assert.deepEqual(to_string(await d.decrypt(file)), "test\n")
assert.equal(await d.decrypt(file, "text"), "test\n")
})

@@ -25,14 +25,14 @@ })

it('should encrypt and decrypt a file', async function () {
const identity = await generateX25519Identity()
const recipient = await x25519IdentityToRecipient(identity)
const identity = await generateIdentity()
const recipient = await identityToRecipient(identity)
const e = new AgeEncrypter()
const e = new Encrypter()
e.addRecipient(recipient)
const file = await e.encrypt(from_string("age"))
const file = await e.encrypt("age")
const d = new AgeDecrypter()
const d = new Decrypter()
d.addIdentity(identity)
const out = await d.decrypt(file)
const out = await d.decrypt(file, "text")
assert.deepEqual(to_string(out), "age")
assert.equal(out, "age")
})

@@ -43,8 +43,8 @@ })

it('should encrypt (and decrypt) a file with a passphrase', async function () {
const e = new AgeEncrypter()
const e = new Encrypter()
e.setScryptWorkFactor(12)
e.addPassphrase("light-original-energy-average-wish-blind-vendor-pencil-illness-scorpion")
const file = await e.encrypt(from_string("age"))
e.setPassphrase("light-original-energy-average-wish-blind-vendor-pencil-illness-scorpion")
const file = await e.encrypt("age")
const d = new AgeDecrypter()
const d = new Decrypter()
d.addPassphrase("light-original-energy-average-wish-blind-vendor-pencil-illness-scorpion")

@@ -56,7 +56,7 @@ const out = await d.decrypt(file)

it('should encrypt (and decrypt) a file with a recipient', async function () {
const e = new AgeEncrypter()
const e = new Encrypter()
e.addRecipient("age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl6")
const file = await e.encrypt(from_string("age"))
const file = await e.encrypt("age")
const d = new AgeDecrypter()
const d = new Decrypter()
d.addIdentity("AGE-SECRET-KEY-1RKH0DGHQ0FU6VLXX2VW6Y3W2TKK7KR4J36N9SNDXK75JHCJ3N6JQNZJF5J")

@@ -68,8 +68,8 @@ const out = await d.decrypt(file)

it('should encrypt (and decrypt) a file with multiple recipients', async function () {
const e = new AgeEncrypter()
const e = new Encrypter()
e.addRecipient("age12wv74vxhhp9kg29j2wzm50c9p4urn7py0t4tzdgz6m0pcqjzmu9qqpzjqn")
e.addRecipient("age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl6")
const file = await e.encrypt(from_string("age"))
const file = await e.encrypt("age")
const d = new AgeDecrypter()
const d = new Decrypter()
d.addIdentity("AGE-SECRET-KEY-1RKH0DGHQ0FU6VLXX2VW6Y3W2TKK7KR4J36N9SNDXK75JHCJ3N6JQNZJF5J")

@@ -81,11 +81,11 @@ const out = await d.decrypt(file)

it('should throw when using multiple passphrases', function () {
const e = new AgeEncrypter()
e.addPassphrase("1")
const e = new Encrypter()
e.setPassphrase("1")
assert.throws(function () {
e.addPassphrase("2")
e.setPassphrase("2")
})
})
it('should throw when using passphrases and recipients', function () {
const e = new AgeEncrypter()
e.addPassphrase("1")
const e = new Encrypter()
e.setPassphrase("1")
assert.throws(function () {

@@ -96,10 +96,10 @@ e.addRecipient("age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl6")

it('should throw when using recipients and passphrases', function () {
const e = new AgeEncrypter()
const e = new Encrypter()
e.addRecipient("age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl6")
assert.throws(function () {
e.addPassphrase("2")
e.setPassphrase("2")
})
})
it('should throw when using bad recipients', function () {
const e = new AgeEncrypter()
const e = new Encrypter()
assert.throws(function () {

@@ -106,0 +106,0 @@ e.addRecipient("age1tgyuvdlmpejqsdf847hevurz9szk7vf3j7ytfyqecgzvphvu2d8qrtaxl")

import * as sodium from "libsodium-wrappers-sumo"
import { from_string } from "libsodium-wrappers-sumo"
import { from_string, to_string } from "libsodium-wrappers-sumo"
import { decode as decodeBech32, encode as encodeBech32 } from "bech32-buffer"

@@ -8,3 +8,3 @@ import { decodeBase64, encodeBase64, encodeHeader, encodeHeaderNoMAC, parseHeader, Stanza } from "./lib/format"

export async function generateX25519Identity(): Promise<string> {
export async function generateIdentity(): Promise<string> {
await sodium.ready

@@ -16,3 +16,3 @@

export async function x25519IdentityToRecipient(identity: string): Promise<string> {
export async function identityToRecipient(identity: string): Promise<string> {
await sodium.ready

@@ -30,3 +30,3 @@

export class AgeEncrypter {
export class Encrypter {
private passphrase: string | null = null

@@ -36,3 +36,3 @@ private scryptWorkFactor = 18

addPassphrase(s: string): void {
setPassphrase(s: string): void {
if (this.passphrase !== null)

@@ -60,5 +60,9 @@ throw new Error("can encrypt to at most one passphrase")

async encrypt(file: Uint8Array): Promise<Uint8Array> {
async encrypt(file: Uint8Array | string): Promise<Uint8Array> {
await sodium.ready
if (typeof file === "string") {
file = from_string(file)
}
const fileKey = sodium.randombytes_buf(16)

@@ -119,3 +123,3 @@ const stanzas: Stanza[] = []

export class AgeDecrypter {
export class Decrypter {
private passphrases: string[] = []

@@ -140,3 +144,5 @@ private identities: x25519Identity[] = []

async decrypt(file: Uint8Array): Promise<Uint8Array> {
async decrypt(file: Uint8Array, outputFormat?: "uint8array"): Promise<Uint8Array>
async decrypt(file: Uint8Array, outputFormat: "text"): Promise<string>
async decrypt(file: Uint8Array, outputFormat?: "text" | "uint8array"): Promise<Uint8Array | string> {
await sodium.ready

@@ -159,3 +165,5 @@

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

@@ -162,0 +170,0 @@

{
"name": "age-encryption",
"version": "0.0.1",
"version": "0.0.2",
"main": "index.ts",

@@ -5,0 +5,0 @@ "repository": "github:FiloSottile/age.ts",

import * as assert from 'assert'
import { AgeDecrypter } from '.'
import { Decrypter } from '.'
import { readFileSync, readdirSync } from 'fs'

@@ -33,3 +33,3 @@ import { crypto_hash_sha256, from_hex, to_hex } from 'libsodium-wrappers-sumo'

it(vec.name + " should succeed", async function () {
const d = new AgeDecrypter()
const d = new Decrypter()
if (vec.meta.passphrase)

@@ -61,3 +61,3 @@ d.addPassphrase(vec.meta.passphrase)

it(vec.name + " should fail", async function () {
const d = new AgeDecrypter()
const d = new Decrypter()
if (vec.meta.passphrase)

@@ -64,0 +64,0 @@ d.addPassphrase(vec.meta.passphrase)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc