7
7
8
8
⚠️ ** WARNING: This projects is under active development. Don't use it until a stable version is released.** ⚠️
9
9
10
- This npm package contains all the cryptographic primitives normally used when
10
+ This npm package contains all the cryptographic primitives normally used when
11
11
developing Javascript/TypeScript applications and tools for Ethereum.
12
12
13
13
Pure Javascript implementations of all the primitives are included, so it can
@@ -24,6 +24,7 @@ The cryptographic primitives included are:
24
24
* ` pbkdf2 `
25
25
* ` sha256 `
26
26
* ` ripemd160 `
27
+ * ` aes `
27
28
* ` secp256k1 `
28
29
29
30
## Installation
@@ -42,15 +43,15 @@ $ yarn add ethereum-cryptography
42
43
43
44
## Usage
44
45
45
- There's a submodule available for each cryprographic primitive.
46
+ There's a submodule available for each cryprographic primitive.
46
47
47
- No ` index.js ` /` main ` is provided, as that would lead to huge bundles when using
48
+ No ` index.js ` /` main ` is provided, as that would lead to huge bundles when using
48
49
this package for the web.
49
50
50
51
## keccak submodule
51
52
52
- The ` keccack ` submodule has four functions that receive a ` Buffer ` with the
53
- message to hash, and return a ` Buffer ` with the hash. These are ` keccak224 ` ,
53
+ The ` keccack ` submodule has four functions that receive a ` Buffer ` with the
54
+ message to hash, and return a ` Buffer ` with the hash. These are ` keccak224 ` ,
54
55
` keccak256 ` , ` keccak384 ` , and ` keccak512 ` .
55
56
56
57
### Function types
@@ -82,8 +83,8 @@ as it will block its main thread and hang your ui.
82
83
83
84
### Password encoding
84
85
85
- Encoding passwords is a frequent source of errors. Please read
86
- [ these notes] ( https://github.com/ricmoo/scrypt-js/tree/0eb70873ddf3d24e34b53e0d9a99a0cef06a79c0#encoding-notes )
86
+ Encoding passwords is a frequent source of errors. Please read
87
+ [ these notes] ( https://github.com/ricmoo/scrypt-js/tree/0eb70873ddf3d24e34b53e0d9a99a0cef06a79c0#encoding-notes )
87
88
before using this submodule.
88
89
89
90
### Function types
@@ -192,10 +193,97 @@ const { ripemd160 } = require("ethereum-cryptography/ripemd160");
192
193
console .log (ripemd160 (Buffer .from (" message" , " ascii" )).toString (" hex" ));
193
194
```
194
195
196
+ ## ripemd160 submodule
197
+
198
+ The ` ripemd160 ` submodule contains a single functions implementing the
199
+ ` ripemd160 ` hash algorithm.
200
+
201
+ ### Function types
202
+
203
+ ``` ts
204
+ function ripemd160(msg : Buffer ): Buffer ;
205
+ ```
206
+
207
+ ### Example usage
208
+
209
+ ``` js
210
+ const { ripemd160 } = require (" ethereum-cryptography/ripemd160" );
211
+
212
+ console .log (ripemd160 (Buffer .from (" message" , " ascii" )).toString (" hex" ));
213
+ ```
214
+
215
+ ## AES submodule
216
+
217
+ The ` aes ` submodule contains encryption and decryption functions implementing
218
+ the [ AES algorithm] ( https://en.wikipedia.org/wiki/Advanced_Encryption_Standard ) .
219
+
220
+ ### Function types
221
+
222
+ ``` ts
223
+ function encrypt(msg : Buffer , key : Buffer , iv : Buffer , mode : string , pkcs7PaddingEnabled = true ): Buffer ;
224
+
225
+ function decrypt(cypherText : Buffer , key : Buffer , iv : Buffer , mode : string , pkcs7PaddingEnabled = true ): Buffer
226
+ ```
227
+
228
+ ### Operation modes
229
+
230
+ This submodules works with different AES
231
+ [modes of operation ](https :// en.wikipedia.org/wiki/Block_cipher_mode_of_operation).
232
+ To choose one of them , you should pass the ` mode ` parameter a string with the
233
+ same format as OpenSSL and Node use . You can take a look at them by running
234
+ ` openssl list -cipher-algorithms ` .
235
+
236
+ In Node , any mode that its OpenSSL version supports can be used .
237
+
238
+ In the browser we test it to work with the modes that are normally used in
239
+ Ethereum libraries and applications . Those are ` aes-128-ctr ` , ` aes-126-cbc ` , and
240
+ ` aes-256-cbc ` , but other modes may work .
241
+
242
+ ### Encrypting with passwords
243
+
244
+ AES is not supposed to be used directly with a password . Doing that will
245
+ compromise your users ' security.
246
+
247
+ The ` key ` parameters in this submodule are meant to be strong cryptographic
248
+ keys . If you want to obtain such a key from a password , please use a
249
+ [key derivation function ](https : // en.wikipedia.org/wiki/Key_derivation_function)
250
+ like [pbkdf2 ](#pbkdf2 -submodule ) or [scrypt ](#scrypt -submodule ).
251
+
252
+ ### Padding plaintext messages
253
+
254
+ Some operation modes require the plaintext message to be a multiple of ` 16 ` . If
255
+ that isn ' t the case, your message has to be padded.
256
+
257
+ By default , this module automatically pads your messages according to [PKCS #7 ](https : // tools.ietf.org/html/rfc2315).
258
+ Note that this padding scheme always adds at least 1 byte of paddding . If you
259
+ are unsure what anything of this means , we **strongly ** recommend you to use
260
+ the defaults.
261
+
262
+ If you need to encrypt without padding , or want to use another padding scheme ,
263
+ you can disable PKCS #7 padding by passing ` false ` as the last argument and
264
+ handling padding yourself. Note that if you do this and your operation mode
265
+ requires padding , ` encrypt ` will throw if your plaintext message isn ' t a
266
+ multiple of ` 16.
267
+
268
+ ### Example usage
269
+
270
+ ` ` ` js
271
+ const { encrypt } = require (" ethereum-cryptography/aes" );
272
+
273
+ console.log(
274
+ encrypt (
275
+ Buffer.from(" message" , " ascii" ),
276
+ Buffer.from(" 2b7e151628aed2a6abf7158809cf4f3c" , " hex" ),
277
+ Buffer.from(" f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff" , " hex" ),
278
+ "aes-128-cbc"
279
+ ).toString(" hex" )
280
+ );
281
+ ` ` `
282
+
195
283
## secp256k1 submodule
196
284
197
- The ` secp256k1 ` submodule has the same API than the native module
198
- [ ` secp256k1 ` from cryptocoinjs] ( https://github.com/cryptocoinjs/secp256k1-node )
285
+ The ` secp256k1 ` submodule has the same API than the native module
286
+ [ ` secp256k1 ` from cryptocoinjs](https://github.com/cryptocoinjs/secp256k1-node)
199
287
version ` 3.x ` , but it's backed by [ ` elliptic ` ](https://www.npmjs.com/package/elliptic).
200
288
201
289
### Function types
@@ -227,7 +315,7 @@ tested with `webpack`, `Rollup`, `Parcel`, and `Browserify`.
227
315
228
316
For using it with ` Rollup ` you need to use these plugins:
229
317
[ ` rollup - plugin - node - builtins ` ](https://www.npmjs.com/package/rollup-plugin-node-builtins),
230
- [ ` rollup-plugin-node-globals ` ] ( https://www.npmjs.com/package/rollup-plugin-node-globals ) ,
318
+ [ ` rollup - plugin - node - globals ` ](https://www.npmjs.com/package/rollup-plugin-node-globals),
231
319
and [ ` rollup - plugin - json ` ](https://www.npmjs.com/package/rollup-plugin-json).
232
320
233
321
## Opt-in native implementations (Node.js only)
@@ -237,11 +325,11 @@ If you are using this package in Node, you can install
237
325
to opt-in to use native implementations of some of the cryptographic primitives
238
326
provided by this package.
239
327
240
- No extra work is needed for this to work. This package will detect that
328
+ No extra work is needed for this to work. This package will detect that
241
329
` ethereum - cryptography - native ` is installed, and use it.
242
330
243
- While installing ` ethereum-cryptography-native ` will generally improve the
244
- performance of your application, we recommend leaving the decision of installing
331
+ While installing ` ethereum - cryptography - native ` will generally improve the
332
+ performance of your application, we recommend leaving the decision of installing
245
333
it to your users. It has multiple native dependencies that need to be compiled,
246
334
and this can be problematic in some environments.
247
335
0 commit comments