@secrez/crypto
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "@secrez/crypto", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -15,2 +15,5 @@ # @secrez/crypto | ||
__0.1.4__ | ||
* adds more methods to encrypt and decrypt buffers too | ||
__0.1.2__ | ||
@@ -26,3 +29,3 @@ * moving all methods that don't work in a browser back to @secrez/core | ||
``` | ||
31 passing (713ms) | ||
32 passing (2s) | ||
@@ -32,4 +35,4 @@ ----------|---------|----------|---------|---------|------------------- | ||
----------|---------|----------|---------|---------|------------------- | ||
All files | 100 | 92.31 | 100 | 100 | | ||
index.js | 100 | 92.31 | 100 | 100 | 27-34,71,112 | ||
All files | 100 | 91.38 | 100 | 100 | | ||
index.js | 100 | 91.38 | 100 | 100 | 27-34,71,112,172 | ||
----------|---------|----------|---------|---------|------------------- | ||
@@ -36,0 +39,0 @@ ``` |
@@ -172,13 +172,9 @@ const crypto = require('crypto') | ||
static encrypt(message, key, nonce = Crypto.randomBytes(secretbox.nonceLength), getNonce) { | ||
static encryptUint8Array(messageUint8, key, nonce = Crypto.randomBytes(secretbox.nonceLength), getNonce, noEncode) { | ||
const keyUint8Array = Crypto.bs58.decode(key) | ||
const messageUint8 = decodeUTF8(message) | ||
const box = secretbox(messageUint8, nonce, keyUint8Array) | ||
const fullMessage = new Uint8Array(nonce.length + box.length) | ||
fullMessage.set(nonce) | ||
fullMessage.set(box, nonce.length) | ||
const encoded = Crypto.bs58.encode(Buffer.from(fullMessage)) | ||
const encoded = noEncode ? fullMessage : Crypto.bs58.encode(Buffer.from(fullMessage)) | ||
if (getNonce) { | ||
@@ -191,9 +187,16 @@ return [nonce, encoded] | ||
static decrypt(messageWithNonce, key) { | ||
static encrypt(message, key, nonce = Crypto.randomBytes(secretbox.nonceLength), getNonce, returnUint8Array) { | ||
return Crypto.encryptUint8Array(decodeUTF8(message), key, nonce, getNonce, returnUint8Array) | ||
} | ||
static encryptBuffer(buf, key, nonce = Crypto.randomBytes(secretbox.nonceLength), getNonce, returnUint8Array) { | ||
return Crypto.encryptUint8Array(new Uint8Array(buf), key, nonce, getNonce, returnUint8Array) | ||
} | ||
static decryptUint8Array(messageWithNonceAsUint8Array, key, returnUint8Array) { | ||
const keyUint8Array = Crypto.bs58.decode(key) | ||
const messageWithNonceAsUint8Array = Crypto.bs58.decode(messageWithNonce) | ||
const nonce = messageWithNonceAsUint8Array.slice(0, secretbox.nonceLength) | ||
const message = messageWithNonceAsUint8Array.slice( | ||
secretbox.nonceLength, | ||
messageWithNonce.length | ||
messageWithNonceAsUint8Array.length | ||
) | ||
@@ -204,5 +207,9 @@ const decrypted = secretbox.open(message, nonce, keyUint8Array) | ||
} | ||
return encodeUTF8(decrypted) | ||
return returnUint8Array ? decrypted : encodeUTF8(decrypted) | ||
} | ||
static decrypt(messageWithNonce, key, returnUint8Array) { | ||
return Crypto.decryptUint8Array(Crypto.bs58.decode(messageWithNonce), key, returnUint8Array) | ||
} | ||
static getNonceFromMessage(messageWithNonce) { | ||
@@ -209,0 +216,0 @@ const messageWithNonceAsUint8Array = Crypto.bs58.decode(messageWithNonce) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92542
275
46