[Pkg-javascript-commits] [node-md5.js] 01/22: Initial commit
Bastien Roucariès
rouca at moszumanska.debian.org
Wed Sep 6 09:56:02 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-md5.js.
commit 38283b6e9b336a9ac5decf50fbdcd76aac90f659
Author: Kirill Fomichev <fanatid at ya.ru>
Date: Mon Apr 4 18:18:09 2016 +0300
Initial commit
---
.gitignore | 5 ++
.travis.yml | 17 ++++++
README.md | 31 +++++++++++
index.js | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 39 +++++++++++++
test/index.js | 21 +++++++
6 files changed, 288 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8afa17e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.nyc_output
+coverage
+node_modules
+
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..85b53b8
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,17 @@
+sudo: false
+language: node_js
+node_js:
+ - "0.10"
+ - "0.11"
+ - "0.12"
+ - "4"
+ - "5"
+env:
+ matrix:
+ - TEST_SUITE=unit
+matrix:
+ include:
+ - node_js: "4"
+ env: TEST_SUITE=lint
+script: npm run-script $TEST_SUITE
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..587d037
--- /dev/null
+++ b/README.md
@@ -0,0 +1,31 @@
+# md5.js
+
+[![NPM Package](https://img.shields.io/npm/v/md5.js.svg?style=flat-square)](https://www.npmjs.org/package/md5.js)
+[![Build Status](https://img.shields.io/travis/crypto-browserify/md5.js.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/md5.js)
+[![Dependency status](https://img.shields.io/david/crypto-browserify/md5.js.svg?style=flat-square)](https://david-dm.org/crypto-browserify/md5.js#info=dependencies)
+
+[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
+
+Node style `md5` on pure JavaScript.
+
+From [NIST SP 800-131A][1]: *md5 is no longer acceptable where collision resistance is required such as digital signatures.*
+
+## Example
+
+```js
+var MD5 = require('md5.js')
+
+console.log(new MD5().update('42').digest('hex'))
+// => a1d0c6e83f027327d8461063f4ac58a6
+
+var md5stream = new MD5()
+md5stream.end('42')
+console.log(md5stream.read().toString('hex'))
+// => a1d0c6e83f027327d8461063f4ac58a6
+```
+
+## LICENSE
+
+MIT
+
+[1]: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..cf2b5e7
--- /dev/null
+++ b/index.js
@@ -0,0 +1,175 @@
+'use strict'
+var inherits = require('inherits')
+var HashBase = require('hash-base')
+
+function MD5 () {
+ HashBase.call(this)
+
+ // state
+ this._a = 0x67452301
+ this._b = 0xefcdab89
+ this._c = 0x98badcfe
+ this._d = 0x10325476
+
+ // block data
+ this._block = new Buffer(64) // 64 * 8 = 512 bit
+ this._blockLength = 0
+ this._lengthL = 0
+ this._lengthH = 0
+}
+
+inherits(MD5, HashBase)
+
+MD5.prototype._update = function (data) {
+ // consume data
+ var offset = 0
+ while (this._blockLength + data.length - offset >= 64) {
+ for (var i = this._blockLength; i < 64;) this._block[i++] = data[offset++]
+ this._handleBlock()
+ this._blockLength = 0
+ }
+
+ while (offset < data.length) {
+ this._block[this._blockLength++] = data[offset++]
+ }
+
+ // update length
+ this._lengthL += data.length * 8
+ if (this._lengthL > 0xffffffff) {
+ this._lengthL -= 0x0100000000
+ this._lengthH += 1
+ if (this._lengthH > 0xffffffff) this._lengthH = 0
+ }
+}
+
+MD5.prototype._digest = function () {
+ // create padding and handle blocks
+ this._block[this._blockLength++] = 0x80
+ if (this._blockLength > 56) {
+ this._block.fill(0, this._blockLength, 64)
+ this._handleBlock()
+ this._blockLength = 0
+ }
+
+ this._block.fill(0, this._blockLength, 56)
+ this._block.writeUInt32LE(this._lengthL, 56)
+ this._block.writeUInt32LE(this._lengthH, 60)
+ this._handleBlock()
+
+ // produce result
+ var buffer = new Buffer(16)
+ buffer.writeUInt32LE(this._a, 0)
+ buffer.writeUInt32LE(this._b, 4)
+ buffer.writeUInt32LE(this._c, 8)
+ buffer.writeUInt32LE(this._d, 12)
+ return buffer
+}
+
+MD5.prototype._handleBlock = function () {
+ var m = new Array(16)
+ for (var i = 0; i < 16; ++i) m[i] = this._block.readUInt32LE(i * 4)
+
+ var a = this._a
+ var b = this._b
+ var c = this._c
+ var d = this._d
+
+ a = fnF(a, b, c, d, m[0], 0xd76aa478, 7)
+ d = fnF(d, a, b, c, m[1], 0xe8c7b756, 12)
+ c = fnF(c, d, a, b, m[2], 0x242070db, 17)
+ b = fnF(b, c, d, a, m[3], 0xc1bdceee, 22)
+ a = fnF(a, b, c, d, m[4], 0xf57c0faf, 7)
+ d = fnF(d, a, b, c, m[5], 0x4787c62a, 12)
+ c = fnF(c, d, a, b, m[6], 0xa8304613, 17)
+ b = fnF(b, c, d, a, m[7], 0xfd469501, 22)
+ a = fnF(a, b, c, d, m[8], 0x698098d8, 7)
+ d = fnF(d, a, b, c, m[9], 0x8b44f7af, 12)
+ c = fnF(c, d, a, b, m[10], 0xffff5bb1, 17)
+ b = fnF(b, c, d, a, m[11], 0x895cd7be, 22)
+ a = fnF(a, b, c, d, m[12], 0x6b901122, 7)
+ d = fnF(d, a, b, c, m[13], 0xfd987193, 12)
+ c = fnF(c, d, a, b, m[14], 0xa679438e, 17)
+ b = fnF(b, c, d, a, m[15], 0x49b40821, 22)
+
+ a = fnG(a, b, c, d, m[1], 0xf61e2562, 5)
+ d = fnG(d, a, b, c, m[6], 0xc040b340, 9)
+ c = fnG(c, d, a, b, m[11], 0x265e5a51, 14)
+ b = fnG(b, c, d, a, m[0], 0xe9b6c7aa, 20)
+ a = fnG(a, b, c, d, m[5], 0xd62f105d, 5)
+ d = fnG(d, a, b, c, m[10], 0x02441453, 9)
+ c = fnG(c, d, a, b, m[15], 0xd8a1e681, 14)
+ b = fnG(b, c, d, a, m[4], 0xe7d3fbc8, 20)
+ a = fnG(a, b, c, d, m[9], 0x21e1cde6, 5)
+ d = fnG(d, a, b, c, m[14], 0xc33707d6, 9)
+ c = fnG(c, d, a, b, m[3], 0xf4d50d87, 14)
+ b = fnG(b, c, d, a, m[8], 0x455a14ed, 20)
+ a = fnG(a, b, c, d, m[13], 0xa9e3e905, 5)
+ d = fnG(d, a, b, c, m[2], 0xfcefa3f8, 9)
+ c = fnG(c, d, a, b, m[7], 0x676f02d9, 14)
+ b = fnG(b, c, d, a, m[12], 0x8d2a4c8a, 20)
+
+ a = fnH(a, b, c, d, m[5], 0xfffa3942, 4)
+ d = fnH(d, a, b, c, m[8], 0x8771f681, 11)
+ c = fnH(c, d, a, b, m[11], 0x6d9d6122, 16)
+ b = fnH(b, c, d, a, m[14], 0xfde5380c, 23)
+ a = fnH(a, b, c, d, m[1], 0xa4beea44, 4)
+ d = fnH(d, a, b, c, m[4], 0x4bdecfa9, 11)
+ c = fnH(c, d, a, b, m[7], 0xf6bb4b60, 16)
+ b = fnH(b, c, d, a, m[10], 0xbebfbc70, 23)
+ a = fnH(a, b, c, d, m[13], 0x289b7ec6, 4)
+ d = fnH(d, a, b, c, m[0], 0xeaa127fa, 11)
+ c = fnH(c, d, a, b, m[3], 0xd4ef3085, 16)
+ b = fnH(b, c, d, a, m[6], 0x04881d05, 23)
+ a = fnH(a, b, c, d, m[9], 0xd9d4d039, 4)
+ d = fnH(d, a, b, c, m[12], 0xe6db99e5, 11)
+ c = fnH(c, d, a, b, m[15], 0x1fa27cf8, 16)
+ b = fnH(b, c, d, a, m[2], 0xc4ac5665, 23)
+
+ a = fnI(a, b, c, d, m[0], 0xf4292244, 6)
+ d = fnI(d, a, b, c, m[7], 0x432aff97, 10)
+ c = fnI(c, d, a, b, m[14], 0xab9423a7, 15)
+ b = fnI(b, c, d, a, m[5], 0xfc93a039, 21)
+ a = fnI(a, b, c, d, m[12], 0x655b59c3, 6)
+ d = fnI(d, a, b, c, m[3], 0x8f0ccc92, 10)
+ c = fnI(c, d, a, b, m[10], 0xffeff47d, 15)
+ b = fnI(b, c, d, a, m[1], 0x85845dd1, 21)
+ a = fnI(a, b, c, d, m[8], 0x6fa87e4f, 6)
+ d = fnI(d, a, b, c, m[15], 0xfe2ce6e0, 10)
+ c = fnI(c, d, a, b, m[6], 0xa3014314, 15)
+ b = fnI(b, c, d, a, m[13], 0x4e0811a1, 21)
+ a = fnI(a, b, c, d, m[4], 0xf7537e82, 6)
+ d = fnI(d, a, b, c, m[11], 0xbd3af235, 10)
+ c = fnI(c, d, a, b, m[2], 0x2ad7d2bb, 15)
+ b = fnI(b, c, d, a, m[9], 0xeb86d391, 21)
+
+ this._a = (this._a + a) >>> 0
+ this._b = (this._b + b) >>> 0
+ this._c = (this._c + c) >>> 0
+ this._d = (this._d + d) >>> 0
+}
+
+function rotl (x, n) {
+ return ((x << n) | (x >>> (32 - n))) >>> 0
+}
+
+function fnSum (a, f, m, k, s, b) {
+ return (rotl(a + (f >>> 0) + m + k, s) + b) >>> 0
+}
+
+function fnF (a, b, c, d, m, k, s) {
+ return fnSum(a, (b & c) | ((~b) & d), m, k, s, b)
+}
+
+function fnG (a, b, c, d, m, k, s) {
+ return fnSum(a, (b & d) | (c & (~d)), m, k, s, b)
+}
+
+function fnH (a, b, c, d, m, k, s) {
+ return fnSum(a, b ^ c ^ d, m, k, s, b)
+}
+
+function fnI (a, b, c, d, m, k, s) {
+ return fnSum(a, c ^ (b | (~d)), m, k, s, b)
+}
+
+module.exports = MD5
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c7f9e2b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "md5.js",
+ "version": "1.0.0",
+ "description": "node style md5 on pure JavaScript",
+ "keywords": [
+ "crypto",
+ "md5"
+ ],
+ "homepage": "https://github.com/crypto-browserify/md5.js",
+ "bugs": {
+ "url": "https://github.com/crypto-browserify/md5.js/issues"
+ },
+ "license": "MIT",
+ "author": "Kirill Fomichev <fanatid at ya.ru> (https://github.com/fanatid)",
+ "files": [
+ "index.js"
+ ],
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com:crypto-browserify/md5.js.git"
+ },
+ "scripts": {
+ "coverage": "nyc node test/*.js",
+ "lint": "standard",
+ "test": "npm run lint && npm run unit",
+ "unit": "node test/*.js"
+ },
+ "dependencies": {
+ "hash-base": "^1.0.2",
+ "inherits": "^2.0.1"
+ },
+ "devDependencies": {
+ "hash-test-vectors": "^1.3.2",
+ "nyc": "^6.1.1",
+ "standard": "^6.0.8",
+ "tape": "^4.2.0"
+ }
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..cc96545
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,21 @@
+'use strict'
+var test = require('tape').test
+var vectors = require('hash-test-vectors')
+
+var MD5 = require('../')
+
+vectors.forEach(function (vector, i) {
+ var input = new Buffer(vector.input, 'base64')
+
+ test('vector #' + (i + 1) + ' with .update', function (t) {
+ t.same(new MD5().update(input).digest('hex'), vector.md5)
+ t.end()
+ })
+
+ test('vector #' + (i + 1) + ' with streams', function (t) {
+ var hash = new MD5()
+ hash.end(input)
+ t.same(hash.read().toString('hex'), vector.md5)
+ t.end()
+ })
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-md5.js.git
More information about the Pkg-javascript-commits
mailing list