Skip to content

Commit

Permalink
make it easier to call for most use cases by having crypto default to…
Browse files Browse the repository at this point in the history
… window.crypto
  • Loading branch information
diegommm committed Oct 7, 2023
1 parent 69916ca commit 93425c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ exports.defaultIvLength = 96;
* a string in it so you still have the chance to check your options. Otherwise
* use properly defined data in constants or sanitize your inputs.
*
* @param crypto - The Crypto implementation, which is typically found as
* window.crypto in browser contexts.
* @param backend - A writable Svelte store that implements set and subscribe.
* Data will be written to and read from this store, and is expected to hold
* a string.
* @param password - The encryption password.
* @param opts - An optional object containing additional arguments.
* @param crypto - The Crypto implementation, which is typically found as
* window.crypto in browser contexts.
*
* @example
* ```
Expand All @@ -66,23 +66,23 @@ exports.defaultIvLength = 96;
* console.log($backend); // prints the Base64 encoded IV+Salt+Ciphertext
* ```
*/
exports.subtleCryptoStore = Object.freeze(function (crypto, backend, password, opts) {
exports.subtleCryptoStore = Object.freeze(function (backend, password, opts, cr = window.crypto) {
if (typeof opts === 'undefined')
opts = {};
const o = Object.freeze({
iterations: positiveIntOrDefault(opts, 'iterations', exports.defaultIterations),
saltLength: positiveIntOrDefault(opts, 'saltLength', exports.defaultSaltLength),
ivLength: positiveIntOrDefault(opts, 'ivLength', exports.defaultIvLength),
});
const getMemoized = newMemoized(crypto.subtle, password);
const decrypt = newDecryptFunc(crypto, getMemoized, o);
const getMemoized = newMemoized(cr.subtle, password);
const decrypt = newDecryptFunc(cr, getMemoized, o);
return Object.freeze({
subscribe: Object.freeze((0, store_1.derived)(backend, decrypt, Promise.resolve('')).subscribe),
set: Object.freeze(function (value) {
return __awaiter(this, void 0, void 0, function* () {
try {
let newVal;
newVal = yield encrypt(crypto, getMemoized, o, value);
newVal = yield encrypt(cr, getMemoized, o, value);
backend.set(newVal);
return Promise.resolve(undefined);
}
Expand All @@ -96,7 +96,7 @@ exports.subtleCryptoStore = Object.freeze(function (crypto, backend, password, o
try {
let newVal;
const cur = decrypt((0, store_1.get)(backend));
newVal = yield encrypt(crypto, getMemoized, o, updater(cur));
newVal = yield encrypt(cr, getMemoized, o, updater(cur));
backend.set(newVal);
return Promise.resolve(undefined);
}
Expand Down
14 changes: 7 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ type ProcessedOptions = Readonly<Required<Options>>;
* a string in it so you still have the chance to check your options. Otherwise
* use properly defined data in constants or sanitize your inputs.
*
* @param crypto - The Crypto implementation, which is typically found as
* window.crypto in browser contexts.
* @param backend - A writable Svelte store that implements set and subscribe.
* Data will be written to and read from this store, and is expected to hold
* a string.
* @param password - The encryption password.
* @param opts - An optional object containing additional arguments.
* @param crypto - The Crypto implementation, which is typically found as
* window.crypto in browser contexts.
*
* @example
* ```
Expand All @@ -65,10 +65,10 @@ type ProcessedOptions = Readonly<Required<Options>>;
* ```
*/
export const subtleCryptoStore = Object.freeze(function(
crypto: Crypto,
backend: Writable<string>,
password: string,
opts?: Options,
cr: Crypto = window.crypto,
): Readonly<Writable<Promise<string>>> {
if (typeof opts === 'undefined')
opts = {};
Expand All @@ -78,9 +78,9 @@ export const subtleCryptoStore = Object.freeze(function(
ivLength: positiveIntOrDefault(opts, 'ivLength', defaultIvLength),
});

const getMemoized = newMemoized(crypto.subtle, password);
const getMemoized = newMemoized(cr.subtle, password);

const decrypt = newDecryptFunc(crypto, getMemoized, o);
const decrypt = newDecryptFunc(cr, getMemoized, o);

return Object.freeze({
subscribe: Object.freeze(
Expand All @@ -95,7 +95,7 @@ export const subtleCryptoStore = Object.freeze(function(
): Promise<undefined> {
try {
let newVal: string;
newVal = await encrypt(crypto, getMemoized, o, value);
newVal = await encrypt(cr, getMemoized, o, value);
backend.set(newVal);
return Promise.resolve(undefined);
} catch(e) {
Expand All @@ -110,7 +110,7 @@ export const subtleCryptoStore = Object.freeze(function(
try {
let newVal: string;
const cur = decrypt(get(backend));
newVal = await encrypt(crypto, getMemoized, o, updater(cur));
newVal = await encrypt(cr, getMemoized, o, updater(cur));
backend.set(newVal);
return Promise.resolve(undefined);
} catch(e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-subtlecrypto-store",
"version": "1.3.0",
"version": "2.0.0",
"description": "Svelte writable store using SubtleCrypto API",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 93425c1

Please sign in to comment.