jupiter-node-sdk
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -14,3 +14,3 @@ "use strict"; | ||
function Encryption(options) { | ||
const alg = 'aes-256-ctr'; | ||
const alg = "aes-256-cbc"; | ||
const sec = options.secret; | ||
@@ -24,9 +24,9 @@ return { | ||
const cipher = crypto_1.default.createCipheriv(this._algorithm, key, iv); | ||
const inputStr = input instanceof Buffer ? input.toString('base64') : input; | ||
let cipherText = cipher.update(inputStr, 'utf8', 'base64'); | ||
cipherText += cipher.final('base64'); | ||
return await this.parseData(`${cipherText}:${iv.toString('base64')}`); | ||
const inputStr = input instanceof Buffer ? input.toString("base64") : input; | ||
let cipherText = cipher.update(inputStr, "utf8", "base64"); | ||
cipherText += cipher.final("base64"); | ||
return await this.parseData(`${cipherText}:${iv.toString("base64")}`); | ||
}, | ||
async encryptFileUtf8(filePath) { | ||
const fileText = await readFile(filePath, { encoding: 'utf8' }); | ||
const fileText = await readFile(filePath, { encoding: "utf8" }); | ||
return await this.encrypt(fileText); | ||
@@ -36,13 +36,13 @@ }, | ||
const inflatedString = (await this.parseData(text, false)).toString(); | ||
const [rawBase64, ivBase64] = inflatedString.split(':'); | ||
const iv = Buffer.from(ivBase64, 'base64'); | ||
const [rawBase64, ivBase64] = inflatedString.split(":"); | ||
const iv = Buffer.from(ivBase64, "base64"); | ||
const secret = getFilledSecret(this._secret); | ||
const { key } = getKeyAndIV(secret, iv); | ||
const decipher = crypto_1.default.createDecipheriv(this._algorithm, key, iv); | ||
let dec = decipher.update(rawBase64, 'base64', 'utf8'); | ||
dec += decipher.final('utf8'); | ||
let dec = decipher.update(rawBase64, "base64", "utf8"); | ||
dec += decipher.final("utf8"); | ||
return dec; | ||
}, | ||
async decryptFileUtf8(filePath) { | ||
const fileText = await readFile(filePath, { encoding: 'utf8' }); | ||
const fileText = await readFile(filePath, { encoding: "utf8" }); | ||
return await this.decrypt(fileText); | ||
@@ -52,7 +52,7 @@ }, | ||
return await new Promise((resolve, reject) => { | ||
const sha256Sum = crypto_1.default.createHash('sha256'); | ||
const sha256Sum = crypto_1.default.createHash("sha256"); | ||
const s = fs_1.default.createReadStream(filePath); | ||
s.on('data', (data) => sha256Sum.update(data)); | ||
s.on('error', reject); | ||
s.on('end', () => resolve(sha256Sum.digest('base64'))); | ||
s.on("data", (data) => sha256Sum.update(data)); | ||
s.on("error", reject); | ||
s.on("end", () => resolve(sha256Sum.digest("base64"))); | ||
}); | ||
@@ -67,7 +67,7 @@ }, | ||
case false: | ||
return await inflate(Buffer.from(value, 'base64')); | ||
return await inflate(Buffer.from(value, "base64")); | ||
default: | ||
//true | ||
const compressedValue = await deflate(value); | ||
return Buffer.from(compressedValue).toString('base64'); | ||
return Buffer.from(compressedValue).toString("base64"); | ||
} | ||
@@ -80,9 +80,9 @@ }, | ||
function getFilledSecret(secret) { | ||
const sha256Sum = crypto_1.default.createHash('sha256'); | ||
const sha256Sum = crypto_1.default.createHash("sha256"); | ||
sha256Sum.update(secret); | ||
return sha256Sum.digest('base64'); | ||
return sha256Sum.digest("base64"); | ||
} | ||
function getKeyAndIV(key, iv) { | ||
const ivBuffer = iv || crypto_1.default.randomBytes(16); | ||
const derivedKey = crypto_1.default.pbkdf2Sync(key, ivBuffer, 1e5, 32, 'sha256'); | ||
const derivedKey = crypto_1.default.pbkdf2Sync(key, ivBuffer, 1e5, 32, "sha256"); | ||
return { | ||
@@ -89,0 +89,0 @@ iv: ivBuffer, |
{ | ||
"name": "jupiter-node-sdk", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "A thin wrapper around the Jupiter blockchain APIs.", | ||
@@ -8,3 +8,3 @@ "main": "./dist/index.js", | ||
"type": "git", | ||
"url": "https://github.com/whatl3y/jupiter-node-sdk" | ||
"url": "https://github.com/moontography/jupiter-node-sdk" | ||
}, | ||
@@ -11,0 +11,0 @@ "scripts": { |
@@ -1,17 +0,17 @@ | ||
import crypto from 'crypto' | ||
import fs from 'fs' | ||
import { promisify } from 'util' | ||
import zlib from 'zlib' | ||
import crypto from "crypto"; | ||
import fs from "fs"; | ||
import { promisify } from "util"; | ||
import zlib from "zlib"; | ||
const readFile = fs.promises.readFile | ||
const inflate: any = promisify(zlib.inflate) | ||
const deflate: any = promisify(zlib.deflate) | ||
const readFile = fs.promises.readFile; | ||
const inflate: any = promisify(zlib.inflate); | ||
const deflate: any = promisify(zlib.deflate); | ||
interface IEncryptionOptions { | ||
secret: string | ||
secret: string; | ||
} | ||
export default function Encryption(options: IEncryptionOptions) { | ||
const alg = 'aes-256-ctr' | ||
const sec = options.secret | ||
const alg = "aes-256-cbc"; | ||
const sec = options.secret; | ||
@@ -23,34 +23,34 @@ return { | ||
async encrypt(input: Buffer | string) { | ||
const secret = getFilledSecret(this._secret) | ||
const { iv, key } = getKeyAndIV(secret) | ||
const cipher = crypto.createCipheriv(this._algorithm, key, iv) | ||
const secret = getFilledSecret(this._secret); | ||
const { iv, key } = getKeyAndIV(secret); | ||
const cipher = crypto.createCipheriv(this._algorithm, key, iv); | ||
const inputStr = | ||
input instanceof Buffer ? input.toString('base64') : input | ||
let cipherText = cipher.update(inputStr, 'utf8', 'base64') | ||
cipherText += cipher.final('base64') | ||
return await this.parseData(`${cipherText}:${iv.toString('base64')}`) | ||
input instanceof Buffer ? input.toString("base64") : input; | ||
let cipherText = cipher.update(inputStr, "utf8", "base64"); | ||
cipherText += cipher.final("base64"); | ||
return await this.parseData(`${cipherText}:${iv.toString("base64")}`); | ||
}, | ||
async encryptFileUtf8(filePath: string) { | ||
const fileText = await readFile(filePath, { encoding: 'utf8' }) | ||
return await this.encrypt(fileText) | ||
const fileText = await readFile(filePath, { encoding: "utf8" }); | ||
return await this.encrypt(fileText); | ||
}, | ||
async decrypt(text: string) { | ||
const inflatedString = (await this.parseData(text, false)).toString() | ||
const [rawBase64, ivBase64] = inflatedString.split(':') | ||
const iv = Buffer.from(ivBase64, 'base64') | ||
const secret = getFilledSecret(this._secret) | ||
const { key } = getKeyAndIV(secret, iv) | ||
const decipher = crypto.createDecipheriv(this._algorithm, key, iv) | ||
const inflatedString = (await this.parseData(text, false)).toString(); | ||
const [rawBase64, ivBase64] = inflatedString.split(":"); | ||
const iv = Buffer.from(ivBase64, "base64"); | ||
const secret = getFilledSecret(this._secret); | ||
const { key } = getKeyAndIV(secret, iv); | ||
const decipher = crypto.createDecipheriv(this._algorithm, key, iv); | ||
let dec = decipher.update(rawBase64, 'base64', 'utf8') | ||
dec += decipher.final('utf8') | ||
return dec | ||
let dec = decipher.update(rawBase64, "base64", "utf8"); | ||
dec += decipher.final("utf8"); | ||
return dec; | ||
}, | ||
async decryptFileUtf8(filePath: string) { | ||
const fileText = await readFile(filePath, { encoding: 'utf8' }) | ||
return await this.decrypt(fileText) | ||
const fileText = await readFile(filePath, { encoding: "utf8" }); | ||
return await this.decrypt(fileText); | ||
}, | ||
@@ -60,9 +60,9 @@ | ||
return await new Promise((resolve, reject) => { | ||
const sha256Sum = crypto.createHash('sha256') | ||
const sha256Sum = crypto.createHash("sha256"); | ||
const s = fs.createReadStream(filePath) | ||
s.on('data', (data) => sha256Sum.update(data)) | ||
s.on('error', reject) | ||
s.on('end', () => resolve(sha256Sum.digest('base64'))) | ||
}) | ||
const s = fs.createReadStream(filePath); | ||
s.on("data", (data) => sha256Sum.update(data)); | ||
s.on("error", reject); | ||
s.on("end", () => resolve(sha256Sum.digest("base64"))); | ||
}); | ||
}, | ||
@@ -80,11 +80,11 @@ | ||
case false: | ||
return await inflate(Buffer.from(value, 'base64')) | ||
return await inflate(Buffer.from(value, "base64")); | ||
default: | ||
//true | ||
const compressedValue = await deflate(value) | ||
return Buffer.from(compressedValue).toString('base64') | ||
const compressedValue = await deflate(value); | ||
return Buffer.from(compressedValue).toString("base64"); | ||
} | ||
}, | ||
} | ||
}; | ||
} | ||
@@ -94,14 +94,14 @@ | ||
function getFilledSecret(secret: string): string { | ||
const sha256Sum = crypto.createHash('sha256') | ||
sha256Sum.update(secret) | ||
return sha256Sum.digest('base64') | ||
const sha256Sum = crypto.createHash("sha256"); | ||
sha256Sum.update(secret); | ||
return sha256Sum.digest("base64"); | ||
} | ||
function getKeyAndIV(key: string, iv?: Buffer) { | ||
const ivBuffer = iv || crypto.randomBytes(16) | ||
const derivedKey = crypto.pbkdf2Sync(key, ivBuffer, 1e5, 32, 'sha256') | ||
const ivBuffer = iv || crypto.randomBytes(16); | ||
const derivedKey = crypto.pbkdf2Sync(key, ivBuffer, 1e5, 32, "sha256"); | ||
return { | ||
iv: ivBuffer, | ||
key: derivedKey, | ||
} | ||
}; | ||
} |
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
104092