Movatterモバイル変換


[0]ホーム

URL:


crypto-magic

0.1.0 • Public • Published

crypto-magic

A set of utility functions/wrapper to simplify the development workflow

$ yarn add crypto-magic

Features

  • Hash Function Wrapper (MD5, SHA1, SHA244, SHA256, SHA384, SHA512, WHIRLPOOL)
  • Create Object/Array Hashes based on its JSON representation
  • UUIDv4 generator usinc async crypto apirandomBytes
  • base64 urlsafe encoder/decoder

Hash Function Wrapper

Provides easy access to theNode.js Crypto Hash functions. Examples are available inexamples/hash.js

const{hash}=require('crypto-magic');console.log(hash.sha256('Hello World'));
Initialization

The argumentencoding is optional and defines the output encoding of the digest.

  • hex (default) - hexadecimal string output
  • base64 - base64 string output
  • base64urlsafe - url-safe base64 string output (/+= are replaced by_-)
  • binary - binary output asBuffer
Hash Algorithms

The following wrappers are included:

  • md5(input:mixed, [encoding:string])
  • sha1(input:mixed, [encoding:string])
  • sha2(input:mixed, [encoding:string])
  • sha224(input:mixed, [encoding:string])
  • sha256(input:mixed, [encoding:string])
  • sha384(input:mixed, [encoding:string])
  • sha512(input:mixed, [encoding:string])
  • whirlpool(input:mixed, [encoding:string])

General Usage

const{hash}=require('crypto-magic');// some inputconstinput='Hello World';// display some hashesconsole.log('Default HEX Output');console.log(' |- MD5      ',hash.md5(input));console.log(' |- SHA1     ',hash.sha1(input));console.log(' |- SHA256   ',hash.sha256(input));console.log(' |- SHA384   ',hash.sha384(input));console.log(' |- SHA512   ',hash.sha512(input));console.log(' |- WHIRLPOOL',hash.whirlpool(input));// override the default output typeconsole.log('Override the default output settings');console.log(' |- HEX      ',hash.sha1(input,'hex'));console.log(' |- BIN      ',hash.sha1(input,'binary'));console.log(' |- BASE64   ',hash.sha1(input,'base64'));console.log('');

Objects

Objects/Arrays are automatically serialized as JSON String. The JSON object is then passed into the hash function.

const{hash}=require('crypto-magic');// demo objectconstobjectInput={x:1,b:2,c:[5,6,7],d:{y:'Hello',z:'World'}};console.log('Object Input');console.log(' |- SHA256   ',hash.sha256(objectInput));

Random Hash Generator

Create random hashes easily. Just calls thecrypto-magic.hash functions with a random content generated bycrypto-magic.random(514) - helpful for random URLs or filenames (e.g. cache hashes).

// url-safe base64const{randomHash}=require('crypto-magic');// all hash function of crypto-magic.hash are supportedconsole.log(awaitrandomHash.sha256('base64urlsafe'));// output like b7qhqd-WnKAH_GmBpSpvQrdkfUdLxL0m__XGcLTRsbI

License

crypto-magic is OpenSource and licensed under the Terms ofThe MIT License (X11) - your're welcome to contribute

Readme

Keywords

none

Package Sidebar

Install

npm i crypto-magic

Weekly Downloads

51

Version

0.1.0

License

MIT

Unpacked Size

9.48 kB

Total Files

12

Last publish

Collaborators

  • andidittrich

[8]ページ先頭

©2009-2025 Movatter.jp