1
- import * as child from 'child_process' ;
1
+ import * as crypto from 'crypto' ;
2
+ import * as fs from 'fs' ;
2
3
import electronLog from 'electron-log' ;
3
4
import { IChecksum , ChecksumAlgorithm } from './Database' ;
4
5
export default class Checksum {
6
+ /**
7
+ * @function fileHash
8
+ * @param filepath
9
+ * @param algorithm
10
+ * @desc Creates the hash of a file with a specufued hash algorithm. Uses Node's crypto module.
11
+ * @return {Promise.string } The calculation result
12
+ */
13
+ static fileHash ( filepath , algorithm ) : Promise < string > {
14
+ return new Promise ( ( resolve , reject ) => {
15
+ if ( ! filepath ) {
16
+ reject ( new Error ( ) ) ;
17
+ }
18
+
19
+ let hashing = crypto . createHash ( algorithm ) ;
20
+ try {
21
+ let stream = new fs . ReadStream ( filepath ) ;
22
+ stream . on ( 'data' , function ( data ) {
23
+ hashing . update ( data ) ;
24
+ } ) ;
25
+ stream . on ( 'end' , function ( ) {
26
+ const hash = hashing . digest ( 'hex' ) ;
27
+ electronLog . info (
28
+ `Computed ${ algorithm } of ${ filepath } result: ${ hash } ` ,
29
+ ) ;
30
+ return resolve ( hash ) ;
31
+ } ) ;
32
+ } catch ( error ) {
33
+ electronLog . error (
34
+ `Error while computing ${ algorithm } of ${ filepath } : ${ error } ` ,
35
+ ) ;
36
+ return reject ( 'calc fail' ) ;
37
+ }
38
+ } ) ;
39
+ }
40
+
5
41
/**
6
42
* @function sha512
7
43
* @param {string } filepath: path to the file
8
44
* @desc Computes sha512 hash of the given file using shasum
9
45
* @return {Promise.string } The calculation result
10
46
*/
11
47
public static sha512 ( filepath : string ) : Promise < string > {
12
- return new Promise < string > ( ( resolve , reject ) => {
13
- child . exec (
14
- 'shasum -a 512 ' + filepath . replace ( / / g, '\\ ' ) ,
15
- ( error , stdout , stderr ) => {
16
- const checksum = stdout . split ( ' ' ) [ 0 ] . trim ( ) ;
17
- if ( error ) {
18
- electronLog . error (
19
- 'Error while computing SHA512 of ' + filepath + ' : ' + error ,
20
- ) ;
21
- reject ( error ) ;
22
- } else {
23
- electronLog . info (
24
- 'Computed SHA512 of ' + filepath + ' result: ' + checksum ,
25
- ) ;
26
- resolve ( checksum ) ;
27
- }
28
- } ,
29
- ) ;
30
- } ) ;
48
+ return Checksum . fileHash ( filepath , 'sha512' ) ;
31
49
}
32
50
33
51
/**
@@ -37,30 +55,7 @@ export default class Checksum {
37
55
* @return {Promise.string } The calculation result
38
56
*/
39
57
public static sha256 ( filepath : string ) : Promise < string > {
40
- return new Promise < string > ( ( resolve , reject ) => {
41
- if ( ! filepath ) {
42
- reject ( new Error ( ) ) ;
43
- }
44
-
45
- child . exec (
46
- 'shasum -a 256 ' + filepath . replace ( / / g, '\\ ' ) ,
47
- ( error , stdout , stderr ) => {
48
- const checksum = stdout . split ( ' ' ) [ 0 ] . trim ( ) ;
49
-
50
- if ( error ) {
51
- electronLog . error (
52
- 'Error while computing sha256 of ' + filepath + ' : ' + error ,
53
- ) ;
54
- reject ( error ) ;
55
- } else {
56
- electronLog . info (
57
- 'Computed sha256 of ' + filepath + ' result: ' + checksum ,
58
- ) ;
59
- resolve ( checksum ) ;
60
- }
61
- } ,
62
- ) ;
63
- } ) ;
58
+ return Checksum . fileHash ( filepath , 'sha256' ) ;
64
59
}
65
60
66
61
/**
@@ -70,25 +65,7 @@ export default class Checksum {
70
65
* @return {Promise.string } The calculation result
71
66
*/
72
67
public static sha1 ( filepath : string ) : Promise < string > {
73
- return new Promise < string > ( ( resolve , reject ) => {
74
- child . exec (
75
- 'openssl sha1 ' + filepath . replace ( / / g, '\\ ' ) ,
76
- ( error , stdout , stderr ) => {
77
- const checksum = stdout . split ( '= ' ) [ 1 ] . trim ( ) ;
78
- if ( error ) {
79
- electronLog . error (
80
- 'Error while computing sha1 of ' + filepath + ' : ' + error ,
81
- ) ;
82
- reject ( error ) ;
83
- } else {
84
- electronLog . info (
85
- 'Computed sha1 of ' + filepath + ' result: ' + checksum ,
86
- ) ;
87
- resolve ( checksum ) ;
88
- }
89
- } ,
90
- ) ;
91
- } ) ;
68
+ return Checksum . fileHash ( filepath , 'sha1' ) ;
92
69
}
93
70
94
71
/**
@@ -98,25 +75,7 @@ export default class Checksum {
98
75
* @return {Promise.string } The calculation result
99
76
*/
100
77
public static md5 ( filepath : string ) : Promise < string > {
101
- return new Promise < string > ( ( resolve , reject ) => {
102
- child . exec (
103
- 'openssl md5 ' + filepath . replace ( / / g, '\\ ' ) ,
104
- ( error , stdout , stderr ) => {
105
- const checksum = stdout . split ( '= ' ) [ 1 ] . trim ( ) ;
106
- if ( error ) {
107
- electronLog . error (
108
- 'Error while computing md5 of ' + filepath + ' : ' + error ,
109
- ) ;
110
- reject ( error ) ;
111
- } else {
112
- electronLog . info (
113
- 'Computed md5 of ' + filepath + ' result: ' + checksum ,
114
- ) ;
115
- resolve ( checksum ) ;
116
- }
117
- } ,
118
- ) ;
119
- } ) ;
78
+ return Checksum . fileHash ( filepath , 'md5' ) ;
120
79
}
121
80
122
81
public static allChecksums ( filepath : string ) : Promise < IChecksum [ ] > {
0 commit comments