添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I would like to compute the hash of a resource (e.g., a PDF) from a URL. To this end, I wrote

const computeHash = co.wrap(function* main(url) {
  const response = yield promisify(request)(url);
  // assume response.status === 200
  const buf = new Uint8Array(response.arrayBuffer);
  const hash = crypto.createHash('sha1');
  hash.update(buf, 'binary');
  return hash.digest('hex');

to be used

const hash = yield computeHash('http://arxiv.org/pdf/1001.1234v3.pdf');

What I like about the code:

  • It's a generator, so I can yield it. Just a step away from async/await.
  • What I don't like:

  • It doesn't correctly compute the hash. :)
  • The request is completed and the response body as a whole piped into the hash function. I'd rather pipe the output of request into the hash function.
  • Any hints?

    crypto.createHash() provides a Hash instance that currently supports two interfaces: legacy (update() and digest()) and streaming. You don't have to do anything special to use either one, so to stream the response to the hashing stream it's as simple as:

    var hasher = crypto.createHash('sha1');
    hasher.setEncoding('hex');
    request(url).pipe(hasher).on('finish', function() {
      console.log('Hash is', hasher.read());
    

    That's how you'd do it with normal callbacks, but I am not sure how you'd work yield into that as I'm not familiar enough with generators and the like.

    with modern async/await, you can now use the stream/promises builtin:

    const streamp = require('stream/promises');
    const hasher = crypto.createHash('sha1');
    await streamp.pipeline(request(url), hasher);
    console.log('Hash is', hasher.digest('hex'));
    

    for co, just replace await with yield

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.