const crypto = require('crypto').webcrypto; // Shims the crypto property onto global global.crypto = crypto; let script1 = require('./script1'); let script2 = require('./script2'); let base58 = new script1.baseX('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'); function base58decode(input) { return arraybufferToString( base58.decode(input) ); } function stringToArraybuffer(message) { const messageArray = new Uint8Array(message.length); for (let i = 0; i < message.length; ++i) { messageArray[i] = message.charCodeAt(i); } return messageArray; } function arraybufferToString(messageArray) { const array = new Uint8Array(messageArray); let message = '', i = 0; while(i < array.length) { message += String.fromCharCode(array[i++]); } return message; } function hexToBytes(hex) { let bytes = []; for (let c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return bytes; } function getPasteKey(encoded_key) { return base58decode(encoded_key).padStart(32, '\u0000'); }; async function deriveKey(key, password, spec) { let keyArray = stringToArraybuffer(key); if (password.length > 0) { // version 1 pastes did append the passwords SHA-256 hash in hex if (spec[7] === 'rawdeflate') { let passwordBuffer = await crypto.subtle.digest( {name: 'SHA-256'}, stringToArraybuffer( utf16To8(password) ) ).catch(Alert.showError); password = Array.prototype.map.call( new Uint8Array(passwordBuffer), x => ('00' + x.toString(16)).slice(-2) ).join(''); } let passwordArray = stringToArraybuffer(password), newKeyArray = new Uint8Array(keyArray.length + passwordArray.length); newKeyArray.set(keyArray, 0); newKeyArray.set(passwordArray, keyArray.length); keyArray = newKeyArray; } // import raw key const importedKey = await crypto.subtle.importKey( 'raw', // only 'raw' is allowed keyArray, {name: 'PBKDF2'}, // we use PBKDF2 for key derivation false, // the key may not be exported ['deriveKey'] // we may only use it for key derivation );//.catch(Alert.showError); console.log("iterations= ", spec[2]) console.log("salt = ", spec[1]) // derive a stronger key for use with AES return crypto.subtle.deriveKey( { name: 'PBKDF2', // we use PBKDF2 for key derivation salt: stringToArraybuffer(spec[1]), // salt used in HMAC iterations: spec[2], // amount of iterations to apply hash: {name: 'SHA-256'} // can be "SHA-1", "SHA-256", "SHA-384" or "SHA-512" }, importedKey, { name: 'AES-' + spec[6].toUpperCase(), // can be any supported AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH" or "HMAC") length: spec[3] // can be 128, 192 or 256 }, true, // the key may not be exported ['encrypt', 'decrypt'] // we may only use it for en- and decryption );//.catch(Alert.showError); } function cryptoSettings(adata, spec) { return { name: 'AES-' + spec[6].toUpperCase(), // can be any supported AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH" or "HMAC") iv: stringToArraybuffer(spec[0]), // the initialization vector you used to encrypt additionalData: stringToArraybuffer(adata), // the addtional data you used during encryption (if any) tagLength: spec[4] // the length of the tag you used to encrypt (if any) }; } async function decipher(key, password, data) { let adataString, spec, cipherMessage, plaintext; //let zlib = (await z); if (data instanceof Array) { // version 2 adataString = JSON.stringify(data[1]); // clone the array instead of passing the reference spec = (data[1][0] instanceof Array ? data[1][0] : data[1]).slice(); cipherMessage = data[0]; } spec[0] = atob(spec[0]); spec[1] = atob(spec[1]); settings = cryptoSettings(adataString, spec) console.log(settings) console.log("iv = ", Buffer.from(settings.iv).toString('hex')) console.log("aad = ", Buffer.from(settings.additionalData).toString('hex')) decoded_ct = stringToArraybuffer(atob(cipherMessage)) console.log("len =", decoded_ct.length) console.log("ciphertext= ", Buffer.from(decoded_ct).toString('hex')) try { plaintext = await crypto.subtle.decrypt( cryptoSettings(adataString, spec), await deriveKey(key, password, spec), stringToArraybuffer( atob(cipherMessage) ) ); } catch(err) { console.error("error here:", err); return ''; } } function getCipherData(message) { return message.v === 1 ? message.data : [message.ct, message.adata]; } function arrayEquals(a, b) { return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((val, index) => val === b[index]); } myspec = [ 'Ho5WSJK+eZzm4jVZgiOlIA==', 'VFiCvGq9/q8=', 100000, 256, 128, 'aes', 'gcm', 'zlib', ]; myMessage = {"status":0,"id":"564bfba973c990c9","url":"\/?564bfba973c990c9","adata":[["Ho5WSJK+eZzm4jVZgiOlIA==","VFiCvGq9\/q8=",100000,256,128,"aes","gcm","zlib"],"markdown",1,0],"meta":{"created":1688131816,"time_to_live":270},"v":2,"ct":"ouhXyQe0q+o7cfXHzrLpHT+sbet2FEUqyya9cCmxcwrAVlu4v221RQzL9Yp3ApIVL+vJuShF6\/myA9ZaBlIcru04DAk5wnuM98oQ5qrTd8HxGMAeu0AKCodGtkMXvEcLAtQNcJEL2vHkHEiLLn0zL5RvBa4PxpAlBQVXiNvsRQxEFHtsYG95oV\/3Fwb0GBQWdQEZK7qgSl0YX27ls2LvP8WFfC+Lpw1sIrV2WPMsOAV3cxySnGPvq6Z62qasm7u07i1kLlA9wpbh8dz9YTQbeYKq8wdCXA0JJUrJQnIFUGr8aHTDwZEiUUtvx4BOUp8HQldKfNhyIsO\/hxvYu639WiURU37zjSO\/1Lj3oPFQ0I7F1fJ8KrYK5rL2tiOvIdDSwdv213koxWDHGZstt3lxQuHc8ODJTYthLWXoZBFHm4jtJ0kubiTdcdcsuQRHZ+wVY3Eh\/oKakCCiP4CTuS6hl\/3li6nohJBGCRSzJqyE7ZwEMNcW71Cv2JnKeSrYIZd4tYM1GtWQ62ogUfLdSWUbdwzImOo+7IkSB32CrUf4oYrGBfS6UAsePN2+dVT3V7S2e30scl75plIPWR0ZiXMOD7Qgqzum7p\/eJWHcAArs9RZ4PCJdlO5T7YspPD3rWQqFQmOoGE+jAIfd7is3une7JAWp1tL9TIxPbB0KSHiVL8Am\/hNXU16O1lcH6taGZYCMN\/2PaC+Gh9jOOtPpefYCGVdbLmON5MIdOiEhIgyB9hboRsisxWBEOtopW1W9aA6Yjjq46koJTmvYyWibS9CJ7EauvS4Q98IaStqUOE4WcnP50Hl1fj1h+dZzdlzmstDe08hGoKBK\/hJDI+dhaNRENRSFK4Y\/dtqW4wHleFX\/HVXqERH9\/eZmbItME62OV6+OzY6hxqqPtTIDkILr78idgj\/TtDGpCNrOJDfJ7chO0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sm9t1\/63","comments":[],"comment_count":0,"comment_offset":0,"@context":"?jsonld=paste"} //taken from decipher myspec[0] = atob(myspec[0]); myspec[1] = atob(myspec[1]); cipherdata = getCipherData(myMessage) console.log(cipherdata) mykey = '16463bb6c4a3fdbdcd82c0b164455304c321e1aa3cdcf288e7a6d1dfa4b82db6'; mykey2 = getPasteKey("2Vx5saNCEFZqdYaQbTxkDiNCiQ99kRXbVEz8UHr4cE1T") console.log(hexToBytes(mykey)) console.log(mykey2) console.log("mykey2 = ", stringToArraybuffer(mykey2)) mypassword = ''; key_bytes = hexToBytes(mykey) effective_key = deriveKey(mykey2, mypassword, myspec).then( (k) => { console.log(k); return crypto.subtle.exportKey("raw", k); }).then((a) => {console.log(a);}); decipher(mykey2, mypassword, cipherdata).then((plaintext) => {console.log(plaintext);}).catch(err => {console.log("caught");});
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");