Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active October 20, 2019 18:55
Show Gist options
  • Select an option

  • Save TravisMullen/dfcd1f08887be1ed82f29276308fb82d to your computer and use it in GitHub Desktop.

Select an option

Save TravisMullen/dfcd1f08887be1ed82f29276308fb82d to your computer and use it in GitHub Desktop.

Revisions

  1. Travis Mullen revised this gist Oct 20, 2019. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions integrity.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,13 @@
    #!/usr/bin/env node

    import { createHash }from 'crypto'
    import { createReadStream }from 'fs'

    import { createHash } from 'crypto'
    import { createReadStream } from 'fs'

    /**
    * Generate hash from file.
    * @param {string} filename - path to file.
    * @param {string} algorithm
    * @returns {Promise}
    */
    export const shasum = (filename = process.argv[2], algorithm = 'sha256') => (
    export const shasum = (filename, algorithm = 'sha256') => (
    new Promise((resolve, reject) => {
    if (!(filename &&
    typeof (filename) !== 'string')) {
  2. Travis Mullen revised this gist Oct 20, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions integrity.js
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,8 @@ import { createReadStream }from 'fs'
    * @param {string} algorithm
    * @returns {Promise}
    */
    export const shasum = (filename, algorithm = 'sha256') => (
    export const shasum = (filename = process.argv[2], algorithm = 'sha256') => (
    new Promise((resolve, reject) => {
    const filename = process.argv[2]
    if (!(filename &&
    typeof (filename) !== 'string')) {
    reject(new Error('file not found.'))
  3. Travis Mullen created this gist Oct 20, 2019.
    41 changes: 41 additions & 0 deletions integrity.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env node

    import { createHash }from 'crypto'
    import { createReadStream }from 'fs'


    /**
    * Generate hash from file.
    * @param {string} filename - path to file.
    * @param {string} algorithm
    * @returns {Promise}
    */
    export const shasum = (filename, algorithm = 'sha256') => (
    new Promise((resolve, reject) => {
    const filename = process.argv[2]
    if (!(filename &&
    typeof (filename) !== 'string')) {
    reject(new Error('file not found.'))
    }

    const hash = createHash(algorithm)
    const input = createReadStream(filename)

    input.on('readable', () => {
    // Only one element is going to be produced by the
    // hash stream.
    const data = input.read()
    if (data) {
    hash.update(data)
    } else {
    const result = hash.digest('hex')
    console.log(`${result} ${filename}`)
    resolve(result)
    }
    })

    hash.on('error', error => {
    reject(error)
    })
    })
    )