Git Product home page Git Product logo

js-md5's Introduction

js-md5

Build Status Coverage Status
NPM

A simple and fast MD5 hash function for JavaScript supports UTF-8 encoding.

Demo

MD5 Online
MD5 File Checksum Online

Download

Compress
Uncompress

Benchmark

jsPerf Benchmark
File Benchmark

Installation

You can also install js-md5 by using Bower.

bower install md5

For node.js, you can use this command to install:

npm install js-md5

Notice

buffer method is deprecated. This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.

Usage

You could use like this:

md5('Message to hash');
var hash = md5.create();
hash.update('Message to hash');
hash.hex();

// HMAC
md5.hmac('key', 'Message to hash');

var hash = md5.hmac.create('key');
hash.update('Message to hash');
hash.hex();

Node.js

If you use node.js, you should require the module first:

var md5 = require('js-md5');

TypeScript

If you use TypeScript, you can import like this:

import { md5 } from 'js-md5';

RequireJS

It supports AMD:

require(['your/path/md5.js'], function(md5) {
// ...
});

See document

Example

md5(''); // d41d8cd98f00b204e9800998ecf8427e
md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0

// It also supports UTF-8 encoding
md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07

// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
md5([]); // d41d8cd98f00b204e9800998ecf8427e
md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e

// Different output
md5(''); // d41d8cd98f00b204e9800998ecf8427e
md5.hex(''); // d41d8cd98f00b204e9800998ecf8427e
md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
md5.arrayBuffer(''); // ArrayBuffer
md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg==

// HMAC
md5.hmac.hex('key', 'Message to hash');
md5.hmac.array('key', 'Message to hash');
// ...

License

The project is released under the MIT license.

Contact

The project's website is located at https://github.com/emn178/js-md5
Author: Chen, Yi-Cyuan ([email protected])

js-md5's People

Contributors

dependabot[bot] avatar emn178 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-md5's Issues

Possible Malware

This is possibly a false positive, but wanted to mention it. I just ran Malwarebytes on my Mac and it quarantined files from this repo for Adware. Worth running a quick scan on your code.

Thanks.

Secret

Is there a way to use a secret when creating the MD5 hash?

Error on IE 10 when using non-string messages

On IE10, there is no ArrayBuffer.isView() method. As this is called when try to call the md5 function with a blob or anything else than a string, IE 10 reports error of isView not being defined.

As a workaround, a polyfill could be created to add isView() function to ArrayBuffer (should be pretty simple, I made it in several lines of code) or change the code to not call that method, at all.

Docs: Correct benchmark result + joseph-md5

I noticed that in your README, there is a benchmark testing, but the joseph's md5 is not included. I wonder which is faster so I tried to compare it.

Docs for joseph-md5: https://www.myersdaily.org/joseph/javascript/md5-text.html

Benchmark 1

Benchmark: https://measurethat.net/Benchmarks/Show/29171/0/md5-performance-comparison-v2

Result:

Screen Shot 2024-01-10 at 7 48 05

Benchmark 2

https://measurethat.net/Benchmarks/Show/29172/0/md5-performance-comparison-long-text-v2

Result:

Screen Shot 2024-01-10 at 7 52 30

Issue with the current benchmark in README

  1. jsperf.app is a bit unreliable on short time self execution. It should be tested with a long input.
  2. a longer string should be used to see the actual speed.
  3. One of the tests is async. deferred.resolve is required.

Correct jsperf.app with deferred.resolve + joseph-md5

Benchmark: https://jsperf.app/jonuhi/6

Screen Shot 2024-01-10 at 7 57 04

Now it comes out the result that aligns with measurethat.net.

Summary

Your library is great ! I believe it is the fastest MD5 library for sync md5 function.
I will submit a PR for updating the README :)

Note: I guess if you convert js-md5 to using low-level WebAssembly coding, it will be much faster.

Array buffer allocation failure

buffer.js:93
return new ArrayBuffer(size);
^

RangeError: Array buffer allocation failed
at new ArrayBuffer ()
at createUnsafeArrayBuffer (buffer.js:93:12)
at createUnsafeBuffer (buffer.js:87:25)
at allocate (buffer.js:291:12)
at fromObject (buffer.js:372:15)
at Function.Buffer.from (buffer.js:192:11)
at new Buffer (buffer.js:158:17)
at nodeMethod (/root/jam-utils/node_modules/js-md5/src/md5.js:170:48)

Is there an absolute maximum of a data stream that can be computed over?

Size limit

Hello,

I'm tried to use your system to checksum big files (3G) and your script doesn't work and stay with loading message. Do you have limited the size on the script or it's a limit of the browser or the system.

Thanks for your answer.

Seems to work on objects too

Hi !

Could you add on the README it works on objects too ? (and it's quite performant)
I was afraid to do a md5(JSON.stringify(object)) (250ms), but md5(myobject) seems to work in 1.5ms.

Wrong result with file size between 2G and 4G

Hello,

When I calc a file of 2,369,284,818 bytes, I got a different result from other md5 tools.

I changed line 338 of md5.js from

blocks[15] = this.hBytes << 3 | this.bytes >> 29;

to

blocks[15] = this.hBytes << 3 | (this.bytes >> 29 & 7);

then all is OK. And more, when I calc "2369284818 >> 29" in my chrome browser, I got "-4", not "4"

Typescript support?

I was trying to use it in a typescript based project. Does it have typescript support? If not - any easy way to get it working with typescript?

And finally if you have plan to official support typescript?

Missing base64 method from type definition

md5 has a method base64 to return a base64 encoded md5 hash, but it's missing from the index.d.ts.

Would it be possible to add that and release?

Thank you for your time

why md5 is different

I download a file from remote url as arrarbuffer , md5 is 441018525208457705bf09a8ee3c1093, but onlien checksum is 549e444b1aefa40c3412fd90695b8069 and linux md5sum is so

using webpack problem

hi , when I use webpack to package my code, it will recognize js-md5 as node module and compact with a lot of polyfill code.

Please take a look at this problems, thanks

eg:
dddd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.