-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kunal Nagar
authored
Jan 2, 2021
1 parent
7272d5d
commit d447e7f
Showing
12 changed files
with
218 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { EventEmitter } from 'events'; | ||
import { ReadStream, WriteStream } from 'fs'; | ||
|
||
import { getCipher, getDecipher } from './utils/cipher'; | ||
import { compress, decompress } from './utils/compress'; | ||
import { getFileSize } from './utils/file-size'; | ||
import { | ||
getInitializationVector, | ||
getInitializationVectorStream, | ||
} from './utils/initialization-vector'; | ||
import { calculateProgress } from './utils/progress'; | ||
import { getDestinationStream, getSourceStream } from './utils/stream'; | ||
|
||
export interface ICrypto extends EventEmitter { | ||
encrypt(): void; | ||
decrypt(): void; | ||
} | ||
|
||
export default class Crypto extends EventEmitter implements ICrypto { | ||
private _sourceFilePath: string; | ||
private _destinationFilePath: string; | ||
private _passphrase: string; | ||
|
||
constructor( | ||
sourceFilePath: string, | ||
destinationFilePath: string, | ||
passphrase: string, | ||
) { | ||
super(); | ||
this._sourceFilePath = sourceFilePath; | ||
this._destinationFilePath = destinationFilePath; | ||
this._passphrase = passphrase; | ||
} | ||
|
||
encrypt(): void { | ||
const initializationVector = getInitializationVector(); | ||
const initializationVectorStream = getInitializationVectorStream( | ||
initializationVector, | ||
); | ||
const cipher = getCipher(this._passphrase, initializationVector); | ||
const sourceStream = getSourceStream(this._sourceFilePath); | ||
const destinationStream = getDestinationStream(this._destinationFilePath); | ||
sourceStream | ||
.pipe(compress()) | ||
.pipe(cipher) | ||
.pipe(initializationVectorStream) | ||
.pipe(destinationStream); | ||
this._handleSourceStream(sourceStream); | ||
this._handleDestinationStream(destinationStream); | ||
} | ||
|
||
decrypt(): void { | ||
const initializationVectorStream = getSourceStream( | ||
this._sourceFilePath, | ||
0, | ||
15, | ||
); | ||
const sourceStream = getSourceStream(this._sourceFilePath, 16); | ||
const destinationStream = getDestinationStream(this._destinationFilePath); | ||
initializationVectorStream.on('data', (chunk) => { | ||
const initializationVector = chunk as Buffer; | ||
const decipher = getDecipher(this._passphrase, initializationVector); | ||
sourceStream.pipe(decipher).pipe(decompress()).pipe(destinationStream); | ||
}); | ||
this._handleSourceStream(sourceStream); | ||
this._handleDestinationStream(destinationStream); | ||
} | ||
|
||
private _handleSourceStream(sourceStream: ReadStream) { | ||
let currentSize = 0; | ||
const totalSize = getFileSize(this._sourceFilePath).size; | ||
sourceStream.on('data', (chunk) => { | ||
currentSize += chunk.length; | ||
this.emit( | ||
'crypto:source_stream:progress', | ||
calculateProgress(currentSize, totalSize), | ||
); | ||
}); | ||
} | ||
|
||
private _handleDestinationStream(destinationStream: WriteStream) { | ||
destinationStream.on('finish', () => { | ||
this.emit('crypto:destination_stream:finish'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
Cipher, | ||
createCipheriv, | ||
createDecipheriv, | ||
createHash, | ||
Decipher, | ||
} from 'crypto'; | ||
|
||
const getCipherKey = (password: string): Buffer => { | ||
return createHash('sha256').update(password).digest(); | ||
}; | ||
|
||
export const getCipher = ( | ||
password: string, | ||
initializationVector: Buffer, | ||
): Cipher => { | ||
return createCipheriv( | ||
'aes-256-cbc', | ||
getCipherKey(password), | ||
initializationVector, | ||
); | ||
}; | ||
|
||
export const getDecipher = ( | ||
password: string, | ||
initializationVector: Buffer, | ||
): Decipher => { | ||
return createDecipheriv( | ||
'aes-256-cbc', | ||
getCipherKey(password), | ||
initializationVector, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createGunzip, createGzip, Gunzip, Gzip } from 'zlib'; | ||
|
||
export const compress = (): Gzip => { | ||
return createGzip(); | ||
}; | ||
|
||
export const decompress = (): Gunzip => { | ||
return createGunzip(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Stats, statSync } from 'fs'; | ||
|
||
export const getFileSize = (path: string): Stats => { | ||
return statSync(path); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { randomBytes } from 'crypto'; | ||
|
||
import Vector from './vector'; | ||
|
||
export const getInitializationVector = (): Buffer => { | ||
return randomBytes(16); | ||
}; | ||
|
||
export const getInitializationVectorStream = ( | ||
initializationVector: Buffer, | ||
): Vector => { | ||
return new Vector(initializationVector); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const calculateProgress = ( | ||
chunkSize: number, | ||
totalSize: number, | ||
): number => { | ||
return Math.floor((chunkSize / totalSize) * 100); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
createReadStream, | ||
createWriteStream, | ||
ReadStream, | ||
WriteStream, | ||
} from 'fs'; | ||
|
||
export const getSourceStream = ( | ||
path: string, | ||
start?: number, | ||
end?: number, | ||
): ReadStream => { | ||
return createReadStream(path, { | ||
start, | ||
end, | ||
}); | ||
}; | ||
|
||
export const getDestinationStream = (path: string): WriteStream => { | ||
return createWriteStream(path); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { Transform, TransformCallback } from 'stream'; | ||
|
||
export default class Vector extends Transform { | ||
private initVector: Buffer; | ||
private isAppended: boolean; | ||
|
||
constructor(initVector: Buffer) { | ||
super(); | ||
this.initVector = initVector; | ||
this.isAppended = false; | ||
} | ||
|
||
_transform( | ||
chunk: any, | ||
encoding: BufferEncoding, | ||
cb: TransformCallback, | ||
): void { | ||
if (!this.isAppended) { | ||
this.push(this.initVector); | ||
this.isAppended = true; | ||
} | ||
this.push(chunk); | ||
cb(); | ||
} | ||
} |
Oops, something went wrong.