# CryptoES
*A cryptography algorithms library compatible with ES6 and TypeScript*
- Inspired by and has the same API with [CryptoJS](https://code.google.com/archive/p/crypto-js/)
- With types for TypeScript usage
- Witten in latest ECMAScript Standard
- Support ES6 module import and partially import
## Usage
Installation:
```
yarn add crypto-es
```
---
In Node.js projects, we recommend you to use ECMAScript Modules insead of CommonJS:
```
// package.json
{
"type": "module"
}
```
```
# In same folder as above package.json
node --experimental-modules my-app.js # Runs as ES module
```
[See details](<https://nodejs.org/dist/latest-v12.x/docs/api/esm.html>)
---
Then you can import CryptoES:
```
import CryptoES from 'crypto-es';
const rst = CryptoES.MD5("Message").toString();
```
Or partially import the function to reduce the package weight:
```
import { MD5 } from 'crypto-es/lib/md5.js';
const rst = MD5("Message").toString();
```
## TypeScript Usage
Make sure to add this entry to your tsconfig.json:
```
{
"compilerOptions": {
...
"skipLibCheck": true,
...
}
}
```
## Guide
> Just the same as [CryptoJS](https://code.google.com/archive/p/crypto-js/)
---
- [Hashers](###Hashers)
- [HMAC](#HMAC)
- [Ciphers](#Ciphers)
- [Encoders](#Encoders)
- [ArrayBuffer and TypedArray](#ArrayBuffer-and-TypedArray)
---
### Hashers
#### The Hasher Algorithms
**MD5**
MD5 is a widely used hash function. It's been used in a variety of security applications and is also commonly used to check the integrity of files. Though, MD5 is not collision resistant, and it isn't suitable for applications like SSL certificates or digital signatures that rely on this property.
```
const hash = CryptoES.MD5("Message");
```
**SHA-1**
The SHA hash functions were designed by the National Security Agency (NSA). SHA-1 is the most established of the existing SHA hash functions, and it's used in a variety of security applications and protocols. Though, SHA-1's collision resistance has been weakening as new attacks are discovered or improved.
```
const hash = CryptoES.SHA1("Message");
```
**SHA-2**
SHA-256 is one of the four variants in the SHA-2 set. It isn't as widely used as SHA-1, though it appears to provide much better security.
```
const hash = CryptoES.SHA256("Message");
```
SHA-512 is largely identical to SHA-256 but operates on 64-bit words rather than 32.
```
const hash = CryptoES.SHA512("Message");
```
CryptoES also supports SHA-224 and SHA-384, which are largely identical but truncated versions of SHA-256 and SHA-512 respectively.
**SHA-3**
SHA-3 is the winner of a five-year competition to select a new cryptographic hash algorithm where 64 competing designs were evaluated.
**NOTE:** I made a mistake when I named this implementation SHA-3. It should be named Keccak[c=2d]. Each of the SHA-3 functions is based on an instance of the Keccak algorithm, which NIST selected as the winner of the SHA-3 competition, but those SHA-3 functions won't produce hashes identical to Keccak.
```
const hash = CryptoES.SHA3("Message");
```
SHA-3 can be configured to output hash lengths of one of 224, 256, 384, or 512 bits. The default is 512 bits.
```
const hash = CryptoES.SHA3("Message", { outputLength: 512 });
const hash = CryptoES.SHA3("Message", { outputLength: 384 });
const hash = CryptoES.SHA3("Message", { outputLength: 256 });
const hash = CryptoES.SHA3("Message", { outputLength: 224 });
```
**RIPEMD-160**
```
const hash = CryptoES.RIPEMD160("Message");
```
#### The Hasher Input
The hash algorithms accept either strings or instances of CryptoES.lib.WordArray. A WordArray object represents an array of 32-bit words. When you pass a string, it's automatically converted to a WordArray encoded as UTF-8.
#### The Hasher Output
The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.
```
const hash = CryptoES.SHA256("Message");
alert(typeof hash); // object
alert(hash); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
```
You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.
```
const hash = CryptoES.SHA256("Message");
alert(hash.toString(CryptoES.enc.Base64)); // L3dmip37+NWEi57rSnFFypTG7ZI25Kdz9tyvpRMrL5E= alert(hash.toString(CryptoES.enc.Latin1)); // /wf��ûøÕ���ëJqEÊ�Æí�6ä§söܯ¥+/�
alert(hash.toString(CryptoES.enc.Hex)); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
```
#### Progressive Hashing
```
const sha256 = CryptoES.algo.SHA256.create();
sha256.update("Message Part 1");
sha256.update("Message Part 2");
sha256.update("Message Part 3");
const hash = sha256.finalize();
```
### HMAC
Keyed-hash message authentication codes (HMAC) is a mechanism for message authentication using cryptographic hash functions.
HMAC can be used in combination with any iterated cryptographic hash function.
```
const hash = CryptoES.HmacMD5("Message", "Secret Passphrase");
const hash = CryptoES.HmacSHA1("Message", "Secret Passphrase");
const hash = CryptoES.HmacSHA256("Message", "Secret Passphrase");
const hash = CryptoES.HmacSHA512("Message", "Secret Passphrase");
```
#### Progressive HMAC Hashing
```
const hmac = CryptoES.algo.HMAC.create(CryptoES.algo.SHA256, "Secret Passphrase");
hmac.update("Message Part 1");
hmac.update("Message Part 2");
hmac.update("Message Part 3");
const hash = hmac.finalize();
```
### PBKDF2
PBKDF2 is a password-based key derivation function. In many applications of cryptography, user security is ultimately dependent on a password, and because a password usually can't be used directly as a cryptographic key, some processing is required.
A salt provides a large set of keys for any given password, and an iteration count increases the cost of producing keys from a password, thereby also increasing the difficulty of attack.
```
const salt = CryptoES.lib.WordArray.random(128/8);
const key128Bits = CryptoES.PBKDF2("Secret Passphrase", salt, { keySize: 128/32 });
const key256Bits = CryptoES.PBKDF2("Secret Passphrase", salt, { keySize: 256/32 });
const key512Bits = CryptoES.PBKDF2("Secret Passphrase", salt, { keySize: 512/32 });
const key512Bits1000Iterations = CryptoES.PBKDF2("Secret Passphrase", salt, { keySize: 512/32, iterations: 1000 });
```
### Ciphers
#### The Cipher Algorithms
**AES**
The Advanced Encryption Standard (AES) is a U.S. Federal Information Processing Standard (FIPS). It was selected after a 5-year process where 15 competing designs were evaluated.
```
const encrypted = CryptoES.AES.encrypt("Message", "Secret Passphrase");
const decrypted = CryptoES.AES.decrypt(encrypted, "Secret Passphrase");
```
CryptoES supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.
**DES, Triple DES**
DES is a previously dominant algorithm for encryption, and was published as an official Federal Information Processing Standard (FIPS). DES is now considered to be insecure due to the small key size.
```
const encrypted = CryptoES.DES.encrypt("Message", "Secret Passphrase");
const decrypted = CryptoES.DES.decrypt(encrypted, "Secret Passphrase");
```
Triple DES applies DES three times to each block to increase the key size. The algorithm is believed to be secure in this form.
```
const encrypted = CryptoES.TripleDES.encrypt("Message", "Secret Passphrase");
const decrypted = CryptoES.TripleDES.decrypt(encrypted, "Secret Passphrase");
```
**Rabbit**
Rabbit is a high-performance stream cipher and a finalist in the eSTREAM Portfolio. It is one of the four designs selected after a 3 1/2-year process where 22 designs were evaluated.
```
const encrypted = CryptoES.Rabbit.encrypt("Message", "Secret Passphrase");
const decrypted = CryptoES.Rabbit.decrypt(encrypted, "Secret Pas