node.js hash string?

后端 未结 11 2362
暗喜
暗喜 2020-12-02 03:49

I have a string that I want to hash. What\'s the easiest way to generate the hash in node.js?

The hash is for versioning, not security.

11条回答
  •  天命终不由人
    2020-12-02 04:16

    The crypto module makes this very easy.

    Setup:

    // import crypto from 'crypto';
    const crypto = require('crypto');
    
    const sha256 = x => crypto.createHash('sha256').update(x, 'utf8').digest('hex');
    

    Usage:

    sha256('Hello, world. ');
    

提交回复
热议问题