Last active
August 29, 2015 14:13
-
-
Save Zegnat/cf14a70e34bfa1cb5739 to your computer and use it in GitHub Desktop.
Week 1 - Programming Assignment
This file contains 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
/*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