mirror of
https://github.com/ente-io/ente.git
synced 2025-08-09 07:48:52 +00:00
B64
This commit is contained in:
parent
44bdb016a8
commit
371fda4e97
@ -2,12 +2,21 @@
|
|||||||
import * as libsodium from "./libsodium";
|
import * as libsodium from "./libsodium";
|
||||||
import type {
|
import type {
|
||||||
DecryptBlobB64,
|
DecryptBlobB64,
|
||||||
|
EncryptB64,
|
||||||
EncryptBytes,
|
EncryptBytes,
|
||||||
EncryptedBlobB64,
|
EncryptedBlobB64,
|
||||||
EncryptedBlobBytes,
|
EncryptedBlobBytes,
|
||||||
EncryptJSON,
|
EncryptJSON,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
|
const EncryptB64ToBytes = async ({
|
||||||
|
dataB64,
|
||||||
|
keyB64,
|
||||||
|
}: EncryptB64): Promise<EncryptBytes> => ({
|
||||||
|
data: await libsodium.fromB64(dataB64),
|
||||||
|
keyB64,
|
||||||
|
});
|
||||||
|
|
||||||
const EncryptedBlobBytesToB64 = async ({
|
const EncryptedBlobBytesToB64 = async ({
|
||||||
encryptedData,
|
encryptedData,
|
||||||
decryptionHeaderB64,
|
decryptionHeaderB64,
|
||||||
@ -16,6 +25,9 @@ const EncryptedBlobBytesToB64 = async ({
|
|||||||
decryptionHeaderB64,
|
decryptionHeaderB64,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const _encryptBoxB64 = (r: EncryptB64) =>
|
||||||
|
EncryptB64ToBytes(r).then((rb) => libsodium.encryptBox(rb));
|
||||||
|
|
||||||
export const _encryptAssociatedData = libsodium.encryptBlob;
|
export const _encryptAssociatedData = libsodium.encryptBlob;
|
||||||
|
|
||||||
export const _encryptThumbnail = _encryptAssociatedData;
|
export const _encryptThumbnail = _encryptAssociatedData;
|
||||||
|
@ -54,6 +54,7 @@ import * as ei from "./ente-impl";
|
|||||||
import type {
|
import type {
|
||||||
DecryptBlobB64,
|
DecryptBlobB64,
|
||||||
DecryptBlobBytes,
|
DecryptBlobBytes,
|
||||||
|
EncryptB64,
|
||||||
EncryptBytes,
|
EncryptBytes,
|
||||||
EncryptJSON,
|
EncryptJSON,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
@ -71,6 +72,23 @@ const assertInWorker = <T>(x: T): T => {
|
|||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt arbitrary data using the given key and a randomly generated nonce.
|
||||||
|
*
|
||||||
|
* Use {@link decryptBoxB64} to decrypt the result.
|
||||||
|
*
|
||||||
|
* ee {@link encryptBox} for the implementation details.
|
||||||
|
*
|
||||||
|
* > The suffix "Box" comes from the fact that it uses the so called secretbox
|
||||||
|
* > APIs provided by libsodium under the hood.
|
||||||
|
* >
|
||||||
|
* > See: [Note: 3 forms of encryption (Box | Blob | Stream)]
|
||||||
|
*/
|
||||||
|
export const encryptBoxB64 = (r: EncryptB64) =>
|
||||||
|
inWorker()
|
||||||
|
? ei._encryptBoxB64(r)
|
||||||
|
: sharedCryptoWorker().then((w) => w.encryptBoxB64(r));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt arbitrary data associated with an Ente object (file, collection,
|
* Encrypt arbitrary data associated with an Ente object (file, collection,
|
||||||
* entity) using the object's key.
|
* entity) using the object's key.
|
||||||
|
@ -205,7 +205,7 @@ export async function fromHex(input: string) {
|
|||||||
*
|
*
|
||||||
* 3. Box returns a "nonce", while Blob returns a "header".
|
* 3. Box returns a "nonce", while Blob returns a "header".
|
||||||
*/
|
*/
|
||||||
const encryptBox = async ({
|
export const encryptBox = async ({
|
||||||
data,
|
data,
|
||||||
keyB64,
|
keyB64,
|
||||||
}: EncryptBytes): Promise<EncryptedBoxBytes> => {
|
}: EncryptBytes): Promise<EncryptedBoxBytes> => {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* An encryption request with the plaintext data as bytes.
|
* An encryption request with the data to encrypt provided as bytes.
|
||||||
*/
|
*/
|
||||||
export interface EncryptBytes {
|
export interface EncryptBytes {
|
||||||
/**
|
/**
|
||||||
@ -13,9 +13,21 @@ export interface EncryptBytes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An encryption request with the plaintext data as a JSON value.
|
* A variant of {@link EncryptBytes} with the data as base64 encoded string.
|
||||||
*
|
*/
|
||||||
* This is a variant of {@link EncryptBytes}.
|
export interface EncryptB64 {
|
||||||
|
/**
|
||||||
|
* A base64 string containing the data to encrypt.
|
||||||
|
*/
|
||||||
|
dataB64: string;
|
||||||
|
/**
|
||||||
|
* A base64 string containing the encryption key.
|
||||||
|
*/
|
||||||
|
keyB64: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A variant of {@link EncryptBytes} with the data as a JSON value.
|
||||||
*/
|
*/
|
||||||
export interface EncryptJSON {
|
export interface EncryptJSON {
|
||||||
/**
|
/**
|
||||||
|
@ -12,6 +12,7 @@ import * as libsodium from "./libsodium";
|
|||||||
* Note: Keep these methods logic free. They are meant to be trivial proxies.
|
* Note: Keep these methods logic free. They are meant to be trivial proxies.
|
||||||
*/
|
*/
|
||||||
export class CryptoWorker {
|
export class CryptoWorker {
|
||||||
|
encryptBoxB64 = ei._encryptBoxB64;
|
||||||
encryptThumbnail = ei._encryptThumbnail;
|
encryptThumbnail = ei._encryptThumbnail;
|
||||||
encryptMetadataJSON = ei._encryptMetadataJSON;
|
encryptMetadataJSON = ei._encryptMetadataJSON;
|
||||||
decryptThumbnail = ei._decryptThumbnail;
|
decryptThumbnail = ei._decryptThumbnail;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user