@stacks/encryption
Advanced tools
Comparing version 4.0.2-beta.0 to 4.0.2-beta.1
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
export declare type CipherTextEncoding = 'hex' | 'base64'; | ||
@@ -22,4 +21,4 @@ export declare type CipherObject = { | ||
export declare function aes256CbcEncrypt(iv: Buffer, key: Buffer, plaintext: Buffer): Promise<Buffer>; | ||
export declare function getHexFromBN(bnInput: BN): string; | ||
export declare function getBufferFromBN(bnInput: BN): Buffer; | ||
export declare function getHexFromBN(bnInput: bigint): string; | ||
export declare function getBufferFromBN(bnInput: bigint): Buffer; | ||
export declare function getCipherObjectWrapper(opts: { | ||
@@ -26,0 +25,0 @@ wasString: boolean; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const sha256_1 = require("@noble/hashes/sha256"); | ||
const utils_1 = require("@noble/hashes/utils"); | ||
const secp256k1_1 = require("@noble/secp256k1"); | ||
@@ -14,3 +13,3 @@ const common_1 = require("@stacks/common"); | ||
const sha2Hash_1 = require("./sha2Hash"); | ||
const utils_2 = require("./utils"); | ||
const utils_1 = require("./utils"); | ||
secp256k1_1.utils.hmacSha256Sync = (key, ...msgs) => { | ||
@@ -95,3 +94,3 @@ const h = hmac_1.hmac.create(sha256_1.sha256, key); | ||
function getHexFromBN(bnInput) { | ||
const hexOut = bnInput.toString('hex', 64); | ||
const hexOut = bnInput.toString(16); | ||
if (hexOut.length === 64) { | ||
@@ -110,5 +109,5 @@ return hexOut; | ||
function getBufferFromBN(bnInput) { | ||
const result = bnInput.toArrayLike(common_1.Buffer, 'be', 32); | ||
const result = (0, common_1.toBuffer)(bnInput, 32); | ||
if (result.byteLength !== 32) { | ||
throw new Error('Failed to generate a 32-byte BN'); | ||
throw new Error('Failed to generate a 32-byte buffer instance'); | ||
} | ||
@@ -154,3 +153,3 @@ return result; | ||
const { payloadShell, payloadValuesLength } = getCipherObjectWrapper(opts); | ||
const cipherTextLength = (0, utils_2.getAesCbcOutputLength)(opts.contentLength); | ||
const cipherTextLength = (0, utils_1.getAesCbcOutputLength)(opts.contentLength); | ||
let encodedCipherTextLength; | ||
@@ -161,3 +160,3 @@ if (!opts.cipherTextEncoding || opts.cipherTextEncoding === 'hex') { | ||
else if (opts.cipherTextEncoding === 'base64') { | ||
encodedCipherTextLength = (0, utils_2.getBase64OutputLength)(cipherTextLength); | ||
encodedCipherTextLength = (0, utils_1.getBase64OutputLength)(cipherTextLength); | ||
} | ||
@@ -191,7 +190,7 @@ else { | ||
const cipherText = await aes256CbcEncrypt(common_1.Buffer.from(initializationVector), sharedKeys.encryptionKey, content); | ||
const macData = (0, utils_1.concatBytes)(initializationVector, ephemeralPublicKey, cipherText); | ||
const macData = (0, common_1.concatBytes)(initializationVector, ephemeralPublicKey, cipherText); | ||
const mac = await hmacSha256(sharedKeys.hmacKey, common_1.Buffer.from(macData)); | ||
let cipherTextString; | ||
if (!cipherTextEncoding || cipherTextEncoding === 'hex') { | ||
cipherTextString = secp256k1_1.utils.bytesToHex(cipherText); | ||
cipherTextString = (0, common_1.bytesToHex)(cipherText); | ||
} | ||
@@ -205,6 +204,6 @@ else if (cipherTextEncoding === 'base64') { | ||
const result = { | ||
iv: secp256k1_1.utils.bytesToHex(initializationVector), | ||
ephemeralPK: secp256k1_1.utils.bytesToHex(ephemeralPublicKey), | ||
iv: (0, common_1.bytesToHex)(initializationVector), | ||
ephemeralPK: (0, common_1.bytesToHex)(ephemeralPublicKey), | ||
cipherText: cipherTextString, | ||
mac: secp256k1_1.utils.bytesToHex(mac), | ||
mac: (0, common_1.bytesToHex)(mac), | ||
wasString, | ||
@@ -227,3 +226,3 @@ }; | ||
const sharedKeys = sharedSecretToKeys(common_1.Buffer.from(sharedSecret)); | ||
const ivBuffer = (0, utils_1.hexToBytes)(cipherObject.iv); | ||
const ivBuffer = (0, common_1.hexToBytes)(cipherObject.iv); | ||
let cipherTextBuffer; | ||
@@ -239,5 +238,5 @@ if (!cipherObject.cipherTextEncoding || cipherObject.cipherTextEncoding === 'hex') { | ||
} | ||
const macData = (0, utils_1.concatBytes)(ivBuffer, (0, utils_1.hexToBytes)(ephemeralPK), cipherTextBuffer); | ||
const macData = (0, common_1.concatBytes)(ivBuffer, (0, common_1.hexToBytes)(ephemeralPK), cipherTextBuffer); | ||
const actualMac = await hmacSha256(sharedKeys.hmacKey, common_1.Buffer.from(macData)); | ||
const expectedMac = (0, utils_1.hexToBytes)(cipherObject.mac); | ||
const expectedMac = (0, common_1.hexToBytes)(cipherObject.mac); | ||
if (!equalConstTime(common_1.Buffer.from(expectedMac), actualMac)) { | ||
@@ -261,3 +260,3 @@ throw new common_1.FailedDecryptionError('Decryption failed: failure in MAC check'); | ||
return { | ||
signature: secp256k1_1.utils.bytesToHex(signature), | ||
signature: (0, common_1.bytesToHex)(signature), | ||
publicKey, | ||
@@ -264,0 +263,0 @@ }; |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
export declare type CipherTextEncoding = 'hex' | 'base64'; | ||
@@ -22,4 +21,4 @@ export declare type CipherObject = { | ||
export declare function aes256CbcEncrypt(iv: Buffer, key: Buffer, plaintext: Buffer): Promise<Buffer>; | ||
export declare function getHexFromBN(bnInput: BN): string; | ||
export declare function getBufferFromBN(bnInput: BN): Buffer; | ||
export declare function getHexFromBN(bnInput: bigint): string; | ||
export declare function getBufferFromBN(bnInput: bigint): Buffer; | ||
export declare function getCipherObjectWrapper(opts: { | ||
@@ -26,0 +25,0 @@ wasString: boolean; |
import { hmac } from '@noble/hashes/hmac'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { concatBytes, hexToBytes } from '@noble/hashes/utils'; | ||
import { getPublicKey, getSharedSecret, Point, signSync, utils, verify } from '@noble/secp256k1'; | ||
import { Buffer, FailedDecryptionError } from '@stacks/common'; | ||
import { Buffer, toBuffer, FailedDecryptionError, concatBytes, hexToBytes, bytesToHex, } from '@stacks/common'; | ||
import { createCipher } from './aesCipher'; | ||
@@ -89,3 +88,3 @@ import { createHmacSha256 } from './hmacSha256'; | ||
export function getHexFromBN(bnInput) { | ||
const hexOut = bnInput.toString('hex', 64); | ||
const hexOut = bnInput.toString(16); | ||
if (hexOut.length === 64) { | ||
@@ -103,5 +102,5 @@ return hexOut; | ||
export function getBufferFromBN(bnInput) { | ||
const result = bnInput.toArrayLike(Buffer, 'be', 32); | ||
const result = toBuffer(bnInput, 32); | ||
if (result.byteLength !== 32) { | ||
throw new Error('Failed to generate a 32-byte BN'); | ||
throw new Error('Failed to generate a 32-byte buffer instance'); | ||
} | ||
@@ -182,3 +181,3 @@ return result; | ||
if (!cipherTextEncoding || cipherTextEncoding === 'hex') { | ||
cipherTextString = utils.bytesToHex(cipherText); | ||
cipherTextString = bytesToHex(cipherText); | ||
} | ||
@@ -192,6 +191,6 @@ else if (cipherTextEncoding === 'base64') { | ||
const result = { | ||
iv: utils.bytesToHex(initializationVector), | ||
ephemeralPK: utils.bytesToHex(ephemeralPublicKey), | ||
iv: bytesToHex(initializationVector), | ||
ephemeralPK: bytesToHex(ephemeralPublicKey), | ||
cipherText: cipherTextString, | ||
mac: utils.bytesToHex(mac), | ||
mac: bytesToHex(mac), | ||
wasString, | ||
@@ -244,3 +243,3 @@ }; | ||
return { | ||
signature: utils.bytesToHex(signature), | ||
signature: bytesToHex(signature), | ||
publicKey, | ||
@@ -247,0 +246,0 @@ }; |
{ | ||
"name": "@stacks/encryption", | ||
"version": "4.0.2-beta.0", | ||
"version": "4.0.2-beta.1", | ||
"description": "Encryption utilities for Stacks", | ||
@@ -38,6 +38,4 @@ "author": "yknl <yukanliao@gmail.com>", | ||
"@scure/bip39": "^1.0.0", | ||
"@stacks/common": "^4.0.1", | ||
"@types/bn.js": "^4.11.6", | ||
"@stacks/common": "^4.0.2-beta.1", | ||
"@types/node": "^14.14.43", | ||
"bn.js": "^5.2.0", | ||
"bs58": "^5.0.0", | ||
@@ -81,3 +79,3 @@ "ripemd160-min": "^0.0.6", | ||
"unpkg": "dist/umd/index.js", | ||
"gitHead": "f578a0d9f8e7afe9836c2ad48acd34951e30cf7d" | ||
"gitHead": "0f65e5b363986e830faaf8050c9b3c7aa30e4924" | ||
} |
import { hmac } from '@noble/hashes/hmac'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { concatBytes, hexToBytes } from '@noble/hashes/utils'; | ||
import { getPublicKey, getSharedSecret, Point, signSync, utils, verify } from '@noble/secp256k1'; | ||
import { Buffer, FailedDecryptionError } from '@stacks/common'; | ||
import * as BN from 'bn.js'; | ||
import { | ||
Buffer, | ||
toBuffer, | ||
FailedDecryptionError, | ||
concatBytes, | ||
hexToBytes, | ||
bytesToHex, | ||
} from '@stacks/common'; | ||
import { createCipher } from './aesCipher'; | ||
@@ -182,8 +187,8 @@ import { createHmacSha256 } from './hmacSha256'; | ||
/** | ||
* Hex encodes a 32-byte BN.js instance. | ||
* Hex encodes a 32-byte bigint instance. | ||
* The result string is zero padded and always 64 characters in length. | ||
* @ignore | ||
*/ | ||
export function getHexFromBN(bnInput: BN): string { | ||
const hexOut = bnInput.toString('hex', 64); | ||
export function getHexFromBN(bnInput: bigint): string { | ||
const hexOut = bnInput.toString(16); | ||
if (hexOut.length === 64) { | ||
@@ -202,10 +207,10 @@ return hexOut; | ||
/** | ||
* Returns a big-endian encoded 32-byte BN.js instance. | ||
* Returns a big-endian encoded 32-byte buffer instance. | ||
* The result Buffer is zero padded and always 32 bytes in length. | ||
* @ignore | ||
*/ | ||
export function getBufferFromBN(bnInput: BN): Buffer { | ||
const result = bnInput.toArrayLike(Buffer, 'be', 32); | ||
export function getBufferFromBN(bnInput: bigint): Buffer { | ||
const result = toBuffer(bnInput, 32); | ||
if (result.byteLength !== 32) { | ||
throw new Error('Failed to generate a 32-byte BN'); | ||
throw new Error('Failed to generate a 32-byte buffer instance'); | ||
} | ||
@@ -367,3 +372,3 @@ return result; | ||
if (!cipherTextEncoding || cipherTextEncoding === 'hex') { | ||
cipherTextString = utils.bytesToHex(cipherText); | ||
cipherTextString = bytesToHex(cipherText); | ||
} else if (cipherTextEncoding === 'base64') { | ||
@@ -376,6 +381,6 @@ cipherTextString = cipherText.toString('base64'); | ||
const result: CipherObject = { | ||
iv: utils.bytesToHex(initializationVector), | ||
ephemeralPK: utils.bytesToHex(ephemeralPublicKey), | ||
iv: bytesToHex(initializationVector), | ||
ephemeralPK: bytesToHex(ephemeralPublicKey), | ||
cipherText: cipherTextString, | ||
mac: utils.bytesToHex(mac), | ||
mac: bytesToHex(mac), | ||
wasString, | ||
@@ -472,3 +477,3 @@ }; | ||
return { | ||
signature: utils.bytesToHex(signature), | ||
signature: bytesToHex(signature), | ||
publicKey, | ||
@@ -475,0 +480,0 @@ }; |
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 too big to display
Sorry, the diff of this file is too big to display
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
3940497
9
8957
- Removed@types/bn.js@^4.11.6
- Removedbn.js@^5.2.0
- Removed@types/bn.js@4.11.6(transitive)
- Removedbn.js@5.2.1(transitive)
Updated@stacks/common@^4.0.2-beta.1