Skip to content

Commit

Permalink
A new consolidated version including all the hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeByZach committed Jul 28, 2021
1 parent 42fba0d commit 616fa0d
Show file tree
Hide file tree
Showing 21 changed files with 2,418 additions and 147 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test results
/test/*.html
95 changes: 58 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jhash
Hash.js
====
[![Latest Release](https://img.shields.io/github/tag/CodeByZach/jhash.svg?label=release)](https://github.com/CodeByZach/jhash/releases/latest) [![Stars](https://img.shields.io/github/stars/CodeByZach/jhash.svg)](https://github.com/CodeByZach/jhash/stargazers) [![Forks](https://img.shields.io/github/forks/CodeByZach/jhash.svg)](https://github.com/CodeByZach/jhash/network/members) [![License](https://img.shields.io/github/license/CodeByZach/jhash.svg)](LICENSE)

A JavaScript hash generator.

Expand All @@ -13,63 +14,83 @@ A JavaScript hash generator.
Common Usage
-------

First [download the bundle](https://github.com/CodeByZach/hash.js/releases/tag/2.2.0). For common use, I recommend using the minified scripts, which have been processed by the YUI Compressor. Note that these only support utf-8 input and hex output. Use it like this:

```html
<script type="text/javascript" src="md5-min.js"></script>
<script type="text/javascript">
var hash = hex_md5("string");
var hmac = hex_hmac_md5("key", "data");
</script>
```
<script src="https://cdn.jsdelivr.net/gh/codebyzach/hash.js@latest/hash.js"></script>
<script>
var md5 = Hash.hex_md5("string");
var md5_hmac = Hash.hex_hmac_md5("key", "data");
The usage for other hashes is essentially the same - `sha1`, `sha256`, `sha512` or `rmd160`.
var sha1_hash = Hash.hex_sha1("string");
var sha1_hmac = Hash.hex_hmac_sha1("key", "data");
var sha256_hash = Hash.hex_sha256("string");
var sha256_hmac = Hash.hex_hmac_sha256("key", "data");
var sha512_hash = Hash.hex_sha512("string");
var sha512_hmac = Hash.hex_hmac_sha512("key", "data");
var rmd160_hash = Hash.hex_rmd160("string");
var rmd160_hmac = Hash.hex_hmac_rmd160("key", "data");
</script>
```

Other Output Encodings
-------

The scripts support base64 encoding, although you must use the full script, not the minified version. If necessary, you can create you own minified script, with the functionality you need. Use it like this:
The scripts support `base64` encoding. Use it like this:

```html
<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
var hash = b64_md5("string");
var hmac = b64_hmac_md5("key", "data");
</script>
```javascript
var md5 = Hash.b64_md5("string");
var md5_hmac = Hash.b64_hmac_md5("key", "data");

var sha1_hash = Hash.b64_sha1("string");
var sha1_hmac = Hash.b64_hmac_sha1("key", "data");

var sha256_hash = Hash.b64_sha256("string");
var sha256_hmac = Hash.b64_hmac_sha256("key", "data");

var sha512_hash = Hash.b64_sha512("string");
var sha512_hmac = Hash.b64_hmac_sha512("key", "data");

var rmd160_hash = Hash.b64_rmd160("string");
var rmd160_hmac = Hash.b64_hmac_rmd160("key", "data");
```

There is also a mode called "any output encoding". This lets you specify a string of characters, and all those characters will be used to encode the password. The string can be any length - it does not need to be a power of 2. This is useful for applications like password generation, when you want to get as much unpredictability as possible into a short password. Use it like this:

```html
<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
var hash = any_md5("string", "encoding");
</script>
```javascript
var md5 = Hash.any_md5("string");
var md5_hmac = Hash.any_hmac_md5("key", "data");

var sha1_hash = Hash.any_sha1("string");
var sha1_hmac = Hash.any_hmac_sha1("key", "data");

var sha256_hash = Hash.any_sha256("string");
var sha256_hmac = Hash.any_hmac_sha256("key", "data");

var sha512_hash = Hash.any_sha512("string");
var sha512_hmac = Hash.any_hmac_sha512("key", "data");

var rmd160_hash = Hash.any_rmd160("string");
var rmd160_hmac = Hash.any_hmac_rmd160("key", "data");
```

If the encoding is `0123456789ABCDEF` the output will be identical to `hex_md5`. It isn't possible to create output that's identical to base64 encoding.
If the encoding is `0123456789ABCDEF` the output will be identical to `hex_md5`. It isn't possible to create output that's identical to `base64` encoding.


Advanced Usage
-------

If you want to use more advanced features, such as multiple repetitions of a hash, or utf-16 encoding, you need to use a slightly lower-level interface to the scripts. These have the concept of a "raw string"; this is a JavaScript string, but all the characters are between 0 and 255 - essentially a binary array. To get a hex hash, using utf-16 encoding:
If you want to use more advanced features, such as multiple repetitions of a hash, or `utf-16` encoding, you need to use a slightly lower-level interface to the scripts. These have the concept of a "raw string"; this is a JavaScript string, but all the characters are between 0 and 255 - essentially a binary array. To get a hex hash, using `utf-16` encoding:

```html
<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
var hash = rstr2hex(rstr_md5(str2rstr_utf16le("string")));
</script>
```javascript
var hash = Hash.rstr2hex(Hash.rstr_md5(Hash.str2rstr_utf16le("string")));
```

You can also use `str2rstr_utf16be`. To perform a double hash:
You can also use `Hash.str2rstr_utf16be`. To perform a double hash:

```html
<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
var hash = rstr2hex(rstr_md5(rstr_md5(str2rstr_utf8("string"))));
</script>
```javascript
var hash = Hash.rstr2hex(Hash.rstr_md5(Hash.rstr_md5(Hash.str2rstr_utf8("string"))));
```

You can use variants of this to produce just about any hash you may need.
Expand All @@ -78,10 +99,10 @@ You can use variants of this to produce just about any hash you may need.
Unit Tests
-------

To run the unit tests, you will need Python 2.5 or newer. The script `test.py` generates an HTML file that runs the tests:
To run the unit tests, you will need Python 2 or newer. The script `test.py` generates an HTML file that runs the tests:

```bash
python test.py > test.html
```

Next, open `test.html` in a browser to run the tests, and see the results. If you want to test the minified versions of the scripts, use `test-min.py`.
Next, open `test.html` in a browser to run the tests, and see the results.

0 comments on commit 616fa0d

Please sign in to comment.