Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zegnat/cf14a70e34bfa1cb5739 to your computer and use it in GitHub Desktop.
Save Zegnat/cf14a70e34bfa1cb5739 to your computer and use it in GitHub Desktop.
Week 1 - Programming Assignment
/*jslint node: true, bitwise: true */
"use strict";
// Polyfilling-ish String#encode from Python.
String.prototype.encode = function () {
var out = '', i, l = this.length;
for (i = 0; i < l; i += 1) {
out += ('00' + this.charCodeAt(i).toString(16)).slice(-2);
}
return out;
};
var MSGS = [ /* --- 11 secret messages --- */ ];
function strxor(a, b) {
var out = '', i, l = Math.min(a.length, b.length);
for (i = 0; i < l; i += 1) {
out += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
}
return out;
}
function random(size) {
size = size || 16;
return require('crypto').randomBytes(size).toString();
}
function encrypt(key, msg) {
var c = strxor(key, msg);
console.log();
console.log(c.encode('hex'));
return c;
}
function main() {
var key = random(1024), i, l = MSGS.length;
for (i = 0; i < l; i = i + 1) {
encrypt(key, MSGS[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment