// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var Util = function() {
this.emailRegEx = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
this.hexdump = function(str) {
var r=[];
var e=str.length;
var c=0;
var h;
var i = 0;
while(c
'
* Different than print_debug because will call hexstrdump iff necessary.
* @param {String} str String of the debug message
* @return {String} An HTML tt entity containing a paragraph with a
* style attribute where the debug message is HTMLencoded in.
*/
this.print_debug_hexstr_dump = function(str,strToHex) {
if (openpgp.config.debug) {
str = str + this.hexstrdump(strToHex);
this.print_output(this.printLevel.debug, str);
}
};
/**
* Helper function to print an error message.
* The calling Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '
'
* @param {String} str String of the error message
* @return {String} A HTML paragraph entity with a style attribute
* containing the HTML encoded error message
*/
this.print_error = function(str) {
this.print_output(this.printLevel.error, str);
};
/**
* Helper function to print an info message.
* The calling Javascript context MUST define
* a "showMessages(text)" function. Line feeds ('\n')
* are automatically converted to HTML line feeds '
'.
* @param {String} str String of the info message
* @return {String} A HTML paragraph entity with a style attribute
* containing the HTML encoded info message
*/
this.print_info = function(str) {
this.print_output(this.printLevel.info, str);
};
this.print_warning = function(str) {
this.print_output(this.printLevel.warning, str);
};
this.print_output = function(level, str) {
var html;
str = openpgp_encoding_html_encode(str).replace(/\n/g,"
");
if (level == this.printLevel.debug) {
html = "
"+str+"
"; } else { var color, heading; switch (level) { case this.printLevel.error: color = "FF8888"; heading = "ERROR"; break; case this.printLevel.warning: color = 'FFAA88'; heading = "WARNING"; break; case this.printLevel.info: color = '88FF88'; heading = 'INFO'; break; } html = ""+heading+":"+str+"
"; } if (typeof showMessages === "function") { // only call function if defined showMessages(html); } } this.getLeftNBits = function (string, bitcount) { var rest = bitcount % 8; if (rest == 0) return string.substring(0, bitcount / 8); var bytes = (bitcount - rest) / 8 +1; var result = string.substring(0, bytes); return this.shiftRight(result, 8-rest); // +String.fromCharCode(string.charCodeAt(bytes -1) << (8-rest) & 0xFF); }; /** * Shifting a string to n bits right * @param {String} value The string to shift * @param {Integer} bitcount Amount of bits to shift (MUST be smaller * than 9) * @return {String} Resulting string. */ this.shiftRight = function(value, bitcount) { var temp = util.str2bin(value); if (bitcount % 8 != 0) { for (var i = temp.length-1; i >= 0; i--) { temp[i] >>= bitcount % 8; if (i > 0) temp[i] |= (temp[i - 1] << (8 - (bitcount % 8))) & 0xFF; } } else { return value; } return util.bin2str(temp); }; /** * Return the algorithm type as string * @return {String} String representing the message type */ this.get_hashAlgorithmString = function(algo) { switch(algo) { case 1: return "MD5"; case 2: return "SHA1"; case 3: return "RIPEMD160"; case 8: return "SHA256"; case 9: return "SHA384"; case 10: return "SHA512"; case 11: return "SHA224"; } return "unknown"; }; }; /** * an instance that should be used. */ var util = new Util();