Last active
February 27, 2025 14:41
-
-
Save yohanesgultom/fa00934584ecac3b1a304f996574d804 to your computer and use it in GitHub Desktop.
Random javascripts (scripts?)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Random javascripts (scripts?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Good old jquery form submit leveraging HTML5 validation | |
// Disabling buttons during async process | |
// Enabling buttons once async process completed | |
// Data Type flavors: FormData, JSON or URL-encoded | |
$('form').on('submit', function(e) { | |
let form = $(this) | |
e.preventDefault() | |
form.find('button').attr('disabled', true) | |
// form data | |
let data = new FormData(this) | |
let config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: data, | |
processData: false, | |
contentType: false, | |
} | |
if (data.get('dataType') == 'json') { | |
data = {} | |
form.serializeArray().map(x => data[x.name] = x.value, {}) | |
config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: JSON.stringify(data), | |
dataType: "json", | |
contentType: "application/json; charset=utf-8", | |
} | |
} else if (data.get('dataType') == 'urlEncoded') { | |
data = form.serialize() | |
console.log(data) | |
config = { | |
url: 'https://httpbin.org/post', | |
type: "POST", | |
data: data, | |
} | |
} | |
// submit | |
$.ajax(config).done(function(res) { | |
alert('Success. Yay') | |
console.log(res) | |
}) | |
.fail(function(err) { | |
alert('Failed. Booo') | |
console.log(err) | |
}) | |
.always(function() { | |
form[0].reset() | |
form.find('button').removeAttr('disabled') | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JS version of Microsoft Enterprise Library Cryptographer's salted Hash Provider | |
* Which is discussed on https://stackoverflow.com/questions/126148/what-is-the-salt-in-enterprise-library-hashprovider-saltenabled-key | |
*/ | |
const crypto = require('crypto'); | |
function encryptPasswordWithSalt(password, salt, encoding='base64') { | |
const hash = crypto.createHash('sha256') | |
if (salt) { | |
hash.update(salt, 'utf-8') | |
} | |
return hash.update(password, 'utf-8') | |
.digest() | |
.toString(encoding); | |
} | |
/** | |
* https://github.com/Chavoshi/EnterpriseLibrary.NetCore/blob/master/Source/Cryptography%20Application%20Block/Security.Cryptography/HashAlgorithmProvider.cs#L149-L165 | |
* | |
* @param {string} base64hash | |
*/ | |
function extractSaltAndHash(base64hash) { | |
const buff = Buffer.from(base64hash, 'base64') | |
const saltBuff = buff.subarray(0, 16) | |
const hashBuff = buff.subarray(16, 48) | |
return [saltBuff, hashBuff] | |
} | |
/** | |
* https://github.com/Chavoshi/EnterpriseLibrary.NetCore/blob/master/Source/Cryptography%20Application%20Block/Security.Cryptography/HashAlgorithmProvider.cs#L93-L127 | |
* | |
* @param {string} password | |
* @param {string} base64hash | |
*/ | |
function compareHash(password, base64hash) { | |
const [ saltBuff, hashBuff ] = extractSaltAndHash(base64hash) | |
const passwordBuff = Buffer.from(password, 'utf-16le') | |
const passwordHashBuff = crypto.createHash('sha256').update(saltBuff).update(passwordBuff).digest() | |
console.log('password hash : ' + passwordHashBuff.toString('hex')) | |
console.log('extracted hash: ' + hashBuff.toString('hex')) | |
return passwordHashBuff.compare(hashBuff) === 0 | |
} | |
/** | |
* https://github.com/Chavoshi/EnterpriseLibrary.NetCore/blob/master/Source/Cryptography%20Application%20Block/Security.Cryptography/Cryptographer.cs#L200-L206 | |
* | |
* @param {string} password | |
*/ | |
function createHash(password) { | |
const passwordBuff = Buffer.from(password, 'utf-16le') | |
const saltBuff = crypto.randomBytes(16) | |
const passwordHashBuff = crypto.createHash('sha256').update(saltBuff).update(passwordBuff).digest() | |
return Buffer.concat([saltBuff, passwordHashBuff]).toString('base64') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const bitcoin = require("bitcoinjs-lib"); | |
const bitcoinNetwork = bitcoin.networks.testnet; | |
/** | |
* Send bitcoin in testnet using BlockCypher | |
* @param {number} amount - Bitcoin amount in BTC | |
* @param {string} to - output Bitcoin wallet address | |
* @param {string} from - input Bitcoin wallet address | |
* @param {string} wif | |
*/ | |
const sendBitcoin = function (amount, to, from, wif) { | |
let keys = bitcoin.ECPair.fromWIF(wif, bitcoinNetwork); | |
return new Promise(function (resolve, reject) { | |
// create tx skeleton | |
request.post({ | |
url: 'https://api.blockcypher.com/v1/btc/test3/txs/new', | |
body: JSON.stringify({ | |
inputs: [{ addresses: [ from ] }], | |
// convert amount from BTC to Satoshis | |
outputs: [{ addresses: [ to ], value: amount * Math.pow(10, 8) }] | |
}), | |
}, | |
function (err, res, body) { | |
if (err) { | |
reject(err); | |
} else { | |
let tmptx = JSON.parse(body); | |
// signing each of the hex-encoded string required to finalize the transaction | |
tmptx.pubkeys = []; | |
tmptx.signatures = tmptx.tosign.map(function (tosign, n) { | |
tmptx.pubkeys.push(keys.getPublicKeyBuffer().toString('hex')); | |
return keys.sign(new Buffer(tosign, 'hex')).toDER().toString('hex'); | |
}); | |
// sending back the transaction with all the signatures to broadcast | |
request.post({ | |
url: 'https://api.blockcypher.com/v1/btc/test3/txs/send', | |
body: JSON.stringify(tmptx), | |
}, | |
function (err, res, body) { | |
if (err) { | |
reject(err); | |
} else { | |
// return tx hash as feedback | |
let finaltx = JSON.parse(body); | |
resolve(finaltx.tx.hash); | |
} | |
} | |
); | |
} | |
} | |
); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from https://gist.github.com/anvk/5602ec398e4fdc521e2bf9940fd90f84 | |
function workMyCollection(arr) { | |
return arr.reduce((promise, item) => { | |
return promise | |
.then((result) => { | |
return asyncFunc(item); | |
}) | |
.catch(console.error); | |
}, Promise.resolve()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cropper = { w: 369, h: 390 }, | |
draw = SVG('cropper').size(cropper.w, cropper.h), | |
jawMask = draw.path('M 50 10 C 50 20 100 20 100 10 L 100 60 C 100 80 50 80 50 60 Z') | |
addHandlers(jawMask, [[0, 1, 2], [1, 5, 6], [2, 1, 2], [3, 5, 6]]) | |
function addHandlers(path, points) { | |
var array = path.array().value, | |
doc = path.doc(), | |
handlers = [] | |
// save points to be moved together | |
for (var i = 0; i < points.length; i++) { | |
var p = points[i], | |
x = array[p[0]][p[1]], | |
y = array[p[0]][p[2]], | |
rect = doc.rect(6, 6).data('point', p).fill('#fff').center(x, y).draggable().on('dragmove', function(e) { | |
p = this.data('point') | |
array[p[0]][p[1]] = e.detail.p.x | |
array[p[0]][p[2]] = e.detail.p.y | |
path.plot(array.join(' ')) | |
}) | |
handlers.push(rect.id()) | |
} | |
path.data('handlerIds', handlers) | |
path.on('dragmove', function(e) { | |
var ids = this.data('handlerIds'), | |
array = path.array().value | |
for (var i = 0; i < ids.length; i++) { | |
var handler = SVG.get(ids[i]), | |
p = handler.data('point'), | |
x = array[p[0]][p[1]], | |
y = array[p[0]][p[2]] | |
handler.center(x, y) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment