Skip to content

Instantly share code, notes, and snippets.

View sergeevabc's full-sized avatar

Aleksandr Sergeev sergeevabc

View GitHub Profile
@sergeevabc
sergeevabc / stringFill.js
Last active August 29, 2015 14:07
Filling string with chars N times (e.g. to test long vectors of hash functions) http://www.di-mgt.com.au/sha_testvectors.html
// console.log(sha1(stringFill("a", 1000000)));
function stringFill(string, times) {
var result = "";
for (;;) {
if (times & 1) result += string;
times >>= 1;
if (times) string += string;
else break;
}
@sergeevabc
sergeevabc / FisherYatesShuffle.js
Created October 17, 2014 16:17
Fisher-Yates shuffle
var i = arr.length,
rndIndex, tmpValue;
while (--i) {
rndIndex = ~~(Math.random() * (i + 1));
tmpValue = arr[i];
arr[i] = arr[rndIndex];
arr[rndIndex] = tmpValue;
}
@sergeevabc
sergeevabc / TypeOfVariable.js
Created October 17, 2014 11:50
What is the type of that variable? (String, Array, etc)
function type(variable) {
return Object.prototype.toString.call(variable).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
}
@sergeevabc
sergeevabc / Base62.js
Created October 17, 2014 11:48
Base62 encode, decode https://github.com/odan/base62js 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
if (!$d) {
var $d = {}
}
$d.BitStream = function BitStream(options) {
this.Source = [];
if (typeof options === 'object') {
this.Source = options
}
if (typeof options === 'number') {
var dim = Math.floor(options);
@sergeevabc
sergeevabc / Base85ZeroMQ.js
Last active August 29, 2015 14:07
Base85 ZeroMQ encode 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#
// https://github.com/noseglid/base85
function z85encode(buffer) {
var enctable = {
0: '0',
1: '1',
2: '2',
3: '3',
4: '4',
5: '5',
6: '6',
@sergeevabc
sergeevabc / Base58.js
Last active August 29, 2015 14:07
Base58 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ
// https://github.com/cryptocoinjs/bs58
function base58encode(buffer) {
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var ALPHABET_MAP = {};
for (var i = 0; i < ALPHABET.length; i++) {
ALPHABET_MAP[ALPHABET.charAt(i)] = i;
}
var BASE = 58;
if (buffer.length === 0)
return '';
@sergeevabc
sergeevabc / Base91.js
Last active August 29, 2015 14:07
Base91 encode, decode https://github.com/mscdex/base91.js ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
(function() {
var hasNode = (typeof process !== 'undefined' && process.versions && process.versions.node),
hasTypedArray = (typeof Uint8Array !== 'undefined');
if (hasNode && require('fs').existsSync(__dirname + '/../build/Release/base91encdec.node')) {
module.exports = require(__dirname + '/../build/Release/base91encdec.node');
return;
}
var AVERAGE_ENCODING_RATIO = 1.2297,
ENCODING_TABLE = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
@sergeevabc
sergeevabc / Base64novnc.js
Last active August 29, 2015 14:07
Base64 encode http://jsperf.com/base64-encode/3 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ and =
var Base64novnc = {
encode: function(data) {
"use strict";
var result = '';
var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('');
var length = data.length;
var lengthpad = (length % 3);
// Convert every three bytes to 4 ascii characters.
for (var i = 0; i < (length - 2); i += 3) {
result += toBase64Table[data[i] >> 2];
function stringToUTF8Array(s) {
var i,
d = unescape(encodeURIComponent(s)),
b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
}
function Uint8ArrayToString(arr) {
var encodedString = String.fromCharCode.apply(null, arr),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
}