-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { base64 } from "@scure/base" | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import { type Encrypter, type Decrypter } from "./index.js" | ||
|
||
/** | ||
* Encode an age encrypted file using the ASCII armor format, a strict subset of | ||
* PEM that starts with `-----BEGIN AGE ENCRYPTED FILE-----`. | ||
* | ||
* @param file - The raw encrypted file (returned by {@link Encrypter.encrypt}). | ||
* | ||
* @returns The ASCII armored file, with a final newline. | ||
*/ | ||
export function encode(file: Uint8Array): string { | ||
const lines: string[] = [] | ||
lines.push("-----BEGIN AGE ENCRYPTED FILE-----\n") | ||
for (let i = 0; i < file.length; i += 48) { | ||
let end = i + 48 | ||
if (end > file.length) end = file.length | ||
lines.push(base64.encode(file.subarray(i, end)) + "\n") | ||
} | ||
lines.push("-----END AGE ENCRYPTED FILE-----\n") | ||
return lines.join("") | ||
} | ||
|
||
/** | ||
* Decode an age encrypted file from the ASCII armor format, a strict subset of | ||
* PEM that starts with `-----BEGIN AGE ENCRYPTED FILE-----`. | ||
* | ||
* Extra whitespace before and after the file is ignored, and newlines can be | ||
* CRLF or LF, but otherwise the format is parsed strictly. | ||
* | ||
* @param file - The ASCII armored file. | ||
* | ||
* @returns The raw encrypted file (to be passed to {@link Decrypter.decrypt}). | ||
*/ | ||
export function decode(file: string): Uint8Array { | ||
const lines = file.trim().replaceAll("\r\n", "\n").split("\n") | ||
if (lines[0] !== "-----BEGIN AGE ENCRYPTED FILE-----") { | ||
throw Error("invalid header") | ||
} | ||
if (lines[lines.length - 1] !== "-----END AGE ENCRYPTED FILE-----") { | ||
throw Error("invalid footer") | ||
} | ||
lines.shift() | ||
lines.pop() | ||
if (lines.some((l, i) => i === lines.length - 1 ? l.length > 64 : l.length !== 64)) { | ||
throw Error("line too long") | ||
} | ||
return base64.decode(lines.join("")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Encrypter, Decrypter, generateIdentity, identityToRecipient } from "age-encryption" | ||
import * as age from "age-encryption" | ||
|
||
const identity = await generateIdentity() | ||
const recipient = await identityToRecipient(identity) | ||
const identity = await age.generateIdentity() | ||
const recipient = await age.identityToRecipient(identity) | ||
console.log(identity) | ||
console.log(recipient) | ||
|
||
const e = new Encrypter() | ||
const e = new age.Encrypter() | ||
e.addRecipient(recipient) | ||
const ciphertext = await e.encrypt("Hello, age!") | ||
const armored = age.armor.encode(ciphertext) | ||
console.log(armored) | ||
|
||
const d = new Decrypter() | ||
const d = new age.Decrypter() | ||
d.addIdentity(identity) | ||
const out = await d.decrypt(ciphertext, "text") | ||
const decoded = age.armor.decode(armored) | ||
const out = await d.decrypt(decoded, "text") | ||
console.log(out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters