@consento/crypto
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -14,3 +14,3 @@ "use strict"; | ||
/* eslint @typescript-eslint/camelcase: "off" */ | ||
const { randombytes_buf, crypto_scalarmult_base, crypto_scalarmult, crypto_scalarmult_BYTES, crypto_kdf_CONTEXTBYTES, crypto_box_seal, crypto_box_seal_open, crypto_box_keypair, crypto_box_SEALBYTES, crypto_box_SECRETKEYBYTES, crypto_box_PUBLICKEYBYTES, crypto_sign_PUBLICKEYBYTES, crypto_sign_SECRETKEYBYTES, crypto_sign_BYTES, crypto_sign_detached, crypto_sign_verify_detached, crypto_sign_keypair, crypto_secretbox_easy, crypto_secretbox_open_easy, crypto_secretbox_NONCEBYTES, crypto_secretbox_MACBYTES, crypto_secretbox_KEYBYTES, sodium_malloc } = sodium.default; | ||
const { crypto_kdf_derive_from_key, randombytes_buf, crypto_scalarmult_base, crypto_scalarmult, crypto_scalarmult_BYTES, crypto_kdf_BYTES_MAX, crypto_kdf_CONTEXTBYTES, crypto_box_seal, crypto_box_seal_open, crypto_box_keypair, crypto_box_SEALBYTES, crypto_box_SECRETKEYBYTES, crypto_box_PUBLICKEYBYTES, crypto_sign_PUBLICKEYBYTES, crypto_sign_SECRETKEYBYTES, crypto_sign_BYTES, crypto_sign_detached, crypto_sign_verify_detached, crypto_sign_keypair, crypto_secretbox_easy, crypto_secretbox_open_easy, crypto_secretbox_NONCEBYTES, crypto_secretbox_MACBYTES, crypto_secretbox_KEYBYTES, sodium_malloc } = sodium.default; | ||
function randomBuffer(size) { | ||
@@ -51,2 +51,7 @@ const buffer = sodium_malloc(size); | ||
}, | ||
async deriveKdfKey(key, index = 1) { | ||
const derivedKey = sodium_malloc(crypto_kdf_BYTES_MAX); | ||
crypto_kdf_derive_from_key(derivedKey, index, deriveContext, key); | ||
return derivedKey; | ||
}, | ||
async encrypt(secretKey, body) { | ||
@@ -53,0 +58,0 @@ const nonce = randomBuffer(crypto_secretbox_NONCEBYTES); |
@@ -35,3 +35,8 @@ "use strict"; | ||
} | ||
const deriveContext = 'conotify'; | ||
exports.sodium = { | ||
async deriveKdfKey(key, index = 1) { | ||
const { crypto_kdf_derive_from_key, crypto_kdf_BYTES_MAX } = await libsodium; | ||
return crypto_kdf_derive_from_key(crypto_kdf_BYTES_MAX, index, deriveContext, assertUint8(key)); | ||
}, | ||
sign, | ||
@@ -38,0 +43,0 @@ verify, |
@@ -20,2 +20,3 @@ import { IEncodable } from '../util/buffer'; | ||
export interface ICryptoCore { | ||
deriveKdfKey(key: Uint8Array, index?: number): Promise<Uint8Array>; | ||
sign(signSecretKey: Uint8Array, body: Uint8Array): Promise<Uint8Array>; | ||
@@ -22,0 +23,0 @@ verify(signPublicKey: Uint8Array, signature: Uint8Array, body: Uint8Array): Promise<boolean>; |
{ | ||
"name": "@consento/crypto", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Crypto functionality used in Consento", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -177,4 +177,43 @@ # @consento/crypto | ||
## Blob Support | ||
The crypto api also provides primitives for working with encrypted blobs: | ||
```javascript | ||
const { encryptBlob, decryptBlob, isEncryptedBlob } = setup(sodium) | ||
const { | ||
blob, // Information about a blob: to pass around | ||
encrypted // Encrypted data to be stored | ||
} = await encryptBlob('Hello Secret!') | ||
blob.path // Path at which to store the encrypted data | ||
blob.secretKey // Secretkey to decrypt this data | ||
blob.size // Number of bytes of the encrypted blob (only available after encryption) | ||
isEncryptedBlob(blob) // To verify if a set of data is a blob | ||
const decrypted = await decryptBlob(blob.secretKey, encrypted) | ||
``` | ||
Blob information is serializable with `toJSON` and deserializable using `toEncryptedBlob`. | ||
```javascript | ||
const { encryptBlob, decryptBlob, toEncryptedBlob } = setup(sodium) | ||
const { blob } = await encryptBlob('Hello Secret!') | ||
const blobJSON = blob.toJSON() | ||
const sameBlob = toEncryptedBlob(blobJSON) | ||
``` | ||
It is possible to restore a blob from it's `secretKey` but that requires async computation: | ||
```javascript | ||
const { encryptBlob, decryptBlob, toEncryptedBlob } = setup(sodium) | ||
const { blob } = await encryptBlob('Hello Secret!') | ||
const sameBlob = await toEncryptedBlob(blob.secretKey) | ||
``` | ||
## License | ||
[MIT](./LICENSE) |
@@ -5,6 +5,8 @@ "use strict"; | ||
const handshake_1 = require("./handshake"); | ||
const blob_1 = require("./blob"); | ||
function create(crypto) { | ||
const primitives = primitives_1.setupPrimitives(crypto); | ||
const handshake = handshake_1.setupHandshake(crypto, primitives); | ||
return Object.assign(Object.assign({}, primitives), handshake); | ||
const blob = blob_1.setupBlob(crypto); | ||
return Object.assign(Object.assign(Object.assign({}, primitives), handshake), blob); | ||
} | ||
@@ -11,0 +13,0 @@ exports.create = create; |
@@ -15,2 +15,3 @@ import * as sodium from 'sodium-universal' | ||
const { | ||
crypto_kdf_derive_from_key, | ||
randombytes_buf, | ||
@@ -20,2 +21,3 @@ crypto_scalarmult_base, | ||
crypto_scalarmult_BYTES, | ||
crypto_kdf_BYTES_MAX, | ||
crypto_kdf_CONTEXTBYTES, | ||
@@ -82,2 +84,7 @@ crypto_box_seal, | ||
}, | ||
async deriveKdfKey (key: Uint8Array, index: number = 1) { | ||
const derivedKey = sodium_malloc(crypto_kdf_BYTES_MAX) | ||
crypto_kdf_derive_from_key(derivedKey, index, deriveContext, key) | ||
return derivedKey | ||
}, | ||
async encrypt (secretKey: Uint8Array, body: IEncodable): Promise<Uint8Array> { | ||
@@ -84,0 +91,0 @@ const nonce = randomBuffer(crypto_secretbox_NONCEBYTES) |
@@ -40,2 +40,3 @@ /* eslint-disable @typescript-eslint/camelcase */ | ||
} | ||
async function verify (signPublicKey: Uint8Array, signature: Uint8Array, body: Uint8Array): Promise<boolean> { | ||
@@ -46,3 +47,9 @@ const { crypto_sign_verify_detached } = await libsodium | ||
const deriveContext = 'conotify' | ||
export const sodium: ICryptoCore = { | ||
async deriveKdfKey (key: Uint8Array, index: number = 1) { | ||
const { crypto_kdf_derive_from_key, crypto_kdf_BYTES_MAX } = await libsodium | ||
return crypto_kdf_derive_from_key(crypto_kdf_BYTES_MAX, index, deriveContext, assertUint8(key)) | ||
}, | ||
sign, | ||
@@ -49,0 +56,0 @@ verify, |
@@ -26,2 +26,3 @@ import { IEncodable } from '../util/buffer' | ||
export interface ICryptoCore { | ||
deriveKdfKey (key: Uint8Array, index?: number): Promise<Uint8Array> | ||
sign (signSecretKey: Uint8Array, body: Uint8Array): Promise<Uint8Array> | ||
@@ -28,0 +29,0 @@ verify (signPublicKey: Uint8Array, signature: Uint8Array, body: Uint8Array): Promise<boolean> |
@@ -5,2 +5,3 @@ import { ICryptoCore } from './core/types' | ||
import { setupHandshake } from './handshake' | ||
import { setupBlob } from './blob' | ||
@@ -10,5 +11,7 @@ export function create (crypto: ICryptoCore): IConsentoCrypto { | ||
const handshake = setupHandshake(crypto, primitives) | ||
const blob = setupBlob(crypto) | ||
return { | ||
...primitives, | ||
...handshake | ||
...handshake, | ||
...blob | ||
} | ||
@@ -15,0 +18,0 @@ } |
@@ -168,1 +168,22 @@ import { IEncryptedMessage, IDecryption } from './core/types' | ||
export type IConsentoCrypto = ICryptoPrimitives & ICryptoHandshake | ||
export interface IEncryptedBlob { | ||
secretKey: Uint8Array | ||
size?: number | ||
path: string[] | ||
toJSON (): IEncryptedBlobJSON | ||
} | ||
export interface IEncryptedBlobJSON { | ||
secretKey: string | ||
size?: number | ||
path: string[] | ||
} | ||
export interface IEncryptedBlobAPI { | ||
encryptBlob (encodable: IEncodable): Promise<{ blob: IEncryptedBlob, encrypted: Uint8Array }> | ||
decryptBlob (secretKey: Uint8Array, encrypted: Uint8Array): Promise<IEncodable> | ||
isEncryptedBlob (input: any): input is IEncryptedBlob | ||
toEncryptedBlob (secretKey: string | Uint8Array): Promise<IEncryptedBlob> | ||
toEncryptedBlob (blob: IEncryptedBlob | IEncryptedBlobJSON): IEncryptedBlob | ||
} |
@@ -138,1 +138,22 @@ import { IEncryptedMessage, IDecryption } from './core/types'; | ||
export declare type IConsentoCrypto = ICryptoPrimitives & ICryptoHandshake; | ||
export interface IEncryptedBlob { | ||
secretKey: Uint8Array; | ||
size?: number; | ||
path: string[]; | ||
toJSON(): IEncryptedBlobJSON; | ||
} | ||
export interface IEncryptedBlobJSON { | ||
secretKey: string; | ||
size?: number; | ||
path: string[]; | ||
} | ||
export interface IEncryptedBlobAPI { | ||
encryptBlob(encodable: IEncodable): Promise<{ | ||
blob: IEncryptedBlob; | ||
encrypted: Uint8Array; | ||
}>; | ||
decryptBlob(secretKey: Uint8Array, encrypted: Uint8Array): Promise<IEncodable>; | ||
isEncryptedBlob(input: any): input is IEncryptedBlob; | ||
toEncryptedBlob(secretKey: string | Uint8Array): Promise<IEncryptedBlob>; | ||
toEncryptedBlob(blob: IEncryptedBlob | IEncryptedBlobJSON): IEncryptedBlob; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
155873
83
2998
219