Giter Site home page Giter Site logo

caligatio / jssha Goto Github PK

View Code? Open in Web Editor NEW
2.2K 71.0 396.0 1.91 MB

A JavaScript/TypeScript implementation of the complete Secure Hash Standard (SHA) family (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512, SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC.

Home Page: https://caligatio.github.io/jsSHA/

License: BSD 3-Clause "New" or "Revised" License

JavaScript 22.32% Python 0.98% TypeScript 76.70%
sha sha-1 sha-2 sha-3 sha-224 sha-256 sha-384 sha-512 sha3-224 sha3-256

jssha's People

Contributors

aravinth2094 avatar bsoyka avatar caligatio avatar dependabot[bot] avatar drewcovi avatar frostschutz avatar ivanrf avatar jbjulia avatar kofalt avatar piranna avatar vlna avatar vogievetsky avatar wesbos avatar wkovacs64 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jssha's Issues

TEXT/ASCII mode silently gives incorrect output for non-ASCII bytes

test.js

function unhexlify(s) {
    if (s.length === 0) { return ''; }
    return s.match(/.{1,2}/g).map(function(x) { return String.fromCharCode(parseInt(x, 16)); }).join('');
}
jsSHA = require("./src/sha512.js");
console.log((new jsSHA("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", "HEX")).getHash("SHA-512", "HEX"));
console.log((new jsSHA(unhexlify("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"), "TEXT")).getHash("SHA-512", "HEX"));
$ nodejs test.js 
357c83864f2833cb427a2ef1c00a013cfdff2768d980c0a3a520f006904de90f9b4f0afe280b746a778684e75442502057b7473a03f08f96f5a38e9287e01f8f
d81fdbd3bfff49c323e40f488da54ed29a178161b0bed812dc6f59f17e70d3783e0f9bdaa50351ae0d0f64b7f800f3335ba1cbafc50ce9cdf2fc2840bd636e0f

This is causing my test of djbec.js to fail, since that uses jsSHA's TEXT/ASCII mode to hash arbitrary bytes. If I manually hex the input and tell jsSHA to decode it in HEX mode, the test works.

Diff for djbec.js to make it work with jsSHA

 function sha512(s) {                      // Requires jsSHA
-  var shaObj = new jsSHA(s, "ASCII");
+  var shaObj = new jsSHA(hexlify(s), "HEX");
   return bi2bytes(hex2bi(shaObj.getHash("SHA-512", "HEX")), 64).reverse();
 }

If TEXT/ASCII mode is not supposed to work with arbitrary bytes, jsSHA should at least offer a BIN mode.

djbec.js: www.flownet.com/ron/code/djbec.js
Test code: https://gist.github.com/infinity0/bd580784d94c147c0564
Test input: http://ed25519.cr.yp.to/python/sign.input

jsSHA SHA-1 nodejs wrong hashed result

I've tried to use the jsSHA SHA-1 implementation in node but had a weird bug.
An earlier implementation of the same method in the browser works as expected.

I wrote a small nodeunit test that uses jsSHA and node crypto to hash the same input.

Running the script in nodeunit I get:

Expected :"B153C0D8C766A64F677A3D1797E680D18EC571DD"
Actual   :"FF620005437AB0686DEF9D889D732B902B8A4B98"

If there's something wrong with the implementation just tell me 👍

Error in Strict mode

When I use the "use strict" string in my function calling jsSHA (just sha1), I have an error : "A is undefined". This error is revealad by the strict mode, and was not there on a previous version of the library, I don't know its number because it was not in the code, I just know it was from 2012 (copyright 2008-2012). I can compare with code history to find it, if it helps you.

I have the same problem with the complete library with "T is undefined".

I don't know what to change in your library to "correct" this. Could you take a look? Thanks.

Handling of non-BMP Unicode characters

Hi, when I inspect the code here, for example, it looks like the code might not be handling non-BMP Unicode characters correctly (i.e. Unicode characters whose code points are higher than 65,536):

for (i = 0; i < str.length; i += 1)
{
  codePnt = str.charCodeAt(i);
  binArr = [];
  ...
}

For example, in its documentation of String.prototype.charCodeAt(), MDN says--

Unicode code points range from 0 to 1,114,111. The first 128 Unicode code points are a direct match of the ASCII character encoding. For information on Unicode, see the JavaScript Guide.

Note that charCodeAt will always return a value that is less than 65,536. This is because the higher code points are represented by a pair of (lower valued) "surrogate" pseudo-characters which are used to comprise the real character. Because of this, in order to examine or reproduce the full character for individual characters of value 65,536 and above, for such characters, it is necessary to retrieve not only charCodeAt(i), but also charCodeAt(i+1) (as if examining/reproducing a string with two letters). See example 2 and 3 below.

But in the code, there is no logic to support looking at "charCodeAt(i+1)". Can you confirm? Do you have any test cases to check non-BMP characters?

Sorry for not preparing a test case myself, but I figured I would just ask. Thanks.

Rounds of hashing?

How can I best salt a hash and then hash it for a predetermined number of rounds?

Incrementally hashing an HTML5 File object

I noticed this on your most release:

The API is changing significantly with upcoming v2.0 to support streaming

That's great to hear, as I'm in the position of having to upload files from the browser to an API that demands to know its hash. Odds are that I'll use an HTML File object to read various slices of the file without loading the whole thing into memory.

I was wondering after the status of your streaming branch, and if there's anything I could contribute towards that end. Further, have you considered File objects, or how to enqueue incremental hashing to not lock up the browser?

[ I figured File object support was worth separately tracking from #22. ]

Obtain several hashes with the same shaObj

Hi,

The code below:

var shaObj = new jsSHA("SHA-1", "TEXT");
shaObj.update("a");
document.write(shaObj.getHash("HEX"));
document.write('<br>');
shaObj.update("b");
document.write(shaObj.getHash("HEX"));

displays the same result twice:

86f7e437faa5a7fce15d1ddcb9eaeaea377667b8
86f7e437faa5a7fce15d1ddcb9eaeaea377667b8

Is there a way to not reinstanciate a jsSHA ?

Thanks.

If file size is too large, Errors out with Maximum Call Stack Exceeded

Bro, love the library - using in production as we speak! We've just upped our file upload limit and we've noticed the hash generation errors out on the search call. I'll need to rebuild with an unminified version to get you a better trace but wanted to give a heads up.

Thanks for the amazing and compact libray!
Conor

sha512 getting different hashes from same string

I don't know what I'm doing wrong. You already saw my script in the other issue and you didn't tell me nothing about it so I guess I'm doing something different instead of what I need.

I want to hash the password values in the client, and the hash of a string should be the same always but I'm getting different hashes from the same string.

In this example the hashes are different every time I press the button: https://jsfiddle.net/pa9sgx3v/3/

What am I doing wrong?

Add method or parameter for multiple iteration

Hi.

It would be great if multiple hashing with specified iteration count is available.

function isha(hashAlgorithm, inputString, inputFormat, inputEncoding,
iterationCount, outputFormat, outputFormatOpts) {

if (iterationCount <= 0) {
    throw "iterationCount(" + iterationCount + ") <= 0";
}

var intermediateFormat = "HEX";
var intermediateEncoding = "UTF8";

var outputString = new jsSHA(inputString, inputFormat, inputEncoding)
    .getHash(hashAlgorithm, intermediateFormat);

for (var i = 1; i < iterationCount - 1; i++) {
    outputString = new jsSHA(outputString, intermediateFormat,
        intermediateEncoding)
        .getHash(hashAlgorithm, intermediateFormat);
}

if (iterationCount > 1) {
    outputString = new jsSHA(outputString, intermediateFormat,
        intermediateEncoding).getHash(hashAlgorithm, outputFormat,
        outputFormatOpts);
}

return outputString;
}

Import in webpack ES6 project

Hi
I 'm tring to import in ES6 Webpack project.
I installed with npm (npm install jsSHA) and then I imported with "import jsSHA".
When i try to use new jsSHA() object the jsSHA is undefined.
Can you help me?

Your documentation is misleading

Please indicate in your documentation that your master branch is version 2 of your library, it is terribly misleading and costs time to try and figure out why it works differently from the usage I found in cordova's twitter oauth plugin only to realize after looking at your branches that you actually have a version 1 and version 2 😢

Using 'salt' value

Does current functionality support adding a given 'salt' value as an extra setting/parameter, so that it can hash the password with it?

Loop error

I'm trying to obtain the sha-1 of 1559 files stored in an array.
For that purpose I've implemented the following routine:

           for (var i=0; i<_files.length; i++){
                (function(file, i) {
                    var reader = new FileReader();
                    reader.onload = function () {
                        try {
                            var raw_content = reader.result;
                            var actual_contents = raw_content.slice(raw_content.indexOf(',') + 1);
                            var shaObj = new jsSHA("SHA-1", "B64");
                            shaObj.update(actual_contents);
                            var file_hash = shaObj.getHash("HEX");
                            console.log("File " + i + " hashed.");
                            file.file_hash = file_hash;
                        }
                        catch (e){
                            console.log("File " + i + "failed");
                            console.log(e);
                            console.log(file);
                        }
                    };
                    reader.readAsDataURL(file);
                })(_files[i], i);
            }

My problem is that the loop only works well with the first 995 elements. Since element 996 it starts to fail returning me this error "Error: Invalid character in base-64 string"

I know that this issue it's possibly more related to the way that I'm trying to read the files than to the jsSHA library, but it someone could shed some light into my mind it will be very helpful to me.

Decryption missing?

Hi,

Nice library. Am I correct in understanding that jsSHA will encrypt using SHA or HMAC, but will not reverse a hash (or hash + key) to output the original data?

Is it possible to implement a function like that, given a relatively limited source dataset (like credentials)?

Thanks,
Rev

Supplied parameters do not match any signature of call target

Following the code for this tutorial:

Typescript Error
Supplied parameters do not match any signature of call target.

src/providers/twitter-utils.ts

headerParameters.oauth_signature = encodeURIComponent(oauthSignatureObject.getHMAC(encodeURIComponent(secretKey) + "&" + encodedTokenSecret, "TEXT", "SHA-1", "B64"));
var headerParameterKeys = Object.keys(headerParameters);
"@types/jssha": "0.0.29"
"jssha": "1.6.2"
"typescript": "2.0.6"

HmacSha256 sometimes produces invalid result since v2.0.0

It seems that sometimes HmacSha256 produces invalid digests in sha.js v2.0.0.

For example for the following input the HmacSha256 is different in sha.js v2.0.0 compared to v1.6.0 (and other sources):

  • secret-key d32712ef9aee67ab414c264e63cf0926 (TEXT)
  • hash input aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (TEXT)
  • expected HmacSha256 is 559eb88c83f6d45f4571b2607705416482494b8e67ad1a3611d10197fd6a2cdb
  • actual HmacSha256 is f1b9ec646824d7424dcf1c0a24303767ab30a08ecd2b94cb6f039fd097fe5ac3 (e.g. according to https://caligatio.github.io/jsSHA/)

I've checked the expected HmacSha256 with a couple of generators:

The cause may be input-length-related, because adding or removing 1 character from the hash-input will produce a correct digest again, e.g.:

  • hash-input aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (TEXT) produces the correct HmacSha256 1fd31c2bdbc9b1643161b87ef860dd2ded12b0017d8be2d1a9898109cbabab8b
  • hash-input aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (TEXT) produces the correct HmacSha256 522423e4629e947f277709d6f70b17296f75d87f9127a32cfbb1f2c472d70e45

Could you please check? Thanks!

Hashing is different from C#

In C# I have the following code which hashes a string:

private static string Hash(string text)
{
    byte[] bytes = Encoding.Unicode.GetBytes(text);

    using (var generator = new SHA256Managed())
    {
        byte[] hash = generator.ComputeHash(bytes);
        return BytesToHex(hash);
    }
}

private static string BytesToHex(byte[] bytes)
{
    var hex = new StringBuilder(bytes.Length * 2);
    foreach (byte b in bytes)
    {
        hex.AppendFormat("{0:x2}", b);
    }

    return hex.ToString();
}

return Hash(text);

This encodes it with UTF-16 and because NodeJS Cryto does not support UTF-16 the following does not work:

return crypto.createHash('sha256').update(text).digest('hex');

I have tried this lib as it supports UTF-16 with:

var shaObj = new jsSHA(text, "TEXT", "UTF16");

return shaObj.getHash("SHA-256", "HEX");

But again I get a different hash to my C# version! Any ideas why?

In the end this works: return crypto.createHash('sha256').update(text, 'ucs2').digest('hex'); but why does you library return a different hash?

Compiled versions not functional in ExtendScript runtime

I realize this is most likely not a JS runtime that is supported but for those of us seeking to use this in Adobe ExtendScript (any Adobe product that supports JSX scripting) I would like to point out that the compiled versions don't work but the uncompiled version (sha_dev) does.

When I use the compiled versions no errors are thrown but hashes in B64 form look like AABhAAAAAAAAAAAUAAD8AAAAAAA=
where the correct hash was
QpQCkYEOC3GKYtRyoJOZOvFDI4k=

Upgrade path from older version

Hello.
I have some code that references an older version of jsSHA that has differences in the input format.
I am not sure what version the old one is there is no version number in the file but is dated Copyright Brian Turek 2008-2012.

The way the old code calls jsSHA is thus:

var shaObj = new jsSHA(secret, 'ASCII');
var sha1Hex = shaObj.getHash("SHA-1", "HEX");
var hmacObj = new jsSHA(time, 'HEX');
var hmac = hmacObj.getHMAC(sha1Hex, 'HEX', 'SHA-1', "HEX");

Note that the type is ASCII, not TEXT, what should be used in the place of ASCII?

I have changed the constructor calls to the below, but I don't think this is correct as the results are different.

let shaObj = new jssha("SHA-1", "TEXT");
shaObj.update(secret);
let sha1Hex = shaObj.getHash("HEX");
var hmacObj = new jssha("SHA-1","HEX");
hmacObj.setHMACKey(sha1Hex,"HEX");
hmacObj.update(timeOrHash,"TEXT");
var hmac = hmacObj.getHMAC("HEX");

Any hints to upgrade to the new version or should I use the old library, do you think?

Issue with calculating hashes of big files

Hi,

I'm currently having an issue with calculating the SHA-1 of a 512MiB file containing the character 'A'.
If I do it with a 511MiB file containing the character 'A' it works just fine.
I guess it related to some of the internal SHA-1 state variables wrapping at some point.

I'm reproducing this using the jsSHA-2.0.2/test/hash-file.html example on
Chromium Version 49.0.2623.108 Ubuntu 15.10 (64-bit). I also tried Firefox with the same result.

Note: I also discovered that the SHA-512 was off as well...

backwards compatibility question

Hei,

I used to use the old version 1.6.2 of jsSHA

    var shaObj = new jsSHA(value, 'ASCII');
    var hash = shaObj.getHash('SHA-256', 'B64');

now I've upgraded to version 2.2.0

    let shaObj = new jsSHA('SHA-256', 'TEXT');
    shaObj.update(value);
    let hash = shaObj.getHash('HEX');

However, for the same value as input, the generated hash values are different from version to version. Is there anything wrong my usage of jsSHA or it's a break of compatibility between versions?

Thanks in advance!

reading array element outside of the size

Hi,

I would like to submit a bug report.
When trying to get a hash with SHA-256, there is warning about array management.

example line 81 (in sha_dev.js) : bin[byteCnt >>> 2] |= binArr[j] << (24 - (8 * (byteCnt % 4)));

ReferenceError: reference to undefined property bin[(byteCnt >>> 2)]

That line is reading an array at a position above the length.
Is it possible to declare array with the size and init them.

I tried other options and get similar messages: line 91.

Regards,
Serge

Hashtypes other than SHA-512 not working

Hi,

It seems strange, but I am able to create a hash or hmac only if I use the SHA-512 hashType.

console.debug("BEFORE ENCRYPTION:"); // Logs out always

var shaObj = new jsSHA("SHA-512", "TEXT");
shaObj.setHMACKey("abc", "TEXT");
shaObj.update("This is a test");
var hmac = shaObj.getHMAC("HEX");

    console.debug("LOG ENCRYPTED RESULTS:"); // Logs out only if the hashType is SHA-512
    console.debug("shaObj:", shaObj); // Logs out only if the hashType is SHA-512
    console.debug("hmac:", hmac); // Logs out only if the hashType is SHA-512

Am I missing something or is this indeed a bug?

P.S. I use the latest version (2.0.1) and examples from the README.md

Call SHA-1 with BYTES input.

Hi,

Does the package support raw bytes input for SHA-1? This is what I did:
// mybytes is a initialized byte array
var shaObj = new jsSHA(mybytes, "BYTES");
// create the HMAC by using password string as key
var authenticator = shaObj.getHMAC(password, "TEXT", "SHA-1", "BYTES");

Chrome browser says " c.charCodeAt is not a function " upon creation of shaObj on new jsSHA()?

Isn't v1.6 supporting bytes for input and output? Since my inputs are coming from websockets as raw bytes, and the program would need to send the authenticator as also raw bytes array on the wire. It would be less hassle for me to just deal with data in byte array.

Thanks.

SHA-512 always resulting in same

When using your SHA-512 hex methods, and this is the only method I have tested, always results in:

cf7881d5774acbe8533362e0fbc780700267639d87460eda3086cb40e85931b0
717dc95288a023a396bab2c14ce0b5e06fc4fe04eae33e0b91f4d80cbd668bee

Always.

SHA-512 (maybe others) not functioning correctly on Safari

Some examples, using this code (and the demo code on the main site):

var shaObj = new jsSHA(passcat, "ASCII");
var hash = shaObj.getHash("SHA-512", "HEX");

Input:
passcat = "test:test";
Ruby:
8b537fa1b7db5287ab508e3a108f1a7be11058e37476ca6507a574feb02e71e7e70ffb14657beaae3600c86a71048700f4d41e08996ccb5a18686edbe6cf7e27
Chrome:
8b537fa1b7db5287ab508e3a108f1a7be11058e37476ca6507a574feb02e71e7e70ffb14657beaae3600c86a71048700f4d41e08996ccb5a18686edbe6cf7e27
Firefox:
8b537fa1b7db5287ab508e3a108f1a7be11058e37476ca6507a574feb02e71e7e70ffb14657beaae3600c86a71048700f4d41e08996ccb5a18686edbe6cf7e27
Safari:
9e743c45c0a06847e3efaad33664429598f0af24ad43ccf9db18179cc07c3d402adb1309111ac1e89c21dd2d0a5ddff4c06b03c09c8a0b7441dbc72c1009c281

It is consistently wrong. Oddly, subsequent runs in safari return:
fe8a3ee70fa2eab307736063d86b4b1f95e3bcce2310fc723b4558f64a126b9b5bfe27ab0df714d03edfea220faae8955c8592e5051b7ff9230567817304ab83

Code for subsequent runs:
for (i = 0; i < 10; i++ ) {
var shaObj = new jsSHA(passcat, "ASCII");
var hash = shaObj.getHash("SHA-512", "HEX");
/* Print hash */
}

Hashes not matching

I apologize if my question seems easy. I am new to hashing and still learning.

I am using jsSHA to generate a has on my webpage and then I compare it in the backend using VB.net, but the 2 are not matching

Example: When I create a SHA-512 hash of 'testpass' using VB.net and jsSHA the strings don't match

jsSHA: eN3IVVuxZ3/1r3W6X8Asswu1krBhAneuFQVeGJt3/j/aSW5QJ6PZnshdVJQa3uHMF0tQQ4/cIdgtCnn4W1jPRA
VB.NET: eN3IVVuxZ3/1r3W6X8Asswu1krBhAneuFQVeGJt3/j/aSW5QJ6PZnshdVJQa3uHMF0tQQ4/cIdgtCnn4W1jPRA==

What do the 2 extra = on the end mean and why does one have it and the other not?

Thanks,
Jeff

TypeError: c is undefined only with register/change_pwd

I just implemented jsSHA512 in my website and I'm having trouble in the register function. It's working ok with the simple login function.

This is the error I'm getting in the firefox console:

TypeError: c is undefined             sha512.js:22:137
A/c()                                 sha512.js:22
w/this.update()                       sha512.js:14
window.onload/<()                     _display:60
jQuery.event.dispatch()               jquery-2.1.4.js:4434
jQuery.event.add/elemData.handle()    jquery-2.1.4.js:4121

https://jsfiddle.net/pa9sgx3v/2/

$(function () {

    var a       = $('form#login'),
        b       = $('form#register'),
        c       = $('form#change_pwd'),
        shaObj  = new jsSHA('SHA-512', 'TEXT');

    a.on('submit', function (e) {

        e.preventDefault();

        var p = $('#password');

        shaObj.update(p.val());

        var p_hash = shaObj.getHash("HEX");

        p.val(p_hash);

        this.submit()

    });

    b.on('submit', function (e) {

        e.preventDefault();

        var p = $('#reg_pwd'),
            q = $('#confirm_pwd');

        shaObj.update(p.val());
        var p_hash = shaObj.getHash("HEX");
        p.val(p_hash);

        shaObj.update(q.val());
        var q_hash = shaObj.getHash("HEX");
        q.val(q_hash);

        this.submit()

    });

    c.on('submit', function (e) {

        e.preventDefault();

        var p = $('#current_pwd'),
            q = $('#new_pwd'),
            r = $('#confirm_newpwd');

        shaObj.update(p.val());
        var p_hash = shaObj.getHash("HEX");
        p.val(p_hash);

        shaObj.update(q.val());
        var q_hash = shaObj.getHash("HEX");
        q.val(q_hash);

        shaObj.update(r.val());
        var r_hash = shaObj.getHash("HEX");
        r.val(r_hash);

        this.submit()

    })

});

Using ARRAYBUFFER gives a different hash than using TEXT as input type

Hello,

I'm trying to use jsSHA to hash a file content.
In my code I drop a file into a div element within an HTML page and use the following code to read the file and hash its contents.
In this test case I'm using jsSHA twice, the first time by passing in an ARRAYBUFFER, the second time by converting the file content to a string and then using jsSHA by passing in a TEXT.

  var dataTransfer =  e.originalEvent.dataTransfer;
  if (dataTransfer && dataTransfer.files.length) { 
    e.preventDefault();
    e.stopPropagation();
    var shaObj = null;
    var hash = null;
    $.each(dataTransfer.files, function(i, file) {
      var fr = new FileReader(); 
      fr.readAsArrayBuffer(file);
      fr.onload = function (e) {
        var ab = fr.result;
        shaObj = new jsSHA("SHA-256", "ARRAYBUFFER");
        shaObj.update(ab);
        hash = shaObj.getHash("HEX");
        $("#holder").append("<p class='fixed'>Length of byte array: " + ab.byteLength + "</p>");
        $("#holder").append("<p class='fixed'>SHA-256(ab): " + hash + "</p>");

        var tx = String.fromCharCode.apply(null, new Uint8Array(ab));
        shaObj = new jsSHA("SHA-256", "TEXT");
        shaObj.update(tx);
        hash = shaObj.getHash("HEX");
        $("#holder").append("<p class='fixed'>String: " + tx + "</p>");
        $("#holder").append("<p class='fixed'>SHA-256(tx): " + hash + "</p>");
      }
    });
  }

The output of my page is as follows:

Length of byte array: 4
SHA-256(ab): df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119

String: test
SHA-256(tx): 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

Why are the two hashes different? The correct one is the second.
The file (file name: f1) is a binary file containing 4 characters as follows: test

$ openssl sha256 f1
SHA256(f1)= 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

SHA1 Hash is wrong if string contains german umlate "ÜÄÖ..."

Hi,
I have the following string:

AMOUNT=12300x3hwihY-ci]iÜk_mCURRENCY=EURx3hwihY-ci]iÜk_mLANGUAGE=de_DEx3hwihY-ci]iÜk_mORDERID=G-20130128.1206x3hwihY-ci]iÜk_mPSPID=fotoschoberx3hwihY-ci]iÜk*m

