# utility [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url] [![Dependency Status][dependency-image]][dependency-url] [![devDependency Status][devDependency-image]][devDependency-url] [npm-image]: https://img.shields.io/npm/v/utility.svg?style=flat-square [npm-url]: https://npmjs.org/package/utility [travis-image]: https://img.shields.io/travis/node-modules/utility.svg?style=flat-square [travis-url]: https://travis-ci.org/node-modules/utility [codecov-image]: https://codecov.io/github/node-modules/utility/coverage.svg?branch=master [codecov-url]: https://codecov.io/github/node-modules/utility?branch=master [download-image]: https://img.shields.io/npm/dm/utility.svg?style=flat-square [download-url]: https://npmjs.org/package/utility [dependency-image]: https://david-dm.org/node-modules/utility.svg [dependency-url]: https://david-dm.org/node-modules/utility [devDependency-image]: https://david-dm.org/node-modules/utility/dev-status.svg [devDependency-url]: https://david-dm.org/node-modules/utility#info=devDependencies A collection of useful utilities. ## Install ```bash $ npm install utility ``` ## Usage ```js const utils = require('utility'); ``` Also you can use it within typescript, like this ↓ ```js import * as utility from 'utility'; ``` ### md5 ```js utils.md5('苏千').should.equal('5f733c47c58a077d61257102b2d44481'); utils.md5(Buffer.from('苏千')).should.equal('5f733c47c58a077d61257102b2d44481'); // md5 base64 format utils.md5('苏千', 'base64'); // 'X3M8R8WKB31hJXECstREgQ==' // Object md5 hash. Sorted by key, and JSON.stringify. See source code for detail utils.md5({foo: 'bar', bar: 'foo'}).should.equal(utils.md5({bar: 'foo', foo: 'bar'})); ``` ### sha1 ```js utils.sha1('苏千').should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5'); utils.sha1(Buffer.from('苏千')).should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5'); // sha1 base64 format utils.sha1('苏千', 'base64'); // 'Ckr/a6tjS5wvmbcfJel2kh/N5aU=' // Object sha1 hash. Sorted by key, and JSON.stringify. See source code for detail utils.sha1({foo: 'bar', bar: 'foo'}).should.equal(utils.sha1({bar: 'foo', foo: 'bar'})); ``` ### sha256 ```js utils.sha256(Buffer.from('苏千')).should.equal('75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635'); ``` ### hmac ```js // hmac-sha1 with base64 output encoding utils.hmac('sha1', 'I am a key', 'hello world'); // 'pO6J0LKDxRRkvSECSEdxwKx84L0=' ``` ### decode and encode ```js // base64 encode utils.base64encode('你好¥'); // '5L2g5aW977+l' utils.base64decode('5L2g5aW977+l') // '你好¥' // urlsafe base64 encode utils.base64encode('你好¥', true); // '5L2g5aW977-l' utils.base64decode('5L2g5aW977-l', true); // '你好¥' // html escape and unescape utils.escape('