Jump to content

Javascipt String Methods not being recognized.

function getSalt(input) {    var findSalt = input.charAt(0);    for (var i = 0; findSalt == char[i]; i++) {    }    return salts[i];}

Im using WebStorm 10.0.4, and Im getting the error while trying to use the string function .charAt():

    var findSalt = input.charAt(0);                         ^TypeError: undefined is not a function 

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to post
Share on other sites

you're feeding to getSalt a parameter that is not a string

Since I used String() to make it a string, is there a similar way to change it back to a integer?

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to post
Share on other sites

Since I used String() to make it a string, is there a similar way to change it back to a integer?

your function wants a string as a parameter, so that's alright if you're doing that

 

also, what is it in your sentence? it would be helpful if we could see how you invoke the function

Link to post
Share on other sites

your function wants a string as a parameter, so that's alright if you're doing that

 

also, what is it in your sentence? it would be helpful if we could see how you invoke the function

Excuse the last statement, now all I'm getting is undefined as the result.

 

Heres the code

/** * Created by Owner on 11/07/2015. */// Arrays to find and declare the salt to be usedvar salts = ["Nothing(0)", "Single(1)", "Pair(2)", "Triangle(3)", "Quater(4)", "Security(5)", "Hex(6)", "Lucky(7)", "Byte(8)", "Cats(9)", "Array", "Because", "Common", "Demonic", "Elevator", "Fantastic", "Gigabyte", "House", "Intellegent", "Jet", "Kellogs", "Limousine", "Monster", "Nation", "Octopus", "Penguin", "Queen", "Revenge", "Spring", "Tycoon", "Umbrella", "Vanilla", "Wizard", "X-Ray", "Yellow", "Zambony"];var char = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];// Generate code to be sent to the userfunction codeGen(input) {    var length = input,        charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",        retVal = "";    for (var i = 0, n = charset.length; i < length; ++i) {        retVal += charset.charAt(Math.floor(Math.random() * n));    }    return retVal;}// Get the Key to be used when generating the Hashfunction getKey(input) {    var key = String(input);    var _key0 = key.charAt(0),        _key1 = key.charCodeAt(0),        _key2 = key.charCodeAt(key.length),        _keyF = key + _key0 + _key1 + _key2;    return _keyF}function getSalt(input) {    var findSalt = input.charAt(0);    for (var i = 0; findSalt == char[i]; i++) {    }    return salts[i];}/* * Calculate the SHA-1 of an array of big-endian words, and a bit length */function binb_sha1(x, len) {    /* Apply Padding */    x[len >> 5] |= 0x80 << (24 - len % 32);    x[((len + 64 >> 9) << 4) + 15] = len;    var w = Array(80);    var a = 1732584193;    var b = -271733879;    var c = -1732584194;    var d = 271733878;    var e = -1009589776;    for (var i = 0; i < x.length; i += 16) {        var olda = a;        var oldb = b;        var oldc = c;        var oldd = d;        var olde = e;        for (var j = 0; j < 80; j++) {            if (j < 16) w[j] = x[i + j];            else w[j] = bit_rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);            var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),                safe_add(safe_add(e, w[j]), sha1_kt(j)));            e = d;            d = c;            c = bit_rol(b, 30);            b = a;            a = t;        }        a = safe_add(a, olda);        b = safe_add(b, oldb);        c = safe_add(c, oldc);        d = safe_add(d, oldd);        e = safe_add(e, olde);    }    return Array(a, b, c, d, e);}/* * Convert a raw string to an array of big-endian words * Characters >255 have their high-byte silently ignored. */function rstr2binb(input) {    var output = Array(input.length >> 2);    for (var i = 0; i < output.length; i++)        output[i] = 0;    for (var i = 0; i < input.length * 8; i += 8)        output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);    return output;}/* * Convert an array of big-endian words to a string */function binb2rstr(input) {    var output = "";    for (var i = 0; i < input.length * 32; i += 8)        output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF);    return output;}/* * Convert a raw string to a hex string */function rstr2hex(input) {    try {        hexcase    } catch (e) {        hexcase = 0;    }    var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";    var output = "";    var x;    for (var i = 0; i < input.length; i++) {        x = input.charCodeAt(i);        output += hex_tab.charAt((x >>> 4) & 0x0F)            + hex_tab.charAt(x & 0x0F);    }    return output;}function krypton(password, cycles, bytes) {    var _password = String(password),        _cycles = cycles,        _bytes = bytes;        i = 0;    var salt = String(getSalt(_password) + getKey(_password));    _password = String(rstr2binb(salt + _password));    // Total number of cycles    var totalCycles = _cycles;    // Run cycles in chunks instead of all at once, so as to not block.    // Define size of chunk here; adjust for slower or faster machines if necessary.    var cyclesInChunk = 10;    // Cycle counter    var cyclesDone = 0;    // Key length, as number of bytes    var keyLength = _bytes;    // The hash cache    var hash = null;    // The length (number of bytes) of the output of the pseudo-random function.    // Since HMAC-SHA1 is the standard, and what is used here, it's 20 bytes.    var hashLength = 20;    // Number of hash-sized blocks in the derived key (called 'l' in RFC2898)    var totalBlocks = Math.ceil(keyLength / hashLength);    // Start computation with the first block    var currentBlock = 1;    // Used in the HMAC-SHA1 computations    var ipad = new Array(16);    var opad = new Array(16);    // This is where the result of the iterations gets sotred    var cycleBuffer = new Array(0x0, 0x0, 0x0, 0x0, 0x0);    // The result    var result = "";    // This object    var thisObject = this;    // The function to call with the result    var resultFunction;    // The function to call with status after computing every chunk    var statusFunction;    // Set up the HMAC-SHA1 computations    if (_password.length > 16) _password = binb_sha1(_password, _password.length * chrsz);    for (var i = 0; i < 16; ++i) {        ipad[i] = _password[i] ^ 0x36363636;        opad[i] = _password[i] ^ 0x5C5C5C5C;    }    // Starts the computation    this.deriveKey = function (statusCallback, resultCallback) {        statusFunction = statusCallback;        resultFunction = resultCallback;        setTimeout(function () {            thisObject.doCycles()        }, 0);    }    // The workhorse    this.doCycles = function () {        var cycles = cyclesInChunk;        if (totalCycles - cyclesDone < cyclesInChunk)            cycles = totalCycles - cyclesDone;        for (var i = 0; i < cycles; ++i) {            // compute HMAC-SHA1            if (cyclesDone == 0) {                var saltBlock = salt +                    String.fromCharCode(currentBlock >> 24 & 0xF) +                    String.fromCharCode(currentBlock >> 16 & 0xF) +                    String.fromCharCode(currentBlock >> 8 & 0xF) +                    String.fromCharCode(currentBlock & 0xF);                hash = binb_sha1(ipad.concat(rstr2binb(saltBlock)),                    512 + saltBlock.length * 8);                hash = binb_sha1(opad.concat(hash), 512 + 160);            }            else {                hash = binb_sha1(ipad.concat(hash),                    512 + hash.length * 32);                hash = binb_sha1(opad.concat(hash), 512 + 160);            }            for (var j = 0; j < hash.length; ++j)                cycleBuffer[j] ^= hash[j];            cyclesDone++;        }        // Call the status callback function        statusFunction((currentBlock - 1 + cyclesDone / totalCycles) / totalBlocks * 100);        if (cyclesDone < totalCycles) {            setTimeout(function () {                thisObject.doCycles()            }, 0);        }        else {            if (currentBlock < totalBlocks) {                // Compute the next block (T_i in RFC 2898)                result += rstr2hex(binb2rstr(cycleBuffer));                currentBlock++;                cycleBuffer = new Array(0x0, 0x0, 0x0, 0x0, 0x0);                cyclesDone = 0;                setTimeout(function () {                    thisObject.doCycles()                }, 0);            }            else {                // We've computed the final block T_l; we're done.                var tmp = rstr2hex(binb2rstr(cycleBuffer));                result += tmp.substr(0, (keyLength - (totalBlocks - 1) * hashLength) * 2);                // Call the result callback function                resultFunction(result);            }        }    }}console.log(krypton("password", 8192, 1024)); 

Sources: http://pajhome.org.uk/crypt/md5/sha1.html, http://anandam.name/pbkdf2/pbkdf2.js.txt

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×