File tree Expand file tree Collapse file tree 5 files changed +23
-20
lines changed
packages/ethereum-cryptography/src Expand file tree Collapse file tree 5 files changed +23
-20
lines changed Original file line number Diff line number Diff line change
1
+ import { Hash } from "crypto" ;
2
+
3
+ export function createHashFunction (
4
+ hashConstructor : ( ) => Hash
5
+ ) : ( msg : Buffer ) => Buffer {
6
+ return msg => {
7
+ const hash = hashConstructor ( ) ;
8
+ hash . update ( msg ) ;
9
+ return Buffer . from ( hash . digest ( ) ) ;
10
+ } ;
11
+ }
Original file line number Diff line number Diff line change 1
1
const { ripemd160 : Ripemd160 } = require ( "hash.js/lib/hash/ripemd" ) ;
2
2
3
- export function ripemd160 ( msg : Buffer ) : Buffer {
4
- const hash = new Ripemd160 ( ) ;
5
- hash . update ( msg ) ;
3
+ import { createHashFunction } from "../hash-utils" ;
6
4
7
- return Buffer . from ( hash . digest ( ) ) ;
8
- }
5
+ export const ripemd160 = createHashFunction ( ( ) => new Ripemd160 ( ) ) ;
Original file line number Diff line number Diff line change 1
1
const Sha256Hash = require ( "hash.js/lib/hash/sha/256" ) ;
2
2
3
- export function sha256 ( msg : Buffer ) : Buffer {
4
- const hash = new Sha256Hash ( ) ;
5
- hash . update ( msg ) ;
3
+ import { createHashFunction } from "../hash-utils" ;
6
4
7
- return Buffer . from ( hash . digest ( ) ) ;
8
- }
5
+ export const sha256 = createHashFunction ( ( ) => new Sha256Hash ( ) ) ;
Original file line number Diff line number Diff line change 1
1
import crypto from "crypto" ;
2
2
3
- export function ripemd160 ( msg : Buffer ) : Buffer {
4
- const hash = crypto . createHash ( "ripemd160" ) ;
5
- hash . update ( msg ) ;
6
- return Buffer . from ( hash . digest ( ) ) ;
7
- }
3
+ import { createHashFunction } from "./hash-utils" ;
4
+
5
+ export const ripemd160 = createHashFunction ( ( ) =>
6
+ crypto . createHash ( "ripemd160" )
7
+ ) ;
Original file line number Diff line number Diff line change 1
1
import crypto from "crypto" ;
2
2
3
- export function sha256 ( msg : Buffer ) : Buffer {
4
- const hash = crypto . createHash ( "sha256" ) ;
5
- hash . update ( msg ) ;
6
- return Buffer . from ( hash . digest ( ) ) ;
7
- }
3
+ import { createHashFunction } from "./hash-utils" ;
4
+
5
+ export const sha256 = createHashFunction ( ( ) => crypto . createHash ( "sha256" ) ) ;
You can’t perform that action at this time.
0 commit comments