@burstjs/crypto
Advanced tools
Comparing version 0.1.0-rc.2 to 0.1.0-rc.3
@@ -13,3 +13,2 @@ export declare class Converter { | ||
static convertByteArrayToSignedInt32(bytes: any, opt_startIndex: any): any; | ||
static convertByteArrayToBigInteger(bytes: any, opt_startIndex: any): any; | ||
static convertByteArrayToWordArray(ba: any): any; | ||
@@ -31,3 +30,4 @@ static convertWordToByteArray(word: any, length: any): any[]; | ||
static int32ToBytes(x: any, opt_bigEndian: any): any[]; | ||
static convertTimestampToDate(timestamp: number): Date; | ||
static convertStringToBase64(text: string): string; | ||
static convertBase64ToString(base64: string): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const BN = require("bn.js"); | ||
const CryptoJS = require("crypto-js"); | ||
@@ -70,13 +69,2 @@ class Converter { | ||
} | ||
static convertByteArrayToBigInteger(bytes, opt_startIndex) { | ||
let index = this.checkBytesToIntInput(bytes, 8, opt_startIndex); | ||
let value = new BN("0", 10); | ||
let temp1, temp2; | ||
for (let i = 7; i >= 0; i--) { | ||
temp1 = value.multiply(new BN("256", 10)); | ||
temp2 = temp1.add(new BN(bytes[opt_startIndex + i].toString(10), 10)); | ||
value = temp2; | ||
} | ||
return value; | ||
} | ||
static convertByteArrayToWordArray(ba) { | ||
@@ -225,5 +213,18 @@ var wa = [], i; | ||
} | ||
static convertTimestampToDate(timestamp) { | ||
return new Date(Date.UTC(2014, 7, 11, 2, 0, 0, 0) + timestamp * 1000); | ||
static convertStringToBase64(text) { | ||
if (global && global.Buffer) { | ||
return new Buffer(text).toString('base64'); | ||
} | ||
else { | ||
return btoa(text); | ||
} | ||
} | ||
static convertBase64ToString(base64) { | ||
if (global && global.Buffer) { | ||
return new Buffer(base64, 'base64').toString(); | ||
} | ||
else { | ||
return atob(base64); | ||
} | ||
} | ||
} | ||
@@ -230,0 +231,0 @@ Converter.charToNibble = {}; |
@@ -6,4 +6,4 @@ "use strict"; | ||
const decrypted = CryptoJS.AES.decrypt(encryptedBase64, key); | ||
return decrypted.toString(CryptoJS.enc.Utf8); | ||
return decrypted && decrypted.toString(CryptoJS.enc.Utf8); | ||
}; | ||
//# sourceMappingURL=decryptAES.js.map |
@@ -1,1 +0,2 @@ | ||
export declare const decryptMessage: (encryptedMessage: string, nonce: string, encryptedPrivateKey: string, pinHash: string, senderPublicKey: string) => string; | ||
import { EncryptedMessage } from '../typings/encryptedMessage'; | ||
export declare function decryptMessage(encryptedMessage: EncryptedMessage, senderPublicKey: string, recipientPrivateKey: string): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const decryptAES_1 = require("./decryptAES"); | ||
const ec_kcdsa_1 = require("./ec-kcdsa"); | ||
const converter_1 = require("./converter"); | ||
const CryptoJS = require("crypto-js"); | ||
exports.decryptMessage = (encryptedMessage, nonce, encryptedPrivateKey, pinHash, senderPublicKey) => { | ||
const privateKey = decryptAES_1.decryptAES(encryptedPrivateKey, pinHash); | ||
let sharedKey = ec_kcdsa_1.ECKCDSA.sharedkey(converter_1.Converter.convertHexStringToByteArray(privateKey), converter_1.Converter.convertHexStringToByteArray(senderPublicKey)); | ||
let nonce_array = converter_1.Converter.convertWordArrayToUint8Array(CryptoJS.enc.Hex.parse(nonce)); | ||
for (let i = 0; i < 32; i++) { | ||
sharedKey[i] ^= nonce_array[i]; | ||
function decryptMessage(encryptedMessage, senderPublicKey, recipientPrivateKey) { | ||
const sharedKey = ec_kcdsa_1.ECKCDSA.sharedkey(converter_1.Converter.convertHexStringToByteArray(recipientPrivateKey), converter_1.Converter.convertHexStringToByteArray(senderPublicKey)); | ||
const SHARED_KEY_SIZE = sharedKey.length; | ||
const nonceArray = converter_1.Converter.convertWordArrayToUint8Array(CryptoJS.enc.Hex.parse(encryptedMessage.nonce)); | ||
for (let i = 0; i < SHARED_KEY_SIZE; i++) { | ||
sharedKey[i] ^= nonceArray[i]; | ||
} | ||
let key = CryptoJS.SHA256(converter_1.Converter.convertByteArrayToWordArray(sharedKey)); | ||
let messageB64 = CryptoJS.enc.Hex.parse(encryptedMessage).toString(CryptoJS.enc.Base64); | ||
let message = CryptoJS.AES.decrypt(messageB64, key.toString()).toString(CryptoJS.enc.Utf8); | ||
return message; | ||
}; | ||
const aeskey = converter_1.Converter.convertByteArrayToHexString(sharedKey); | ||
const tokens = encryptedMessage.data.split(':'); | ||
if (tokens.length !== 2) { | ||
throw new Error('Invalid message format'); | ||
} | ||
return CryptoJS.AES.decrypt(tokens[1], aeskey, { iv: converter_1.Converter.convertBase64ToString(tokens[0]) }).toString(CryptoJS.enc.Utf8); | ||
} | ||
exports.decryptMessage = decryptMessage; | ||
//# sourceMappingURL=decryptMessage.js.map |
@@ -1,1 +0,2 @@ | ||
export declare const encryptMessage: (message: string, encryptedPrivateKey: string, pinHash: string, recipientPublicKey: string) => string; | ||
import { EncryptedMessage } from '../typings/encryptedMessage'; | ||
export declare function encryptMessage(message: string, recipientPublicKey: string, senderPrivateKey: string): EncryptedMessage; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const decryptAES_1 = require("./decryptAES"); | ||
const CryptoJS = require("crypto-js"); | ||
const ec_kcdsa_1 = require("./ec-kcdsa"); | ||
const converter_1 = require("./converter"); | ||
exports.encryptMessage = (message, encryptedPrivateKey, pinHash, recipientPublicKey) => { | ||
const privateKey = decryptAES_1.decryptAES(encryptedPrivateKey, pinHash); | ||
let sharedKey = ec_kcdsa_1.ECKCDSA.sharedkey(converter_1.Converter.convertHexStringToByteArray(privateKey), converter_1.Converter.convertHexStringToByteArray(recipientPublicKey)); | ||
let random_bytes = CryptoJS.lib.WordArray.random(32); | ||
let r_nonce = converter_1.Converter.convertWordArrayToUint8Array(random_bytes); | ||
for (let i = 0; i < 32; i++) { | ||
sharedKey[i] ^= r_nonce[i]; | ||
function encryptMessage(message, recipientPublicKey, senderPrivateKey) { | ||
const sharedKey = ec_kcdsa_1.ECKCDSA.sharedkey(converter_1.Converter.convertHexStringToByteArray(senderPrivateKey), converter_1.Converter.convertHexStringToByteArray(recipientPublicKey)); | ||
const SHARED_KEY_SIZE = sharedKey.length; | ||
const randomBytes = CryptoJS.lib.WordArray.random(SHARED_KEY_SIZE); | ||
const randomNonce = converter_1.Converter.convertWordArrayToUint8Array(randomBytes); | ||
for (let i = 0; i < SHARED_KEY_SIZE; i++) { | ||
sharedKey[i] ^= randomNonce[i]; | ||
} | ||
let key = CryptoJS.SHA256(converter_1.Converter.convertByteArrayToWordArray(sharedKey)); | ||
let iv = CryptoJS.lib.WordArray.random(16); | ||
let messageB64 = CryptoJS.AES.encrypt(message, key.toString(), { iv: iv }).toString(); | ||
return "ohagers fix needed here"; | ||
}; | ||
const nonceHex = randomBytes.toString(); | ||
const aeskey = converter_1.Converter.convertByteArrayToHexString(sharedKey); | ||
const iv = CryptoJS.lib.WordArray.random(16); | ||
const encryptedTextBase64 = CryptoJS.AES.encrypt(message, aeskey, { iv }).toString(); | ||
return { | ||
data: `${iv.toString(CryptoJS.enc.Base64)}:${encryptedTextBase64}`, | ||
nonce: nonceHex, | ||
isText: true | ||
}; | ||
} | ||
exports.encryptMessage = encryptMessage; | ||
//# sourceMappingURL=encryptMessage.js.map |
@@ -5,12 +5,10 @@ "use strict"; | ||
const CryptoJS = require("crypto-js"); | ||
const BN = require("bn.js"); | ||
const big_js_1 = require("big.js"); | ||
exports.getAccountIdFromPublicKey = (publicKey) => { | ||
const hash = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(publicKey)); | ||
const bytes = converter_1.Converter.convertWordArrayToByteArray(hash); | ||
let slice = bytes.slice(0, 8); | ||
slice = slice.reverse(); | ||
const numbers = slice.map((byte) => byte.toString(10)); | ||
const id = new BN(numbers, 256); | ||
return id.toString(); | ||
const slice = bytes.slice(0, 8); | ||
const result = slice.reduce((acc, num, index) => acc.add(big_js_1.Big(num).mul(big_js_1.Big(2).pow(index * 8))), big_js_1.Big(0)); | ||
return result.toFixed(); | ||
}; | ||
//# sourceMappingURL=getAccountIdFromPublicKey.js.map |
@@ -12,1 +12,2 @@ export * from './converter'; | ||
export * from '../typings/keys'; | ||
export * from '../typings/encryptedMessage'; |
{ | ||
"name": "@burstjs/crypto", | ||
"version": "0.1.0-rc.2", | ||
"version": "0.1.0-rc.3", | ||
"description": "Cryptographic functions for building Burstcoin apps.", | ||
@@ -43,6 +43,7 @@ "contributors": [ | ||
"dependencies": { | ||
"@types/big.js": "^4.0.5", | ||
"@types/crypto-js": "^3.1.43", | ||
"@types/node": "^10.12.18", | ||
"@types/seedrandom": "^2.4.27", | ||
"bn.js": "^4.11.8", | ||
"big.js": "^5.2.2", | ||
"crypto-js": "^3.1.9-1", | ||
@@ -56,3 +57,3 @@ "seedrandom": "^2.4.3" | ||
}, | ||
"gitHead": "8080c7a2fef903055d44793fe8ed5d1a864ff398", | ||
"gitHead": "049583d0dfbf99ba6328e2291d459e8f377c5625", | ||
"publishConfig": { | ||
@@ -59,0 +60,0 @@ "access": "public" |
@@ -7,9 +7,7 @@ import {generateMasterKeys} from '../generateMasterKeys'; | ||
it('getAccountIdFromPublicKey successfully', () => { | ||
const keys = generateMasterKeys('weather state leave always grace arrow eventually loss fact ring desperate hey'); | ||
const accountId = getAccountIdFromPublicKey(keys.publicKey); | ||
expect(accountId).toBe('6502115112683865257'); | ||
}); | ||
}); |
@@ -10,3 +10,2 @@ /* tslint:disable */ | ||
import * as BN from 'bn.js'; | ||
import * as CryptoJS from 'crypto-js'; | ||
@@ -103,18 +102,2 @@ | ||
public static convertByteArrayToBigInteger(bytes, opt_startIndex) { | ||
let index = this.checkBytesToIntInput(bytes, 8, opt_startIndex); | ||
let value = new BN("0", 10); | ||
let temp1, temp2; | ||
for (let i = 7; i >= 0; i--) { | ||
temp1 = value.multiply(new BN("256", 10)); | ||
temp2 = temp1.add(new BN(bytes[opt_startIndex + i].toString(10), 10)); | ||
value = temp2; | ||
} | ||
return value; | ||
} | ||
public static convertByteArrayToWordArray(ba) { | ||
@@ -310,7 +293,31 @@ var wa = [], | ||
/** | ||
* @deprecated Use @burstjs/util/convertBurstTimeToEpochTime | ||
* Converts a string to base64 | ||
* @param text The string to be converted | ||
* @return the converted base64 string | ||
*/ | ||
public static convertTimestampToDate(timestamp: number) { | ||
return new Date(Date.UTC(2014, 7, 11, 2, 0, 0, 0) + timestamp * 1000); | ||
public static convertStringToBase64(text: string) : string{ | ||
if (global && global.Buffer) { | ||
return new Buffer(text).toString('base64'); | ||
} | ||
else { | ||
// @ts-ignore | ||
return btoa(text); | ||
} | ||
} | ||
/** | ||
* Converts a base64 string to clear text | ||
* @param base64 The base64 string to be converted | ||
* @return the clear text string | ||
*/ | ||
public static convertBase64ToString(base64: string): string{ | ||
if (global && global.Buffer) { | ||
return new Buffer(base64, 'base64').toString(); | ||
} | ||
else { | ||
// @ts-ignore | ||
return atob(base64) | ||
} | ||
} | ||
} |
@@ -13,3 +13,3 @@ /** @module crypto */ | ||
const decrypted = CryptoJS.AES.decrypt(encryptedBase64, key); | ||
return decrypted.toString(CryptoJS.enc.Utf8); | ||
return decrypted && decrypted.toString(CryptoJS.enc.Utf8); | ||
}; |
@@ -1,43 +0,49 @@ | ||
/* tslint:disable */ | ||
// tslint:disable:no-bitwise | ||
/** @ignore */ | ||
/** @module crypto */ | ||
// TODO: remove ignore whene fixed | ||
/** @ignore */ | ||
import {decryptAES} from "./decryptAES"; | ||
import {ECKCDSA} from "./ec-kcdsa"; | ||
import {Converter} from "./converter"; | ||
import * as CryptoJS from "crypto-js"; | ||
// FIXME: This implementation is not compatible with current BRS encryption | ||
/** | ||
* Decrypt a message attached to transaction | ||
* FIXME: Fix en/decryption! | ||
* Original work Copyright (c) 2018 PoC-Consortium | ||
* Modified work Copyright (c) 2019 Burst Apps Team | ||
*/ | ||
export const decryptMessage = ( | ||
encryptedMessage: string, | ||
nonce: string, | ||
encryptedPrivateKey: string, | ||
pinHash: string, | ||
senderPublicKey: string | ||
): string => { | ||
const privateKey = decryptAES(encryptedPrivateKey, pinHash); | ||
// generate shared key | ||
let sharedKey = | ||
import {ECKCDSA} from './ec-kcdsa'; | ||
import {Converter} from './converter'; | ||
import * as CryptoJS from 'crypto-js'; | ||
import {EncryptedMessage} from '../typings/encryptedMessage'; | ||
/** | ||
* Decrypts an encrypted Message | ||
* @param encryptedMessage The encrypted message | ||
* @param senderPublicKey The senders public key | ||
* @param recipientPrivateKey The recipients private (agreement) key | ||
* @return The original message | ||
*/ | ||
export function decryptMessage(encryptedMessage: EncryptedMessage, senderPublicKey: string, recipientPrivateKey: string): string { | ||
const sharedKey = | ||
ECKCDSA.sharedkey( | ||
Converter.convertHexStringToByteArray(privateKey), | ||
Converter.convertHexStringToByteArray(recipientPrivateKey), | ||
Converter.convertHexStringToByteArray(senderPublicKey) | ||
); | ||
// convert nonce to uint8array | ||
let nonce_array = Converter.convertWordArrayToUint8Array(CryptoJS.enc.Hex.parse(nonce)); | ||
// combine | ||
for (let i = 0; i < 32; i++) { | ||
sharedKey[i] ^= nonce_array[i]; | ||
const SHARED_KEY_SIZE = sharedKey.length; | ||
const nonceArray = Converter.convertWordArrayToUint8Array(CryptoJS.enc.Hex.parse(encryptedMessage.nonce)); | ||
for (let i = 0; i < SHARED_KEY_SIZE; i++) { | ||
sharedKey[i] ^= nonceArray[i]; | ||
} | ||
// hash shared key | ||
let key = CryptoJS.SHA256(Converter.convertByteArrayToWordArray(sharedKey)) | ||
// convert message hex back to base 64 due to limitation of node | ||
let messageB64 = CryptoJS.enc.Hex.parse(encryptedMessage).toString(CryptoJS.enc.Base64); | ||
// decrypt it | ||
let message = CryptoJS.AES.decrypt(messageB64, key.toString()).toString(CryptoJS.enc.Utf8); | ||
// return decrypted message | ||
return message; | ||
const aeskey = Converter.convertByteArrayToHexString(sharedKey); | ||
const tokens = encryptedMessage.data.split(':'); | ||
if (tokens.length !== 2) { | ||
throw new Error('Invalid message format'); | ||
} | ||
return CryptoJS.AES.decrypt( | ||
tokens[1], | ||
aeskey, | ||
{iv: Converter.convertBase64ToString(tokens[0])} | ||
).toString(CryptoJS.enc.Utf8); | ||
} |
@@ -61,3 +61,3 @@ /** @ignore */ | ||
let w, i; | ||
let h1 = new Array(32) | ||
let h1 = new Array(32); | ||
let x1 = new Array(32); | ||
@@ -64,0 +64,0 @@ let tmp1 = new Array(64); |
@@ -1,43 +0,45 @@ | ||
/* tslint:disable */ | ||
// tslint:disable:no-bitwise | ||
/** @ignore */ | ||
/** @module crypto */ | ||
// TODO: remove ignore whene fixed | ||
/** @ignore */ | ||
import { decryptAES } from "./decryptAES"; | ||
import * as CryptoJS from "crypto-js"; | ||
import { ECKCDSA } from "./ec-kcdsa"; | ||
import { Converter } from "./converter"; | ||
// FIXME: This implementation is not compatible with current BRS encryption | ||
/* | ||
* Encrypt a message attached to a transaction | ||
* FIXME: Fix en/decryption! | ||
*/ | ||
export const encryptMessage = (message: string, encryptedPrivateKey: string, pinHash: string, recipientPublicKey: string): string => { | ||
const privateKey = decryptAES(encryptedPrivateKey, pinHash) | ||
// generate shared key | ||
let sharedKey = | ||
import * as CryptoJS from 'crypto-js'; | ||
import {ECKCDSA} from './ec-kcdsa'; | ||
import {Converter} from './converter'; | ||
import {EncryptedMessage} from '../typings/encryptedMessage'; | ||
/** | ||
* Encrypts a message | ||
* @param message Message to be encrypted | ||
* @param recipientPublicKey The recipients public key | ||
* @param senderPrivateKey The senders private (agreement) key | ||
* @return The encrypted Message | ||
*/ | ||
export function encryptMessage(message: string, recipientPublicKey: string, senderPrivateKey: string): EncryptedMessage { | ||
const sharedKey = | ||
ECKCDSA.sharedkey( | ||
Converter.convertHexStringToByteArray(privateKey), | ||
Converter.convertHexStringToByteArray(senderPrivateKey), | ||
Converter.convertHexStringToByteArray(recipientPublicKey) | ||
); | ||
// Create random nonce | ||
let random_bytes = CryptoJS.lib.WordArray.random(32); | ||
let r_nonce = Converter.convertWordArrayToUint8Array(random_bytes); | ||
// combine | ||
for (let i = 0; i < 32; i++) { | ||
sharedKey[i] ^= r_nonce[i]; | ||
const SHARED_KEY_SIZE = sharedKey.length; | ||
const randomBytes = CryptoJS.lib.WordArray.random(SHARED_KEY_SIZE); | ||
const randomNonce = Converter.convertWordArrayToUint8Array(randomBytes); | ||
for (let i = 0; i < SHARED_KEY_SIZE; i++) { | ||
sharedKey[i] ^= randomNonce[i]; | ||
} | ||
// hash shared key | ||
let key = CryptoJS.SHA256(Converter.convertByteArrayToWordArray(sharedKey)); | ||
// ENCRYPT | ||
let iv = CryptoJS.lib.WordArray.random(16); | ||
let messageB64 = CryptoJS.AES.encrypt(message, key.toString(), {iv: iv}).toString(); | ||
// convert base 64 to hex due to node limitation | ||
// todo: ohagers fix | ||
// let messageHex = iv.toString(CryptoJS.enc.Hex) + CryptoJS.enc.Base64.parse(messageB64).toString(CryptoJS.enc.Hex); | ||
// Uint 8 to hex | ||
// let nonce = random_bytes.toString(CryptoJS.enc.Hex); | ||
// return encrypted pair | ||
// resolve({ m: messageHex, n: nonce }) | ||
return "ohagers fix needed here"; | ||
const nonceHex = randomBytes.toString(); | ||
const aeskey = Converter.convertByteArrayToHexString(sharedKey); | ||
const iv = CryptoJS.lib.WordArray.random(16); | ||
const encryptedTextBase64 = CryptoJS.AES.encrypt(message, aeskey, {iv}).toString(); | ||
return { | ||
// @ts-ignore | ||
data: `${iv.toString(CryptoJS.enc.Base64)}:${encryptedTextBase64}`, | ||
nonce: nonceHex, | ||
isText: true | ||
}; | ||
} |
/** @module crypto */ | ||
import { Converter } from './converter'; | ||
import {Converter} from './converter'; | ||
import * as CryptoJS from 'crypto-js'; | ||
import * as BN from 'bn.js'; | ||
import {Big} from 'big.js'; | ||
@@ -13,10 +13,11 @@ /** | ||
export const getAccountIdFromPublicKey = (publicKey: string): string => { | ||
// hash with SHA 256 | ||
const hash = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(publicKey)); | ||
const bytes = Converter.convertWordArrayToByteArray(hash); | ||
let slice = bytes.slice(0, 8); | ||
slice = slice.reverse(); | ||
const numbers = slice.map((byte: Number) => byte.toString(10)); | ||
const id = new BN(numbers, 256); // base 256 for byte | ||
return id.toString(); | ||
const slice = bytes.slice(0, 8); | ||
const result = slice.reduce( | ||
(acc, num, index) => acc.add(Big(num).mul(Big(2).pow(index * 8))), | ||
Big(0) | ||
); | ||
return result.toFixed(); | ||
}; |
/** @module crypto */ | ||
// export * from './crypto'; | ||
export * from './converter'; | ||
// export * from './curve25519'; | ||
// export * from './ec-kcdsa'; | ||
export * from './encryptAES'; | ||
@@ -18,1 +16,2 @@ export * from './decryptAES'; | ||
export * from '../typings/keys'; | ||
export * from '../typings/encryptedMessage'; |
/** @ignore */ | ||
/** @module crypto */ | ||
/* tslint:disable:max-line-length */ | ||
// taken from https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/english.txt | ||
// being prepared for hierarchical determenistic wallet | ||
// being prepared for hierarchical deterministic wallet | ||
export const words: string[] = [ | ||
@@ -8,0 +7,0 @@ 'abandon', |
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
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
Sorry, the diff of this file is not supported yet
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
271719
86
7043
7
+ Added@types/big.js@^4.0.5
+ Addedbig.js@^5.2.2
+ Added@types/big.js@4.0.5(transitive)
+ Addedbig.js@5.2.2(transitive)
- Removedbn.js@^4.11.8
- Removedbn.js@4.12.1(transitive)