Skip to content

Instantly share code, notes, and snippets.

@krizpoon
Last active August 29, 2015 14:07
Show Gist options
  • Save krizpoon/0fe7e970be1f14213613 to your computer and use it in GitHub Desktop.
Save krizpoon/0fe7e970be1f14213613 to your computer and use it in GitHub Desktop.
Javascript Data Utilities
/*
Converts byte array -> base64 encoded string
*/
function base64EncodeBytes (aBytes)
{
function uint6ToB64 (nUint6)
{
return nUint6 < 26 ?
nUint6 + 65
: nUint6 < 52 ?
nUint6 + 71
: nUint6 < 62 ?
nUint6 - 4
: nUint6 === 62 ?
43
: nUint6 === 63 ?
47
:
65;
}
var nMod3 = 2, sB64Enc = "";
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++)
{
nMod3 = nIdx % 3;
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1)
{
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) + (nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==');
}
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
function bytesToUTF8(array)
{
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len)
{
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
return out;
}
/*
* Read image data from an IMG element
*
* imageFormat: e.g. image/png
* quality: used for image formats like JPEG
* outputFormat: dataURL | base64 | binary | bytes | blob
*/
$.fn.getImageData = function(outputFormat, imageFormat, quality)
{
if (!this.is('img'))
{
return null;
}
else
{
var img = this.get(0);
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
if (!imageFormat) imageFormat = "image/png";
var dataUrl = canvas.toDataURL(imageFormat, quality);
if (outputFormat == 'dataURL') return dataUrl;
var b64 = dataUrl.replace(/^data:[\w\/]+;base64,/, "");
if (outputFormat == 'base64') return b64;
var binStr = atob(b64);
if (outputFormat == 'binary') return binStr;
function TSStringToBytes(str) { var bytes = new Uint8Array(str.length); for (var i=0; i<str.length; i++) bytes[i] = str.charCodeAt(i); return bytes; }
var bytes = TSStringToBytes(binStr);
if (outputFormat == 'bytes') return bytes;
return new Blob([bytes], {type:imageFormat});
}
}
function hex2bin(hex)
{
if (!hex) return null;
hex = hex.replace(/\s/g, '');
var bytes = [];
for(var i=0; i< hex.length-1; i+=2) { bytes.push(parseInt(hex.substr(i, 2), 16)); }
return String.fromCharCode.apply(String, bytes);
}
function hexToArray(hex)
{
if (!hex) return null;
hex = hex.replace(/\s/g, '');
var n = hex.length / 2;
var arr = new Uint8Array(n);
for (var i=0; i<n; i++) { arr[i] = parseInt(hex.substr(i*2, 2), 16); }
return arr;
}
/*
* RC4 symmetric cipher encryption/decryption
*
* @license Public Domain
* @param string key - secret key for encryption/decryption
* @param string str - string to be encrypted/decrypted
* @return string
*/
function rc4(key, str)
{
var s = [], j = 0, x, res = [];
for (var i = 0; i < 256; i++) {
s[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
for (var y = 0; y < str.length; y++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
res.push(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment