hashion
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

Quick start

Install

npm i hashion

Useage

import Hashion (Base class)

import { Hashion } from 'hashion'

Sha

import { Sha } from 'hashion/sha'

type ShaAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512'

const hasher = new Hashion(Sha, options: {
  algorithm: 'SHA-256'
})

[!NOTE] 不传参数options, 默认使用SHA-256算法

Spark

import { Spark } from 'hashion/spark'

const hasher = new Hashion(Spark)

SparkWorker

import { SparkWorker } from 'hashion/sparkWorker'

const hasher = new Hashion(SparkWorker)

How to Use

type HashParameters = {
  /** File */
  file: File
  /** 分开大小 */
  chunkSize: number
}

type HashCallbackData = {
  /** 计算进度 1-100 */
  progress: number
  /** 成功之后的hash值 */
  hash?: string
  /** 计算耗时,单位为ms */
  time?: number
}

type HashCallback = (e: any, data: HashCallbackData) => void

let readCancel
const chunkSize = 5 * 1024 * 1024
const hasher = new Hashion(Spark)

const callback = ({ progress }) => {
  console.log('progress', progress)
}

const handleSelected = async (e) => {
  const file = e.target.files[0]
  e.target.value = ''

  const { promise, abort } = hasher.computedHash({ file, chunkSize }, callback)
  readCancel = abort
  const result: HashCallbackData = await promise
}

const handleAbort = () => readCancel && readCancel()

Package Sidebar

Install

npm i hashion

Weekly Downloads

359

Version

0.0.8

License

MIT

Unpacked Size

34.5 kB

Total Files

14

Last publish

Collaborators

  • moyuderen.npm