the hash for this should be: f060a54453131220943e994f7ee7e56482b960fa
but the function returns: 5bf83164e0740446c788ef08d4afc254b171e286

If I do a manual base64 encoding of the string then I get the right hash... So I think there is a problem with german characters link "äöüÖÄÜ"...

outputFormat must be HEX, B64, or BYTES

Pleaseeeeeeeeeeee
can you be so kind to tell what is wrong in this line:

headerParameters.oauth_signature = encodeURIComponent(oauthSignatureObject.getHMAC(encodeURIComponent(secretKey) + "&" + encodedTokenSecret, ["TEXT", "SHA-1", "B64"]));

My (ionic2) app break exactly there throwing this error:

outputFormat must be HEX, B64, or BYTES

screenshot

I have trying modifying everything but nothing works.

I am following this tutorial for twitter integration with Ionic2:
http://blog.ionic.io/twitter-app-ionic-2/

By the way it seems that we are force to use jssha 1.6 because ngCordovaAuth doesn't support jssha v2.
But now it seems also that DefinitelyTyped doesn't support jssha 1.6.

Shoud I start using drugs or there is actually a solution?

Thanks!

2 rounds error?

I'm getting bad results for sha512 or am I missing something? I'm doing 2 rounds.

test -> faadcaf60afd35dfcdb5e9ea0d0a0531f6338c62187cff37a1efe11f1d41a34879731bc9db49df75aecf5d582ad09b5c6ded2d86bd1f07c11bd755d1fccc81fe

