Skip to content

Commit 6f1f207

Browse files
authored
Merge pull request #7 from phasehq/b64
BREAKING CHANGE: use base64 encoding for data
2 parents be57755 + 16a0d29 commit 6f1f207

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@phase.dev/phase-node",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Node.js Server SDK for Phase",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/utils/crypto.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ export const encryptString = async (plaintext: string, key: Uint8Array) => {
7171
await _sodium.ready;
7272
const sodium = _sodium;
7373

74-
return sodium.to_hex(await encryptRaw(sodium.from_string(plaintext), key));
74+
return sodium.to_base64(
75+
await encryptRaw(sodium.from_string(plaintext), key),
76+
sodium.base64_variants.ORIGINAL
77+
);
7578
};
7679

7780
/**
@@ -85,7 +88,12 @@ export const decryptString = async (cipherText: string, key: Uint8Array) => {
8588
await _sodium.ready;
8689
const sodium = _sodium;
8790

88-
return sodium.to_string(await decryptRaw(sodium.from_hex(cipherText), key));
91+
return sodium.to_string(
92+
await decryptRaw(
93+
sodium.from_base64(cipherText, sodium.base64_variants.ORIGINAL),
94+
key
95+
)
96+
);
8997
};
9098

9199
/**

tests/index.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ describe("Phase", () => {
5858
expect(segments[4]).toBe(tag);
5959
// Check if the one-time public key and ciphertext are valid hex strings
6060
expect(segments[2]).toMatch(/^[0-9a-f]+$/);
61-
expect(segments[3]).toMatch(/^[0-9a-f]+$/);
61+
expect(segments[3]).toMatch(
62+
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/
63+
);
6264
});
6365

6466
test("Check if Phase encrypt always produces ciphertexts (ph:*) of the same length for the same plaintext", async () => {

version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const LIB_VERSION = "1.0.0";
1+
export const LIB_VERSION = "2.0.0";

0 commit comments

Comments
 (0)