@1n/file-hash
Version:
Get the hash-sum of a given file, with low memory usage, even on huge file
135 lines (99 loc) • 2.57 kB
Markdown
Get the hash-sum of a given file, with low memory usage, even on huge files.
```sh
npm install --save @1n/file-hash
```
```js
import { hash, hashSync } from '@1n/file-hash'
const code = hash('package.json') // md5
const code = hash('package.json', 'sha256') // sha256
const code = hash('package.json', { algorithm: 'sha256' }) // sha256
const code = hashSync('package.json') // md5
const code = hashSync('package.json', 'sha256') // sha256
const code = hashSync('package.json', { algorithm: 'sha256' }) // sha256
console.log(`The hash sum of package.json is: ${code}`)
```
```js
import { hash, hashAsync } from '@1n/file-hash'
const code = await hash('package.json', { async: true }) // md5
const code = await hash('package.json', {
async: true,
algorithm: 'sha256'
}) // sha256
const code = await hashAsync('package.json') // md5
const code = await hashAsync('package.json', 'sha256') // sha256
const code = await hashAsync('package.json', { algorithm: 'sha256' }) // sha256
console.log(`The hash sum of package.json is: ${code}`)
```
```ts
type HashAlgorithm = 'md5' | 'sha1' | 'sha256' | 'sha512'
interface HashOptions<Async extends boolean = boolean> {
async?: Async
algorithm?: HashAlgorithm
}
```
Synchronously get the hash-sum of the file at `filePath`.
```ts
import { hash } from '@1n/file-hash'
hash(
filePath: string,
algorithm?: HashAlgorithm = 'md5'
): string
hash(
filePath: string,
options?: {
async?: false
algorithm?: HashAlgorithm = 'md5'
}
): string
```
```ts
import { hashSync } from '@1n/file-hash'
hashSync(
filePath: string,
algorithm?: HashAlgorithm = 'md5'
): string
hashSync(
filePath: string,
options?: {
async?: false
algorithm?: HashAlgorithm = 'md5'
}
): string
```
### Async api
Asynchronously get the hash-sum of the file at `filePath`.
Returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that will be resolved with a string containing the algorithm.
```ts
import { hash } from '@1n/file-hash'
hash(
filePath: string,
options?: {
async: true
algorithm?: HashAlgorithm = 'md5'
}
): Promise<string>
```
```ts
import { hashAsync } from '@1n/file-hash'
hashAsync(
filePath: string,
algorithm?: HashAlgorithm = 'md5'
):Promise<string>
hashAsync(
filePath: string,
options?: {
async?: true
algorithm: HashAlgorithm = 'md5'
}
): Promise<string>
```
### License
MIT