Manually:

test ->
ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff ->
5797448d5c775085bc82b808b9d01bb4ef795dfd05ea377d9b537d5e3495179f0b0c03f372518209db3dfbdae47c8529412ce9154dd1c5b99932cdf3b079c125

problem using with Browserify

Hi,

Thanks for this great library! I'm using this with browserify and have the following problem:

when I call

var jsSHA = require('jssha')
var hash = new jsSHA(inputString, 'TEXT').getHash('SHA-256', 'HEX')
console.log(hash);

it returns the error message:

Chosen SHA variant is not supported

In the package main file, script is src/sha.js and the README says it is the bundle. What do I miss?

Thanks,
Ge

Use native implementation when possible

Native browsers support for Web Cryptography evolved quite well, would be nice to use native code when possible and fallback to current implementation of SHA2 when not.
Here is a bit of details about hashing in particular: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
Here is browsers support: http://caniuse.com/#search=web%20cryptography

What really bother me a bit is that there is no native support at all in IE10, but since it will go away with IE11 and Edge being two latest versions, pretty much all visitors might take advantage of higher performance by using something that is already in their browsers.

Hope you'll have time to adopt it.

Add support for calling update on hash object

It is useful, for example when working with streams, to be able to incrementally call update on the hash object with a chunk of data instead of the whole.

This would allow you, for example, to calculate the hash of a big file without having to store the whole file in memory.

Is there interest in supporting this feature in the library?

I am interested in having this feature to implement encrypted streaming in openpgp.js that uses your library: openpgpjs/openpgpjs#173 openpgpjs/openpgpjs#260.

Can you update the shaObj multiple times?

Hi,

Is it possible to reuse the shaObj declared the following way and just .update with new strings?

Thanks in advance!
Ge

vadr sha512 = new jssha('SHA-512', 'TEXT')
//inside function
function getHash(string){
   sha512.update(string)
   return sha512.getHash('HEX')
}

Uncompressed SHA1

Hi can we have uncompressed version of SHA1.js please

In firefox addon the on initialize unobfoscation is a disturbing overhead to some users please.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.