- TypeScript interfaces. #6, #9
- HMAC feature.
- support for web worker. #14
### Fixed
- deprecated `new Buffer`, replace with `Buffer.from`. #10
- dependencies and security issues.
- refactor: simplify formatMessage internal logic.
- Generates incorrect hash in some cases.
### Changed
- remove `eval` and use `require` directly. #8
- throw error by Error oject.
- throw error if update after finalize
- use unsigned right shift.
@ -20,83 +21,74 @@ For node.js, you can use this command to install:
npm install js-sha1
## Notice
NIST formally deprecated use of SHA-1 in 2011 and disallowed its use for digital signatures in 2013, and declared that it should be phased out by 2030. However, SHA-1 is still secure for HMAC. [wiki](https://en.wikipedia.org/wiki/SHA-1)
## Usage
You could use like this:
```JavaScript
sha1('Message to hash');
```
If you use node.js, you should require the module first:
```JavaScript
sha1 = require('js-sha1');
```
## Example
Code
```JavaScript
sha1('');
sha1('The quick brown fox jumps over the lazy dog');
sha1('The quick brown fox jumps over the lazy dog.');
```
Output
var hash = sha1.create();
hash.update('Message to hash');
hash.hex();
da39a3ee5e6b4b0d3255bfef95601890afd80709
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
408d94384216f890ff7a0c3528e8bed1e0b01621
// HMAC
sha1.hmac('key', 'Message to hash');
It also supports UTF-8 encoding:
Code
```JavaScript
sha1('中文');
var hash = sha1.hmac.create('key');
hash.update('Message to hash');
hash.hex();
```
Output
7be2d2d20c106eee0836c9bc2b939890a78e8fb3
It also supports byte Array or Uint8Array input:
Code
### Node.js
If you use node.js, you should require the module first:
```JavaScript
sha1([]);
sha1(new Uint8Array([]));
var sha1 = require('js-sha1');
```
Output
da39a3ee5e6b4b0d3255bfef95601890afd80709
da39a3ee5e6b4b0d3255bfef95601890afd80709
## Benchmark
[UTF8](http://jsperf.com/sha1-speed-test/11)
[ASCII](http://jsperf.com/sha1-speed-test/10)
## Extensions
### jQuery
If you prefer jQuery style, you can add following code to add a jQuery extension.
Code
```JavaScript
jQuery.sha1 = sha1;
### TypeScript
If you use TypeScript, you can import like this:
```TypeScript
import { sha1 } from 'js-sha1';
```
And then you could use like this:
```JavaScript
$.sha1('message');
```
### Prototype
If you prefer prototype style, you can add following code to add a prototype extension.
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12':'The quick brown fox jumps over the lazy dog',
'408d94384216f890ff7a0c3528e8bed1e0b01621':'The quick brown fox jumps over the lazy dog.'
},
'ascii more than 64 bytes':{
'8690faab7755408a03875895176fac318f14a699':'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.'
'20cc8fef058da244b90ed814ab1b6d9d5b4796bf':'Tehtäväsi on muotoilla teksti, jossa käyttäjälle suositellaan hänen henkilökohtaisiin tietoihinsa perustuen avioehdon tekemistä. Suosittelemme nimenomaan tätä tuotetta käyttäjän henkilökohtaisista tiedoista johtuen. Tuottamasi tekstin tulee olla lyhyt, persoonallinen, ymmärrettävä ja todenmukainen. Suositus perustuu seuraaviin seikkoihin:\n\nSeikka 0: Avioehdon tekeminen on verohyötyjen vuoksi suositeltavaa, koska olet ilmaissut että haluat kuoleman tapauksessa turvata ensisijaisesti aviopuolisosi asemaa (ennemmin kuin lasten asemaa). Avioehdolla voit määrätä, että puolet kuolleen puolison omaisuudesta siirtyy verovapaasti leskelle.\n\nVoit halutessasi hyödyntää seuraavia tietoja tekstin muotoilussa persoonallisemmaksi: Käyttäjällä on kumppani nimeltä PARTNER_NAME. Käyttäjällä on suurperhe. Käyttäjällä on lapsiperhe.\n\nPuhuttele käyttäjää siihen sävyyn että tiedät jo mitä käyttäjä haluaa, koska hän on kertonut toiveistaan, ja niiden perusteella tehty suositus on ilmiselvä. Joten älä käytä epävarmoja ilmaisuja kuten "halutessasi", vaan kirjoita itsevarmoilla ilmaisuilla kuten "koska haluat". Älä tee oletuksia käyttäjän sukupuolesta, emme tiedä onko hän mies vai nainen. Älä käytä sanaa jälkikasvu, puhu ennemmin lapsesta tai lapsista riippuen onko lapsia yksi vai useampia. Älä puhuttele käyttäjää ensimmäisessä persoonassa, käytä ennemmin passiivimuotoa. Tekstin sävyn tulisi olla neutraalin asiallinen, ei melodramaattinen eikä leikkisä. Pysy totuudessa, älä keksi uusia seikkoja yllä listattujen lisäksi. Viittaa ihmisiin nimillä silloin kun se on mahdollista. Tekstin tulisi olla vain muutaman lauseen mittainen. Älä siis kirjoita pitkiä selityksiä äläkä kirjoita listoja. Tiivistä oleellinen tieto lyhyeksi ja persoonalliseksi tekstiksi.'
expect(sha1('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('8690faab7755408a03875895176fac318f14a699');