0.0.1 • Published 5 months ago

@1n/file-hash v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@1n/file-hash

Get the hash-sum of a given file, with low memory usage, even on huge files.

Installation

npm install --save @1n/file-hash

Usage

Sync usage

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}`)

Async usage

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}`)

API

Types

type HashAlgorithm = 'md5' | 'sha1' | 'sha256' | 'sha512'
interface HashOptions<Async extends boolean = boolean> {
  async?: Async
  algorithm?: HashAlgorithm
}

Sync api

Synchronously get the hash-sum of the file at filePath.

import { hash } from '@1n/file-hash'

hash(
  filePath: string, 
  algorithm?: HashAlgorithm = 'md5'
): string

hash(
  filePath: string, 
  options?: {
    async?: false
    algorithm?: HashAlgorithm = 'md5'
  }
): string
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 that will be resolved with a string containing the algorithm.

import { hash } from '@1n/file-hash'

hash(
  filePath: string, 
  options?: {
    async: true
    algorithm?: HashAlgorithm = 'md5'
  }
): Promise<string>
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