From 4fcd04d6c4ac705a67a8346fe7fc920ef292cd57 Mon Sep 17 00:00:00 2001 From: Torben Ewert Date: Sun, 19 Apr 2020 16:28:54 +0200 Subject: [PATCH] TASK: Implement MD5 --- README.md | 16 +- __tests__/ReCrypt_test.re | 134 ++-- lib/js/__tests__/ReCrypt_test.bs.js | 117 +-- lib/js/src/ReCrypt.bs.js | 4 + lib/js/src/ReCrypt/ReCrypt_Constants.bs.js | 804 -------------------- lib/js/src/ReCrypt/ReCrypt_Functions.bs.js | 11 +- lib/js/src/ReCrypt/ReCrypt_MD5.bs.js | 177 +++++ lib/js/src/ReCrypt/ReCrypt_Preprocess.bs.js | 15 +- lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js | 17 +- lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js | 70 +- lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js | 70 +- lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js | 344 ++++++++- lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js | 344 ++++++++- lib/js/src/ReCrypt/ReCrypt_Utils.bs.js | 36 +- package.json | 1 + src/ReCrypt.re | 4 + src/ReCrypt.rei | 5 + src/ReCrypt/ReCrypt_Constants.re | 313 -------- src/ReCrypt/ReCrypt_Constants.rei | 5 - src/ReCrypt/ReCrypt_Functions.re | 6 + src/ReCrypt/ReCrypt_Functions.rei | 2 + src/ReCrypt/ReCrypt_MD5.re | 175 +++++ src/ReCrypt/ReCrypt_MD5.rei | 4 + src/ReCrypt/ReCrypt_Preprocess.re | 22 +- src/ReCrypt/ReCrypt_Sha1.re | 12 +- src/ReCrypt/ReCrypt_Sha224.re | 69 +- src/ReCrypt/ReCrypt_Sha256.re | 68 +- src/ReCrypt/ReCrypt_Sha384.re | 96 ++- src/ReCrypt/ReCrypt_Sha512.re | 96 ++- src/ReCrypt/ReCrypt_Utils.re | 28 +- src/ReCrypt/ReCrypt_Utils.rei | 7 +- 31 files changed, 1708 insertions(+), 1364 deletions(-) delete mode 100644 lib/js/src/ReCrypt/ReCrypt_Constants.bs.js create mode 100644 lib/js/src/ReCrypt/ReCrypt_MD5.bs.js delete mode 100644 src/ReCrypt/ReCrypt_Constants.re delete mode 100644 src/ReCrypt/ReCrypt_Constants.rei create mode 100644 src/ReCrypt/ReCrypt_MD5.re create mode 100644 src/ReCrypt/ReCrypt_MD5.rei diff --git a/README.md b/README.md index 0df65d4..b05e36f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # What is it? -Secure Hash Algorithms implemented in ReasonML based on -[FIPS180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) +Hashing Algorithms implemented in ReasonML based on: -# What state is it in? +- [RFC 1321](https://tools.ietf.org/html/rfc1321) +- [FIPS180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) -**Complete** +# The following algorithms are currently supported +- [x] MD5 - [x] SHA-1 - [x] SHA-224 - [x] SHA-256 @@ -35,6 +36,13 @@ Then add `@ewert-online/re-crypt` as a dependency to `bsconfig.json`: # How do I use it? +### MD5 + +```reason +let hash = ReCrypt.MD5.make("unicorn"); +/* 1abcb33beeb811dca15f0ac3e47b88d9 */ +``` + ### SHA-1 ```reason diff --git a/__tests__/ReCrypt_test.re b/__tests__/ReCrypt_test.re index dfc4ac8..4b5f456 100644 --- a/__tests__/ReCrypt_test.re +++ b/__tests__/ReCrypt_test.re @@ -3,6 +3,7 @@ open Expect; type test = { input: string, + md5: string, sha1: string, sha224: string, sha256: string, @@ -13,6 +14,7 @@ type test = { let tests = [| { input: "unicorn", + md5: "1abcb33beeb811dca15f0ac3e47b88d9", sha1: "84de6753b298abd027fcd1d790eade2413eafb5a", sha224: "983d515094574856a57db3a13741f0a65509bb640bfa551e78fa01d9", sha256: "c6cb50e7eea0df1fd3eaf52ada2358f5423afd7c0b5ee2395231a9b3208ffcaf", @@ -21,6 +23,7 @@ let tests = [| }, { input: "", + md5: "d41d8cd98f00b204e9800998ecf8427e", sha1: "da39a3ee5e6b4b0d3255bfef95601890afd80709", sha224: "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", @@ -29,6 +32,7 @@ let tests = [| }, { input: "Some pretty long string which probably exeed the length of one message block (now it really does it...aaaaaaaaaaaaaaaaaaaaaaah)", + md5: "f59fbeaf0597357d7fb6cf4755211e99", sha1: "87fd7e086d40019262d6fc1ceccd9d731afa6be3", sha224: "935f26cf156817962231e82021da64b808b3c68fb9ff9f53ce9c75cc", sha256: "69c22a11ab4c21beeb184e4fa40556d41bc4d8fe431618b21715352fac67e9ec", @@ -37,6 +41,7 @@ let tests = [| }, { input: "This message is exactly one bit longer than one 128bit word: f49932d6b40a4e4d8424196af7c3477397818e59e748f722186", + md5: "c46500acee5415406c471da700f1511c", sha1: "b0aec6e4ac57547d24be7b38768a7c29c9456a7b", sha224: "19f7516f806e661dd2b72326bdfee7b4805560122becadb3801d8da7", sha256: "1a99b57fe8b12fcc8294f9238d1c1e893d19d798bd20a45a19f1d45d2543f891", @@ -45,6 +50,7 @@ let tests = [| }, { input: "This message is exactly one bit longer than a 64bit word", + md5: "7d3febf8022523df2093238bf6a07792", sha1: "042a302c0fb5ee74143d99a795bcc1036683f602", sha224: "a671f59e0187aef4a1812bb7380e3988b2ed9f41fd2e8a1db91ebbbe", sha256: "15cba6d94c28070f62aa1a16e51d44c0514f0acd5272d482ae3cfdb9a4f79f29", @@ -53,102 +59,80 @@ let tests = [| }, |]; +describe("MD5", () => { + describe("make()", () => { + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.MD5.make(t.input)) |> toBe(t.md5) + ) + }, + tests, + ) + }) +}); + describe("Sha1", () => { describe("make()", () => { - test("\"" ++ tests[0].input ++ "\"", () => - expect(ReCrypt.Sha1.make(tests[0].input)) |> toBe(tests[0].sha1) - ); - test("\"" ++ tests[1].input ++ "\"", () => - expect(ReCrypt.Sha1.make(tests[1].input)) |> toBe(tests[1].sha1) - ); - test("\"" ++ tests[2].input ++ "\"", () => - expect(ReCrypt.Sha1.make(tests[2].input)) |> toBe(tests[2].sha1) - ); - test("\"" ++ tests[3].input ++ "\"", () => - expect(ReCrypt.Sha1.make(tests[3].input)) |> toBe(tests[3].sha1) - ); - test("\"" ++ tests[4].input ++ "\"", () => - expect(ReCrypt.Sha1.make(tests[4].input)) |> toBe(tests[4].sha1) - ); + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.Sha1.make(t.input)) |> toBe(t.sha1) + ) + }, + tests, + ) }) }); describe("Sha224", () => { describe("make()", () => { - test("\"" ++ tests[0].input ++ "\"", () => - expect(ReCrypt.Sha224.make(tests[0].input)) |> toBe(tests[0].sha224) - ); - test("\"" ++ tests[1].input ++ "\"", () => - expect(ReCrypt.Sha224.make(tests[1].input)) |> toBe(tests[1].sha224) - ); - test("\"" ++ tests[2].input ++ "\"", () => - expect(ReCrypt.Sha224.make(tests[2].input)) |> toBe(tests[2].sha224) - ); - test("\"" ++ tests[3].input ++ "\"", () => - expect(ReCrypt.Sha224.make(tests[3].input)) |> toBe(tests[3].sha224) - ); - test("\"" ++ tests[4].input ++ "\"", () => - expect(ReCrypt.Sha224.make(tests[4].input)) |> toBe(tests[4].sha224) - ); + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.Sha224.make(t.input)) |> toBe(t.sha224) + ) + }, + tests, + ) }) }); describe("Sha256", () => { describe("make()", () => { - test("\"" ++ tests[0].input ++ "\"", () => - expect(ReCrypt.Sha256.make(tests[0].input)) |> toBe(tests[0].sha256) - ); - test("\"" ++ tests[1].input ++ "\"", () => - expect(ReCrypt.Sha256.make(tests[1].input)) |> toBe(tests[1].sha256) - ); - test("\"" ++ tests[2].input ++ "\"", () => - expect(ReCrypt.Sha256.make(tests[2].input)) |> toBe(tests[2].sha256) - ); - test("\"" ++ tests[3].input ++ "\"", () => - expect(ReCrypt.Sha256.make(tests[3].input)) |> toBe(tests[3].sha256) - ); - test("\"" ++ tests[4].input ++ "\"", () => - expect(ReCrypt.Sha256.make(tests[4].input)) |> toBe(tests[4].sha256) - ); + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.Sha256.make(t.input)) |> toBe(t.sha256) + ) + }, + tests, + ) }) }); describe("Sha384", () => { describe("make()", () => { - test("\"" ++ tests[0].input ++ "\"", () => - expect(ReCrypt.Sha384.make(tests[0].input)) |> toBe(tests[0].sha384) - ); - test("\"" ++ tests[1].input ++ "\"", () => - expect(ReCrypt.Sha384.make(tests[1].input)) |> toBe(tests[1].sha384) - ); - test("\"" ++ tests[2].input ++ "\"", () => - expect(ReCrypt.Sha384.make(tests[2].input)) |> toBe(tests[2].sha384) - ); - test("\"" ++ tests[3].input ++ "\"", () => - expect(ReCrypt.Sha384.make(tests[3].input)) |> toBe(tests[3].sha384) - ); - test("\"" ++ tests[4].input ++ "\"", () => - expect(ReCrypt.Sha384.make(tests[4].input)) |> toBe(tests[4].sha384) - ); + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.Sha384.make(t.input)) |> toBe(t.sha384) + ) + }, + tests, + ) }) }); describe("Sha512", () => { describe("make()", () => { - test("\"" ++ tests[0].input ++ "\"", () => - expect(ReCrypt.Sha512.make(tests[0].input)) |> toBe(tests[0].sha512) - ); - test("\"" ++ tests[1].input ++ "\"", () => - expect(ReCrypt.Sha512.make(tests[1].input)) |> toBe(tests[1].sha512) - ); - test("\"" ++ tests[2].input ++ "\"", () => - expect(ReCrypt.Sha512.make(tests[2].input)) |> toBe(tests[2].sha512) - ); - test("\"" ++ tests[3].input ++ "\"", () => - expect(ReCrypt.Sha512.make(tests[3].input)) |> toBe(tests[3].sha512) - ); - test("\"" ++ tests[4].input ++ "\"", () => - expect(ReCrypt.Sha512.make(tests[4].input)) |> toBe(tests[4].sha512) - ); + Array.iter( + (t: test) => { + test("\"" ++ t.input ++ "\"", () => + expect(ReCrypt.Sha512.make(t.input)) |> toBe(t.sha512) + ) + }, + tests, + ) }) }); \ No newline at end of file diff --git a/lib/js/__tests__/ReCrypt_test.bs.js b/lib/js/__tests__/ReCrypt_test.bs.js index 95ed9cc..5e40ed2 100644 --- a/lib/js/__tests__/ReCrypt_test.bs.js +++ b/lib/js/__tests__/ReCrypt_test.bs.js @@ -2,13 +2,14 @@ 'use strict'; var Jest = require("@glennsl/bs-jest/lib/js/src/jest.js"); +var $$Array = require("bs-platform/lib/js/array.js"); var Curry = require("bs-platform/lib/js/curry.js"); var ReCrypt = require("../src/ReCrypt.bs.js"); -var Caml_array = require("bs-platform/lib/js/caml_array.js"); var tests = [ { input: "unicorn", + md5: "1abcb33beeb811dca15f0ac3e47b88d9", sha1: "84de6753b298abd027fcd1d790eade2413eafb5a", sha224: "983d515094574856a57db3a13741f0a65509bb640bfa551e78fa01d9", sha256: "c6cb50e7eea0df1fd3eaf52ada2358f5423afd7c0b5ee2395231a9b3208ffcaf", @@ -17,6 +18,7 @@ var tests = [ }, { input: "", + md5: "d41d8cd98f00b204e9800998ecf8427e", sha1: "da39a3ee5e6b4b0d3255bfef95601890afd80709", sha224: "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", @@ -25,6 +27,7 @@ var tests = [ }, { input: "Some pretty long string which probably exeed the length of one message block (now it really does it...aaaaaaaaaaaaaaaaaaaaaaah)", + md5: "f59fbeaf0597357d7fb6cf4755211e99", sha1: "87fd7e086d40019262d6fc1ceccd9d731afa6be3", sha224: "935f26cf156817962231e82021da64b808b3c68fb9ff9f53ce9c75cc", sha256: "69c22a11ab4c21beeb184e4fa40556d41bc4d8fe431618b21715352fac67e9ec", @@ -33,6 +36,7 @@ var tests = [ }, { input: "This message is exactly one bit longer than one 128bit word: f49932d6b40a4e4d8424196af7c3477397818e59e748f722186", + md5: "c46500acee5415406c471da700f1511c", sha1: "b0aec6e4ac57547d24be7b38768a7c29c9456a7b", sha224: "19f7516f806e661dd2b72326bdfee7b4805560122becadb3801d8da7", sha256: "1a99b57fe8b12fcc8294f9238d1c1e893d19d798bd20a45a19f1d45d2543f891", @@ -41,6 +45,7 @@ var tests = [ }, { input: "This message is exactly one bit longer than a 64bit word", + md5: "7d3febf8022523df2093238bf6a07792", sha1: "042a302c0fb5ee74143d99a795bcc1036683f602", sha224: "a671f59e0187aef4a1812bb7380e3988b2ed9f41fd2e8a1db91ebbbe", sha256: "15cba6d94c28070f62aa1a16e51d44c0514f0acd5272d482ae3cfdb9a4f79f29", @@ -49,103 +54,63 @@ var tests = [ } ]; +Jest.describe("MD5", (function (param) { + return Jest.describe("make()", (function (param) { + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.md5, Jest.Expect.expect(Curry._1(ReCrypt.MD5.make, t.input))); + })); + }), tests); + })); + })); + Jest.describe("Sha1", (function (param) { return Jest.describe("make()", (function (param) { - Jest.test("\"" + (Caml_array.caml_array_get(tests, 0).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 0).sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, Caml_array.caml_array_get(tests, 0).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 1).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 1).sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, Caml_array.caml_array_get(tests, 1).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 2).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 2).sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, Caml_array.caml_array_get(tests, 2).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 3).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 3).sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, Caml_array.caml_array_get(tests, 3).input))); - })); - return Jest.test("\"" + (Caml_array.caml_array_get(tests, 4).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 4).sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, Caml_array.caml_array_get(tests, 4).input))); - })); + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.sha1, Jest.Expect.expect(Curry._1(ReCrypt.Sha1.make, t.input))); + })); + }), tests); })); })); Jest.describe("Sha224", (function (param) { return Jest.describe("make()", (function (param) { - Jest.test("\"" + (Caml_array.caml_array_get(tests, 0).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 0).sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, Caml_array.caml_array_get(tests, 0).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 1).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 1).sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, Caml_array.caml_array_get(tests, 1).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 2).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 2).sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, Caml_array.caml_array_get(tests, 2).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 3).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 3).sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, Caml_array.caml_array_get(tests, 3).input))); - })); - return Jest.test("\"" + (Caml_array.caml_array_get(tests, 4).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 4).sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, Caml_array.caml_array_get(tests, 4).input))); - })); + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.sha224, Jest.Expect.expect(Curry._1(ReCrypt.Sha224.make, t.input))); + })); + }), tests); })); })); Jest.describe("Sha256", (function (param) { return Jest.describe("make()", (function (param) { - Jest.test("\"" + (Caml_array.caml_array_get(tests, 0).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 0).sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, Caml_array.caml_array_get(tests, 0).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 1).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 1).sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, Caml_array.caml_array_get(tests, 1).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 2).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 2).sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, Caml_array.caml_array_get(tests, 2).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 3).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 3).sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, Caml_array.caml_array_get(tests, 3).input))); - })); - return Jest.test("\"" + (Caml_array.caml_array_get(tests, 4).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 4).sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, Caml_array.caml_array_get(tests, 4).input))); - })); + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.sha256, Jest.Expect.expect(Curry._1(ReCrypt.Sha256.make, t.input))); + })); + }), tests); })); })); Jest.describe("Sha384", (function (param) { return Jest.describe("make()", (function (param) { - Jest.test("\"" + (Caml_array.caml_array_get(tests, 0).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 0).sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, Caml_array.caml_array_get(tests, 0).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 1).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 1).sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, Caml_array.caml_array_get(tests, 1).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 2).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 2).sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, Caml_array.caml_array_get(tests, 2).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 3).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 3).sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, Caml_array.caml_array_get(tests, 3).input))); - })); - return Jest.test("\"" + (Caml_array.caml_array_get(tests, 4).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 4).sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, Caml_array.caml_array_get(tests, 4).input))); - })); + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.sha384, Jest.Expect.expect(Curry._1(ReCrypt.Sha384.make, t.input))); + })); + }), tests); })); })); Jest.describe("Sha512", (function (param) { return Jest.describe("make()", (function (param) { - Jest.test("\"" + (Caml_array.caml_array_get(tests, 0).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 0).sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, Caml_array.caml_array_get(tests, 0).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 1).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 1).sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, Caml_array.caml_array_get(tests, 1).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 2).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 2).sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, Caml_array.caml_array_get(tests, 2).input))); - })); - Jest.test("\"" + (Caml_array.caml_array_get(tests, 3).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 3).sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, Caml_array.caml_array_get(tests, 3).input))); - })); - return Jest.test("\"" + (Caml_array.caml_array_get(tests, 4).input + "\""), (function (param) { - return Jest.Expect.toBe(Caml_array.caml_array_get(tests, 4).sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, Caml_array.caml_array_get(tests, 4).input))); - })); + return $$Array.iter((function (t) { + return Jest.test("\"" + (t.input + "\""), (function (param) { + return Jest.Expect.toBe(t.sha512, Jest.Expect.expect(Curry._1(ReCrypt.Sha512.make, t.input))); + })); + }), tests); })); })); diff --git a/lib/js/src/ReCrypt.bs.js b/lib/js/src/ReCrypt.bs.js index dbbbd9e..5515f77 100644 --- a/lib/js/src/ReCrypt.bs.js +++ b/lib/js/src/ReCrypt.bs.js @@ -1,12 +1,15 @@ // Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE 'use strict'; +var ReCrypt_MD5 = require("./ReCrypt/ReCrypt_MD5.bs.js"); var ReCrypt_Sha1 = require("./ReCrypt/ReCrypt_Sha1.bs.js"); var ReCrypt_Sha224 = require("./ReCrypt/ReCrypt_Sha224.bs.js"); var ReCrypt_Sha256 = require("./ReCrypt/ReCrypt_Sha256.bs.js"); var ReCrypt_Sha384 = require("./ReCrypt/ReCrypt_Sha384.bs.js"); var ReCrypt_Sha512 = require("./ReCrypt/ReCrypt_Sha512.bs.js"); +var MD5 = ReCrypt_MD5; + var Sha1 = ReCrypt_Sha1; var Sha224 = ReCrypt_Sha224; @@ -17,6 +20,7 @@ var Sha384 = ReCrypt_Sha384; var Sha512 = ReCrypt_Sha512; +exports.MD5 = MD5; exports.Sha1 = Sha1; exports.Sha224 = Sha224; exports.Sha256 = Sha256; diff --git a/lib/js/src/ReCrypt/ReCrypt_Constants.bs.js b/lib/js/src/ReCrypt/ReCrypt_Constants.bs.js deleted file mode 100644 index 81d9145..0000000 --- a/lib/js/src/ReCrypt/ReCrypt_Constants.bs.js +++ /dev/null @@ -1,804 +0,0 @@ -// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE -'use strict'; - - -function sha1(t) { - if (t >= 0 && t <= 19) { - return 1518500249; - } else if (t >= 20 && t <= 39) { - return 1859775393; - } else if (t >= 40 && t <= 59) { - return -1894007588; - } else if (t >= 60 && t <= 79) { - return -899497514; - } else { - return 0; - } -} - -var sha224 = [ - 1116352408, - 1899447441, - -1245643825, - -373957723, - 961987163, - 1508970993, - -1841331548, - -1424204075, - -670586216, - 310598401, - 607225278, - 1426881987, - 1925078388, - -2132889090, - -1680079193, - -1046744716, - -459576895, - -272742522, - 264347078, - 604807628, - 770255983, - 1249150122, - 1555081692, - 1996064986, - -1740746414, - -1473132947, - -1341970488, - -1084653625, - -958395405, - -710438585, - 113926993, - 338241895, - 666307205, - 773529912, - 1294757372, - 1396182291, - 1695183700, - 1986661051, - -2117940946, - -1838011259, - -1564481375, - -1474664885, - -1035236496, - -949202525, - -778901479, - -694614492, - -200395387, - 275423344, - 430227734, - 506948616, - 659060556, - 883997877, - 958139571, - 1322822218, - 1537002063, - 1747873779, - 1955562222, - 2024104815, - -2067236844, - -1933114872, - -1866530822, - -1538233109, - -1090935817, - -965641998 -]; - -var sha256 = [ - 1116352408, - 1899447441, - -1245643825, - -373957723, - 961987163, - 1508970993, - -1841331548, - -1424204075, - -670586216, - 310598401, - 607225278, - 1426881987, - 1925078388, - -2132889090, - -1680079193, - -1046744716, - -459576895, - -272742522, - 264347078, - 604807628, - 770255983, - 1249150122, - 1555081692, - 1996064986, - -1740746414, - -1473132947, - -1341970488, - -1084653625, - -958395405, - -710438585, - 113926993, - 338241895, - 666307205, - 773529912, - 1294757372, - 1396182291, - 1695183700, - 1986661051, - -2117940946, - -1838011259, - -1564481375, - -1474664885, - -1035236496, - -949202525, - -778901479, - -694614492, - -200395387, - 275423344, - 430227734, - 506948616, - 659060556, - 883997877, - 958139571, - 1322822218, - 1537002063, - 1747873779, - 1955562222, - 2024104815, - -2067236844, - -1933114872, - -1866530822, - -1538233109, - -1090935817, - -965641998 -]; - -var sha384 = [ - /* int64 */[ - /* hi */1116352408, - /* lo */3609767458 - ], - /* int64 */[ - /* hi */1899447441, - /* lo */602891725 - ], - /* int64 */[ - /* hi */-1245643825, - /* lo */3964484399 - ], - /* int64 */[ - /* hi */-373957723, - /* lo */2173295548 - ], - /* int64 */[ - /* hi */961987163, - /* lo */4081628472 - ], - /* int64 */[ - /* hi */1508970993, - /* lo */3053834265 - ], - /* int64 */[ - /* hi */-1841331548, - /* lo */2937671579 - ], - /* int64 */[ - /* hi */-1424204075, - /* lo */3664609560 - ], - /* int64 */[ - /* hi */-670586216, - /* lo */2734883394 - ], - /* int64 */[ - /* hi */310598401, - /* lo */1164996542 - ], - /* int64 */[ - /* hi */607225278, - /* lo */1323610764 - ], - /* int64 */[ - /* hi */1426881987, - /* lo */3590304994 - ], - /* int64 */[ - /* hi */1925078388, - /* lo */4068182383 - ], - /* int64 */[ - /* hi */-2132889090, - /* lo */991336113 - ], - /* int64 */[ - /* hi */-1680079193, - /* lo */633803317 - ], - /* int64 */[ - /* hi */-1046744716, - /* lo */3479774868 - ], - /* int64 */[ - /* hi */-459576895, - /* lo */2666613458 - ], - /* int64 */[ - /* hi */-272742522, - /* lo */944711139 - ], - /* int64 */[ - /* hi */264347078, - /* lo */2341262773 - ], - /* int64 */[ - /* hi */604807628, - /* lo */2007800933 - ], - /* int64 */[ - /* hi */770255983, - /* lo */1495990901 - ], - /* int64 */[ - /* hi */1249150122, - /* lo */1856431235 - ], - /* int64 */[ - /* hi */1555081692, - /* lo */3175218132 - ], - /* int64 */[ - /* hi */1996064986, - /* lo */2198950837 - ], - /* int64 */[ - /* hi */-1740746414, - /* lo */3999719339 - ], - /* int64 */[ - /* hi */-1473132947, - /* lo */766784016 - ], - /* int64 */[ - /* hi */-1341970488, - /* lo */2566594879 - ], - /* int64 */[ - /* hi */-1084653625, - /* lo */3203337956 - ], - /* int64 */[ - /* hi */-958395405, - /* lo */1034457026 - ], - /* int64 */[ - /* hi */-710438585, - /* lo */2466948901 - ], - /* int64 */[ - /* hi */113926993, - /* lo */3758326383 - ], - /* int64 */[ - /* hi */338241895, - /* lo */168717936 - ], - /* int64 */[ - /* hi */666307205, - /* lo */1188179964 - ], - /* int64 */[ - /* hi */773529912, - /* lo */1546045734 - ], - /* int64 */[ - /* hi */1294757372, - /* lo */1522805485 - ], - /* int64 */[ - /* hi */1396182291, - /* lo */2643833823 - ], - /* int64 */[ - /* hi */1695183700, - /* lo */2343527390 - ], - /* int64 */[ - /* hi */1986661051, - /* lo */1014477480 - ], - /* int64 */[ - /* hi */-2117940946, - /* lo */1206759142 - ], - /* int64 */[ - /* hi */-1838011259, - /* lo */344077627 - ], - /* int64 */[ - /* hi */-1564481375, - /* lo */1290863460 - ], - /* int64 */[ - /* hi */-1474664885, - /* lo */3158454273 - ], - /* int64 */[ - /* hi */-1035236496, - /* lo */3505952657 - ], - /* int64 */[ - /* hi */-949202525, - /* lo */106217008 - ], - /* int64 */[ - /* hi */-778901479, - /* lo */3606008344 - ], - /* int64 */[ - /* hi */-694614492, - /* lo */1432725776 - ], - /* int64 */[ - /* hi */-200395387, - /* lo */1467031594 - ], - /* int64 */[ - /* hi */275423344, - /* lo */851169720 - ], - /* int64 */[ - /* hi */430227734, - /* lo */3100823752 - ], - /* int64 */[ - /* hi */506948616, - /* lo */1363258195 - ], - /* int64 */[ - /* hi */659060556, - /* lo */3750685593 - ], - /* int64 */[ - /* hi */883997877, - /* lo */3785050280 - ], - /* int64 */[ - /* hi */958139571, - /* lo */3318307427 - ], - /* int64 */[ - /* hi */1322822218, - /* lo */3812723403 - ], - /* int64 */[ - /* hi */1537002063, - /* lo */2003034995 - ], - /* int64 */[ - /* hi */1747873779, - /* lo */3602036899 - ], - /* int64 */[ - /* hi */1955562222, - /* lo */1575990012 - ], - /* int64 */[ - /* hi */2024104815, - /* lo */1125592928 - ], - /* int64 */[ - /* hi */-2067236844, - /* lo */2716904306 - ], - /* int64 */[ - /* hi */-1933114872, - /* lo */442776044 - ], - /* int64 */[ - /* hi */-1866530822, - /* lo */593698344 - ], - /* int64 */[ - /* hi */-1538233109, - /* lo */3733110249 - ], - /* int64 */[ - /* hi */-1090935817, - /* lo */2999351573 - ], - /* int64 */[ - /* hi */-965641998, - /* lo */3815920427 - ], - /* int64 */[ - /* hi */-903397682, - /* lo */3928383900 - ], - /* int64 */[ - /* hi */-779700025, - /* lo */566280711 - ], - /* int64 */[ - /* hi */-354779690, - /* lo */3454069534 - ], - /* int64 */[ - /* hi */-176337025, - /* lo */4000239992 - ], - /* int64 */[ - /* hi */116418474, - /* lo */1914138554 - ], - /* int64 */[ - /* hi */174292421, - /* lo */2731055270 - ], - /* int64 */[ - /* hi */289380356, - /* lo */3203993006 - ], - /* int64 */[ - /* hi */460393269, - /* lo */320620315 - ], - /* int64 */[ - /* hi */685471733, - /* lo */587496836 - ], - /* int64 */[ - /* hi */852142971, - /* lo */1086792851 - ], - /* int64 */[ - /* hi */1017036298, - /* lo */365543100 - ], - /* int64 */[ - /* hi */1126000580, - /* lo */2618297676 - ], - /* int64 */[ - /* hi */1288033470, - /* lo */3409855158 - ], - /* int64 */[ - /* hi */1501505948, - /* lo */4234509866 - ], - /* int64 */[ - /* hi */1607167915, - /* lo */987167468 - ], - /* int64 */[ - /* hi */1816402316, - /* lo */1246189591 - ] -]; - -var sha512 = [ - /* int64 */[ - /* hi */1116352408, - /* lo */3609767458 - ], - /* int64 */[ - /* hi */1899447441, - /* lo */602891725 - ], - /* int64 */[ - /* hi */-1245643825, - /* lo */3964484399 - ], - /* int64 */[ - /* hi */-373957723, - /* lo */2173295548 - ], - /* int64 */[ - /* hi */961987163, - /* lo */4081628472 - ], - /* int64 */[ - /* hi */1508970993, - /* lo */3053834265 - ], - /* int64 */[ - /* hi */-1841331548, - /* lo */2937671579 - ], - /* int64 */[ - /* hi */-1424204075, - /* lo */3664609560 - ], - /* int64 */[ - /* hi */-670586216, - /* lo */2734883394 - ], - /* int64 */[ - /* hi */310598401, - /* lo */1164996542 - ], - /* int64 */[ - /* hi */607225278, - /* lo */1323610764 - ], - /* int64 */[ - /* hi */1426881987, - /* lo */3590304994 - ], - /* int64 */[ - /* hi */1925078388, - /* lo */4068182383 - ], - /* int64 */[ - /* hi */-2132889090, - /* lo */991336113 - ], - /* int64 */[ - /* hi */-1680079193, - /* lo */633803317 - ], - /* int64 */[ - /* hi */-1046744716, - /* lo */3479774868 - ], - /* int64 */[ - /* hi */-459576895, - /* lo */2666613458 - ], - /* int64 */[ - /* hi */-272742522, - /* lo */944711139 - ], - /* int64 */[ - /* hi */264347078, - /* lo */2341262773 - ], - /* int64 */[ - /* hi */604807628, - /* lo */2007800933 - ], - /* int64 */[ - /* hi */770255983, - /* lo */1495990901 - ], - /* int64 */[ - /* hi */1249150122, - /* lo */1856431235 - ], - /* int64 */[ - /* hi */1555081692, - /* lo */3175218132 - ], - /* int64 */[ - /* hi */1996064986, - /* lo */2198950837 - ], - /* int64 */[ - /* hi */-1740746414, - /* lo */3999719339 - ], - /* int64 */[ - /* hi */-1473132947, - /* lo */766784016 - ], - /* int64 */[ - /* hi */-1341970488, - /* lo */2566594879 - ], - /* int64 */[ - /* hi */-1084653625, - /* lo */3203337956 - ], - /* int64 */[ - /* hi */-958395405, - /* lo */1034457026 - ], - /* int64 */[ - /* hi */-710438585, - /* lo */2466948901 - ], - /* int64 */[ - /* hi */113926993, - /* lo */3758326383 - ], - /* int64 */[ - /* hi */338241895, - /* lo */168717936 - ], - /* int64 */[ - /* hi */666307205, - /* lo */1188179964 - ], - /* int64 */[ - /* hi */773529912, - /* lo */1546045734 - ], - /* int64 */[ - /* hi */1294757372, - /* lo */1522805485 - ], - /* int64 */[ - /* hi */1396182291, - /* lo */2643833823 - ], - /* int64 */[ - /* hi */1695183700, - /* lo */2343527390 - ], - /* int64 */[ - /* hi */1986661051, - /* lo */1014477480 - ], - /* int64 */[ - /* hi */-2117940946, - /* lo */1206759142 - ], - /* int64 */[ - /* hi */-1838011259, - /* lo */344077627 - ], - /* int64 */[ - /* hi */-1564481375, - /* lo */1290863460 - ], - /* int64 */[ - /* hi */-1474664885, - /* lo */3158454273 - ], - /* int64 */[ - /* hi */-1035236496, - /* lo */3505952657 - ], - /* int64 */[ - /* hi */-949202525, - /* lo */106217008 - ], - /* int64 */[ - /* hi */-778901479, - /* lo */3606008344 - ], - /* int64 */[ - /* hi */-694614492, - /* lo */1432725776 - ], - /* int64 */[ - /* hi */-200395387, - /* lo */1467031594 - ], - /* int64 */[ - /* hi */275423344, - /* lo */851169720 - ], - /* int64 */[ - /* hi */430227734, - /* lo */3100823752 - ], - /* int64 */[ - /* hi */506948616, - /* lo */1363258195 - ], - /* int64 */[ - /* hi */659060556, - /* lo */3750685593 - ], - /* int64 */[ - /* hi */883997877, - /* lo */3785050280 - ], - /* int64 */[ - /* hi */958139571, - /* lo */3318307427 - ], - /* int64 */[ - /* hi */1322822218, - /* lo */3812723403 - ], - /* int64 */[ - /* hi */1537002063, - /* lo */2003034995 - ], - /* int64 */[ - /* hi */1747873779, - /* lo */3602036899 - ], - /* int64 */[ - /* hi */1955562222, - /* lo */1575990012 - ], - /* int64 */[ - /* hi */2024104815, - /* lo */1125592928 - ], - /* int64 */[ - /* hi */-2067236844, - /* lo */2716904306 - ], - /* int64 */[ - /* hi */-1933114872, - /* lo */442776044 - ], - /* int64 */[ - /* hi */-1866530822, - /* lo */593698344 - ], - /* int64 */[ - /* hi */-1538233109, - /* lo */3733110249 - ], - /* int64 */[ - /* hi */-1090935817, - /* lo */2999351573 - ], - /* int64 */[ - /* hi */-965641998, - /* lo */3815920427 - ], - /* int64 */[ - /* hi */-903397682, - /* lo */3928383900 - ], - /* int64 */[ - /* hi */-779700025, - /* lo */566280711 - ], - /* int64 */[ - /* hi */-354779690, - /* lo */3454069534 - ], - /* int64 */[ - /* hi */-176337025, - /* lo */4000239992 - ], - /* int64 */[ - /* hi */116418474, - /* lo */1914138554 - ], - /* int64 */[ - /* hi */174292421, - /* lo */2731055270 - ], - /* int64 */[ - /* hi */289380356, - /* lo */3203993006 - ], - /* int64 */[ - /* hi */460393269, - /* lo */320620315 - ], - /* int64 */[ - /* hi */685471733, - /* lo */587496836 - ], - /* int64 */[ - /* hi */852142971, - /* lo */1086792851 - ], - /* int64 */[ - /* hi */1017036298, - /* lo */365543100 - ], - /* int64 */[ - /* hi */1126000580, - /* lo */2618297676 - ], - /* int64 */[ - /* hi */1288033470, - /* lo */3409855158 - ], - /* int64 */[ - /* hi */1501505948, - /* lo */4234509866 - ], - /* int64 */[ - /* hi */1607167915, - /* lo */987167468 - ], - /* int64 */[ - /* hi */1816402316, - /* lo */1246189591 - ] -]; - -exports.sha1 = sha1; -exports.sha224 = sha224; -exports.sha256 = sha256; -exports.sha384 = sha384; -exports.sha512 = sha512; -/* No side effect */ diff --git a/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js b/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js index 057f215..9e87a42 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js @@ -9,6 +9,14 @@ function rotl(x, n) { return (x << n) | (x >>> (64 - n | 0)) | 0; } +var MD5 = { + rotl: rotl +}; + +function rotl$1(x, n) { + return (x << n) | (x >>> (64 - n | 0)) | 0; +} + function parity(x, y, z) { return x ^ y ^ z; } @@ -91,7 +99,7 @@ function sigma1$1(x) { var Sha1 = { f: f, - rotl: rotl + rotl: rotl$1 }; var Sha224_256 = { @@ -112,6 +120,7 @@ var Sha384_512 = { sigma1: sigma1$1 }; +exports.MD5 = MD5; exports.Sha1 = Sha1; exports.Sha224_256 = Sha224_256; exports.Sha384_512 = Sha384_512; diff --git a/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js b/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js new file mode 100644 index 0000000..d4329c0 --- /dev/null +++ b/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js @@ -0,0 +1,177 @@ +// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE +'use strict'; + +var Char = require("bs-platform/lib/js/char.js"); +var Bytes = require("bs-platform/lib/js/bytes.js"); +var Caml_array = require("bs-platform/lib/js/caml_array.js"); +var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); +var Caml_int32 = require("bs-platform/lib/js/caml_int32.js"); +var Pervasives = require("bs-platform/lib/js/pervasives.js"); +var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); +var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); + +var constants = [ + -680876936, + -389564586, + 606105819, + -1044525330, + -176418897, + 1200080426, + -1473231341, + -45705983, + 1770035416, + -1958414417, + -42063, + -1990404162, + 1804603682, + -40341101, + -1502002290, + 1236535329, + -165796510, + -1069501632, + 643717713, + -373897302, + -701558691, + 38016083, + -660478335, + -405537848, + 568446438, + -1019803690, + -187363961, + 1163531501, + -1444681467, + -51403784, + 1735328473, + -1926607734, + -378558, + -2022574463, + 1839030562, + -35309556, + -1530992060, + 1272893353, + -155497632, + -1094730640, + 681279174, + -358537222, + -722521979, + 76029189, + -640364487, + -421815835, + 530742520, + -995338651, + -198630844, + 1126891415, + -1416354905, + -57434055, + 1700485571, + -1894986606, + -1051523, + -2054922799, + 1873313359, + -30611744, + -1560198380, + 1309151649, + -145523070, + -1120210379, + 718787259, + -343485551 +]; + +var s = [ + 7, + 12, + 17, + 22, + 5, + 9, + 14, + 20, + 4, + 11, + 16, + 23, + 6, + 10, + 15, + 21 +]; + +var initialHash = [ + 1732584193, + -271733879, + -1732584194, + 271733878 +]; + +function preprocess(msgBytes) { + var size = (msgBytes.length + 8 | 0) + 1 | 0; + while(size % 64 !== 0) { + size = size + 1 | 0; + }; + var length = msgBytes.length; + var bytes = Bytes.init(size, (function (index) { + if (index < length) { + return Caml_bytes.get(msgBytes, index); + } else if (index === length) { + return Char.chr(128); + } else { + return Char.chr(0); + } + })); + var va = (length << 3); + for(var i = 0; i <= 7; ++i){ + bytes[i + (size - 8 | 0) | 0] = Char.chr(va & 255); + va = (va >>> 8); + } + return bytes; +} + +function make(message) { + var messageBytes = Bytes.of_string(message); + var message$1 = preprocess(messageBytes); + var chunks = ((message$1.length + 8 | 0) >> 6); + var a = Caml_array.caml_array_get(initialHash, 0); + var b = Caml_array.caml_array_get(initialHash, 1); + var c = Caml_array.caml_array_get(initialHash, 2); + var d = Caml_array.caml_array_get(initialHash, 3); + var w = Caml_array.caml_make_vect(16, 0); + for(var i = 0 ,i_finish = chunks - 1 | 0; i <= i_finish; ++i){ + for(var j = 0; j <= 63; ++j){ + Caml_array.caml_array_set(w, (j >>> 2), (Caml_bytes.get(message$1, (i << 6) + j | 0) << 24) | (Caml_array.caml_array_get(w, (j >>> 2)) >>> 8)); + } + var originalA = a; + var originalB = b; + var originalC = c; + var originalD = d; + for(var j$1 = 0; j$1 <= 63; ++j$1){ + var f = 0 <= j$1 && j$1 <= 15 ? b & c | Pervasives.lnot(b) & d : ( + 16 <= j$1 && j$1 <= 31 ? b & d | c & Pervasives.lnot(d) : ( + 32 <= j$1 && j$1 <= 47 ? b ^ c ^ d : c ^ (b | Pervasives.lnot(d)) + ) + ); + var g = 0 <= j$1 && j$1 <= 15 ? j$1 : ( + 16 <= j$1 && j$1 <= 31 ? (Caml_int32.imul(5, j$1) + 1 | 0) % 16 : ( + 32 <= j$1 && j$1 <= 47 ? (Caml_int32.imul(3, j$1) + 5 | 0) % 16 : Caml_int32.imul(7, j$1) % 16 + ) + ); + var temp = b + ReCrypt_Functions.MD5.rotl(((a + f | 0) + Caml_array.caml_array_get(w, g) | 0) + Caml_array.caml_array_get(constants, j$1) | 0, Caml_array.caml_array_get(s, ((j$1 >>> 4) << 2) | j$1 & 3)) | 0; + a = d; + d = c; + c = b; + b = temp; + } + a = originalA + a | 0; + b = originalB + b | 0; + c = originalC + c | 0; + d = originalD + d | 0; + } + var md5 = Bytes.make(16, Char.chr(0)); + ReCrypt_Utils.append(md5, a, 0); + ReCrypt_Utils.append(md5, b, 4); + ReCrypt_Utils.append(md5, c, 8); + ReCrypt_Utils.append(md5, d, 12); + return ReCrypt_Utils.stringToHex(Bytes.to_string(md5)); +} + +exports.make = make; +/* No side effect */ diff --git a/lib/js/src/ReCrypt/ReCrypt_Preprocess.bs.js b/lib/js/src/ReCrypt/ReCrypt_Preprocess.bs.js index 6f53f50..a76ba22 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Preprocess.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Preprocess.bs.js @@ -4,7 +4,6 @@ var Char = require("bs-platform/lib/js/char.js"); var Bytes = require("bs-platform/lib/js/bytes.js"); var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); -var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); function pad(msgBytes) { var size = (msgBytes.length + 8 | 0) + 1 | 0; @@ -21,7 +20,12 @@ function pad(msgBytes) { return Char.chr(0); } })); - return ReCrypt_Utils.toBytes((length << 3), bytes); + var va = (length << 3); + for(var i = 1; i <= 15; ++i){ + bytes[bytes.length - i | 0] = Char.chr(va & 255); + va = (va >> 8); + } + return bytes; } var Sha1_224_256 = { @@ -43,7 +47,12 @@ function pad$1(msgBytes) { return Char.chr(0); } })); - return ReCrypt_Utils.toBytes((length << 3), bytes); + var va = (length << 3); + for(var i = 1; i <= 15; ++i){ + bytes[bytes.length - i | 0] = Char.chr(va & 255); + va = (va >> 8); + } + return bytes; } var Sha384_512 = { diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js index acc9110..4c3f33b 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js @@ -6,10 +6,23 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Constants = require("./ReCrypt_Constants.bs.js"); var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +function constants(t) { + if (t >= 0 && t <= 19) { + return 1518500249; + } else if (t >= 20 && t <= 39) { + return 1859775393; + } else if (t >= 40 && t <= 59) { + return -1894007588; + } else if (t >= 60 && t <= 79) { + return -899497514; + } else { + return 0; + } +} + var initialHash = [ 1732584193, -271733879, @@ -43,7 +56,7 @@ function make(message) { var originalD = d; var originalE = e; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp = (((ReCrypt_Functions.Sha1.rotl(a, 5) + ReCrypt_Functions.Sha1.f(t$2, b, c, d) | 0) + e | 0) + ReCrypt_Constants.sha1(t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp = (((ReCrypt_Functions.Sha1.rotl(a, 5) + ReCrypt_Functions.Sha1.f(t$2, b, c, d) | 0) + e | 0) + constants(t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; e = d; d = c; c = ReCrypt_Functions.Sha1.rotl(b, 30); diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js index 8f2fb6d..b321dcc 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js @@ -6,10 +6,76 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Constants = require("./ReCrypt_Constants.bs.js"); var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var constants = [ + 1116352408, + 1899447441, + -1245643825, + -373957723, + 961987163, + 1508970993, + -1841331548, + -1424204075, + -670586216, + 310598401, + 607225278, + 1426881987, + 1925078388, + -2132889090, + -1680079193, + -1046744716, + -459576895, + -272742522, + 264347078, + 604807628, + 770255983, + 1249150122, + 1555081692, + 1996064986, + -1740746414, + -1473132947, + -1341970488, + -1084653625, + -958395405, + -710438585, + 113926993, + 338241895, + 666307205, + 773529912, + 1294757372, + 1396182291, + 1695183700, + 1986661051, + -2117940946, + -1838011259, + -1564481375, + -1474664885, + -1035236496, + -949202525, + -778901479, + -694614492, + -200395387, + 275423344, + 430227734, + 506948616, + 659060556, + 883997877, + 958139571, + 1322822218, + 1537002063, + 1747873779, + 1955562222, + 2024104815, + -2067236844, + -1933114872, + -1866530822, + -1538233109, + -1090935817, + -965641998 +]; + var initialHash = [ -1056596264, 914150663, @@ -52,7 +118,7 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 63; ++t$2){ - var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(ReCrypt_Constants.sha224, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; var temp2 = ReCrypt_Functions.Sha224_256.sum0(a) + ReCrypt_Functions.Sha224_256.majority(a, b, c) | 0; h = g; g = f; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js index 2f2a81d..754e3c7 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js @@ -6,10 +6,76 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Constants = require("./ReCrypt_Constants.bs.js"); var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var constants = [ + 1116352408, + 1899447441, + -1245643825, + -373957723, + 961987163, + 1508970993, + -1841331548, + -1424204075, + -670586216, + 310598401, + 607225278, + 1426881987, + 1925078388, + -2132889090, + -1680079193, + -1046744716, + -459576895, + -272742522, + 264347078, + 604807628, + 770255983, + 1249150122, + 1555081692, + 1996064986, + -1740746414, + -1473132947, + -1341970488, + -1084653625, + -958395405, + -710438585, + 113926993, + 338241895, + 666307205, + 773529912, + 1294757372, + 1396182291, + 1695183700, + 1986661051, + -2117940946, + -1838011259, + -1564481375, + -1474664885, + -1035236496, + -949202525, + -778901479, + -694614492, + -200395387, + 275423344, + 430227734, + 506948616, + 659060556, + 883997877, + 958139571, + 1322822218, + 1537002063, + 1747873779, + 1955562222, + 2024104815, + -2067236844, + -1933114872, + -1866530822, + -1538233109, + -1090935817, + -965641998 +]; + var initialHash = [ 1779033703, -1150833019, @@ -52,7 +118,7 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 63; ++t$2){ - var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(ReCrypt_Constants.sha256, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; var temp2 = ReCrypt_Functions.Sha224_256.sum0(a) + ReCrypt_Functions.Sha224_256.majority(a, b, c) | 0; h = g; g = f; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js index c69c47e..9fd49ff 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js @@ -5,12 +5,335 @@ var Char = require("bs-platform/lib/js/char.js"); var Bytes = require("bs-platform/lib/js/bytes.js"); var Int64 = require("bs-platform/lib/js/int64.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); +var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Constants = require("./ReCrypt_Constants.bs.js"); var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var constants = [ + /* int64 */[ + /* hi */1116352408, + /* lo */3609767458 + ], + /* int64 */[ + /* hi */1899447441, + /* lo */602891725 + ], + /* int64 */[ + /* hi */-1245643825, + /* lo */3964484399 + ], + /* int64 */[ + /* hi */-373957723, + /* lo */2173295548 + ], + /* int64 */[ + /* hi */961987163, + /* lo */4081628472 + ], + /* int64 */[ + /* hi */1508970993, + /* lo */3053834265 + ], + /* int64 */[ + /* hi */-1841331548, + /* lo */2937671579 + ], + /* int64 */[ + /* hi */-1424204075, + /* lo */3664609560 + ], + /* int64 */[ + /* hi */-670586216, + /* lo */2734883394 + ], + /* int64 */[ + /* hi */310598401, + /* lo */1164996542 + ], + /* int64 */[ + /* hi */607225278, + /* lo */1323610764 + ], + /* int64 */[ + /* hi */1426881987, + /* lo */3590304994 + ], + /* int64 */[ + /* hi */1925078388, + /* lo */4068182383 + ], + /* int64 */[ + /* hi */-2132889090, + /* lo */991336113 + ], + /* int64 */[ + /* hi */-1680079193, + /* lo */633803317 + ], + /* int64 */[ + /* hi */-1046744716, + /* lo */3479774868 + ], + /* int64 */[ + /* hi */-459576895, + /* lo */2666613458 + ], + /* int64 */[ + /* hi */-272742522, + /* lo */944711139 + ], + /* int64 */[ + /* hi */264347078, + /* lo */2341262773 + ], + /* int64 */[ + /* hi */604807628, + /* lo */2007800933 + ], + /* int64 */[ + /* hi */770255983, + /* lo */1495990901 + ], + /* int64 */[ + /* hi */1249150122, + /* lo */1856431235 + ], + /* int64 */[ + /* hi */1555081692, + /* lo */3175218132 + ], + /* int64 */[ + /* hi */1996064986, + /* lo */2198950837 + ], + /* int64 */[ + /* hi */-1740746414, + /* lo */3999719339 + ], + /* int64 */[ + /* hi */-1473132947, + /* lo */766784016 + ], + /* int64 */[ + /* hi */-1341970488, + /* lo */2566594879 + ], + /* int64 */[ + /* hi */-1084653625, + /* lo */3203337956 + ], + /* int64 */[ + /* hi */-958395405, + /* lo */1034457026 + ], + /* int64 */[ + /* hi */-710438585, + /* lo */2466948901 + ], + /* int64 */[ + /* hi */113926993, + /* lo */3758326383 + ], + /* int64 */[ + /* hi */338241895, + /* lo */168717936 + ], + /* int64 */[ + /* hi */666307205, + /* lo */1188179964 + ], + /* int64 */[ + /* hi */773529912, + /* lo */1546045734 + ], + /* int64 */[ + /* hi */1294757372, + /* lo */1522805485 + ], + /* int64 */[ + /* hi */1396182291, + /* lo */2643833823 + ], + /* int64 */[ + /* hi */1695183700, + /* lo */2343527390 + ], + /* int64 */[ + /* hi */1986661051, + /* lo */1014477480 + ], + /* int64 */[ + /* hi */-2117940946, + /* lo */1206759142 + ], + /* int64 */[ + /* hi */-1838011259, + /* lo */344077627 + ], + /* int64 */[ + /* hi */-1564481375, + /* lo */1290863460 + ], + /* int64 */[ + /* hi */-1474664885, + /* lo */3158454273 + ], + /* int64 */[ + /* hi */-1035236496, + /* lo */3505952657 + ], + /* int64 */[ + /* hi */-949202525, + /* lo */106217008 + ], + /* int64 */[ + /* hi */-778901479, + /* lo */3606008344 + ], + /* int64 */[ + /* hi */-694614492, + /* lo */1432725776 + ], + /* int64 */[ + /* hi */-200395387, + /* lo */1467031594 + ], + /* int64 */[ + /* hi */275423344, + /* lo */851169720 + ], + /* int64 */[ + /* hi */430227734, + /* lo */3100823752 + ], + /* int64 */[ + /* hi */506948616, + /* lo */1363258195 + ], + /* int64 */[ + /* hi */659060556, + /* lo */3750685593 + ], + /* int64 */[ + /* hi */883997877, + /* lo */3785050280 + ], + /* int64 */[ + /* hi */958139571, + /* lo */3318307427 + ], + /* int64 */[ + /* hi */1322822218, + /* lo */3812723403 + ], + /* int64 */[ + /* hi */1537002063, + /* lo */2003034995 + ], + /* int64 */[ + /* hi */1747873779, + /* lo */3602036899 + ], + /* int64 */[ + /* hi */1955562222, + /* lo */1575990012 + ], + /* int64 */[ + /* hi */2024104815, + /* lo */1125592928 + ], + /* int64 */[ + /* hi */-2067236844, + /* lo */2716904306 + ], + /* int64 */[ + /* hi */-1933114872, + /* lo */442776044 + ], + /* int64 */[ + /* hi */-1866530822, + /* lo */593698344 + ], + /* int64 */[ + /* hi */-1538233109, + /* lo */3733110249 + ], + /* int64 */[ + /* hi */-1090935817, + /* lo */2999351573 + ], + /* int64 */[ + /* hi */-965641998, + /* lo */3815920427 + ], + /* int64 */[ + /* hi */-903397682, + /* lo */3928383900 + ], + /* int64 */[ + /* hi */-779700025, + /* lo */566280711 + ], + /* int64 */[ + /* hi */-354779690, + /* lo */3454069534 + ], + /* int64 */[ + /* hi */-176337025, + /* lo */4000239992 + ], + /* int64 */[ + /* hi */116418474, + /* lo */1914138554 + ], + /* int64 */[ + /* hi */174292421, + /* lo */2731055270 + ], + /* int64 */[ + /* hi */289380356, + /* lo */3203993006 + ], + /* int64 */[ + /* hi */460393269, + /* lo */320620315 + ], + /* int64 */[ + /* hi */685471733, + /* lo */587496836 + ], + /* int64 */[ + /* hi */852142971, + /* lo */1086792851 + ], + /* int64 */[ + /* hi */1017036298, + /* lo */365543100 + ], + /* int64 */[ + /* hi */1126000580, + /* lo */2618297676 + ], + /* int64 */[ + /* hi */1288033470, + /* lo */3409855158 + ], + /* int64 */[ + /* hi */1501505948, + /* lo */4234509866 + ], + /* int64 */[ + /* hi */1607167915, + /* lo */987167468 + ], + /* int64 */[ + /* hi */1816402316, + /* lo */1246189591 + ] +]; + var initialHash = [ /* int64 */[ /* hi */-876896931, @@ -61,7 +384,22 @@ function make(message) { for(var i = 0 ,i_finish = chunks - 1 | 0; i <= i_finish; ++i){ var w = Caml_array.caml_make_vect(80, Int64.zero); for(var t = 0; t <= 15; ++t){ - Caml_array.caml_array_set(w, t, ReCrypt_Utils.int64_of_bytes(message$1, (i << 7) + (t << 3) | 0)); + for(var j = 0; j <= 7; ++j){ + var tmp; + var exit = 0; + var some; + try { + some = Caml_bytes.get(message$1, j + ((i << 7) + (t << 3) | 0) | 0); + exit = 1; + } + catch (exn){ + tmp = Int64.zero; + } + if (exit === 1) { + tmp = Caml_int64.of_int32(some & 255); + } + Caml_array.caml_array_set(w, t, Caml_int64.add(Caml_int64.lsl_(Caml_array.caml_array_get(w, t), 8), tmp)); + } } for(var t$1 = 16; t$1 <= 79; ++t$1){ Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions.Sha384_512.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions.Sha384_512.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); @@ -75,7 +413,7 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(ReCrypt_Constants.sha384, t$2)), Caml_array.caml_array_get(w, t$2)); + var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); var temp2 = Caml_int64.add(ReCrypt_Functions.Sha384_512.sum0(a), ReCrypt_Functions.Sha384_512.majority(a, b, c)); h = g; g = f; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js index a64a238..808dd64 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js @@ -5,12 +5,335 @@ var Char = require("bs-platform/lib/js/char.js"); var Bytes = require("bs-platform/lib/js/bytes.js"); var Int64 = require("bs-platform/lib/js/int64.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); +var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Constants = require("./ReCrypt_Constants.bs.js"); var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var constants = [ + /* int64 */[ + /* hi */1116352408, + /* lo */3609767458 + ], + /* int64 */[ + /* hi */1899447441, + /* lo */602891725 + ], + /* int64 */[ + /* hi */-1245643825, + /* lo */3964484399 + ], + /* int64 */[ + /* hi */-373957723, + /* lo */2173295548 + ], + /* int64 */[ + /* hi */961987163, + /* lo */4081628472 + ], + /* int64 */[ + /* hi */1508970993, + /* lo */3053834265 + ], + /* int64 */[ + /* hi */-1841331548, + /* lo */2937671579 + ], + /* int64 */[ + /* hi */-1424204075, + /* lo */3664609560 + ], + /* int64 */[ + /* hi */-670586216, + /* lo */2734883394 + ], + /* int64 */[ + /* hi */310598401, + /* lo */1164996542 + ], + /* int64 */[ + /* hi */607225278, + /* lo */1323610764 + ], + /* int64 */[ + /* hi */1426881987, + /* lo */3590304994 + ], + /* int64 */[ + /* hi */1925078388, + /* lo */4068182383 + ], + /* int64 */[ + /* hi */-2132889090, + /* lo */991336113 + ], + /* int64 */[ + /* hi */-1680079193, + /* lo */633803317 + ], + /* int64 */[ + /* hi */-1046744716, + /* lo */3479774868 + ], + /* int64 */[ + /* hi */-459576895, + /* lo */2666613458 + ], + /* int64 */[ + /* hi */-272742522, + /* lo */944711139 + ], + /* int64 */[ + /* hi */264347078, + /* lo */2341262773 + ], + /* int64 */[ + /* hi */604807628, + /* lo */2007800933 + ], + /* int64 */[ + /* hi */770255983, + /* lo */1495990901 + ], + /* int64 */[ + /* hi */1249150122, + /* lo */1856431235 + ], + /* int64 */[ + /* hi */1555081692, + /* lo */3175218132 + ], + /* int64 */[ + /* hi */1996064986, + /* lo */2198950837 + ], + /* int64 */[ + /* hi */-1740746414, + /* lo */3999719339 + ], + /* int64 */[ + /* hi */-1473132947, + /* lo */766784016 + ], + /* int64 */[ + /* hi */-1341970488, + /* lo */2566594879 + ], + /* int64 */[ + /* hi */-1084653625, + /* lo */3203337956 + ], + /* int64 */[ + /* hi */-958395405, + /* lo */1034457026 + ], + /* int64 */[ + /* hi */-710438585, + /* lo */2466948901 + ], + /* int64 */[ + /* hi */113926993, + /* lo */3758326383 + ], + /* int64 */[ + /* hi */338241895, + /* lo */168717936 + ], + /* int64 */[ + /* hi */666307205, + /* lo */1188179964 + ], + /* int64 */[ + /* hi */773529912, + /* lo */1546045734 + ], + /* int64 */[ + /* hi */1294757372, + /* lo */1522805485 + ], + /* int64 */[ + /* hi */1396182291, + /* lo */2643833823 + ], + /* int64 */[ + /* hi */1695183700, + /* lo */2343527390 + ], + /* int64 */[ + /* hi */1986661051, + /* lo */1014477480 + ], + /* int64 */[ + /* hi */-2117940946, + /* lo */1206759142 + ], + /* int64 */[ + /* hi */-1838011259, + /* lo */344077627 + ], + /* int64 */[ + /* hi */-1564481375, + /* lo */1290863460 + ], + /* int64 */[ + /* hi */-1474664885, + /* lo */3158454273 + ], + /* int64 */[ + /* hi */-1035236496, + /* lo */3505952657 + ], + /* int64 */[ + /* hi */-949202525, + /* lo */106217008 + ], + /* int64 */[ + /* hi */-778901479, + /* lo */3606008344 + ], + /* int64 */[ + /* hi */-694614492, + /* lo */1432725776 + ], + /* int64 */[ + /* hi */-200395387, + /* lo */1467031594 + ], + /* int64 */[ + /* hi */275423344, + /* lo */851169720 + ], + /* int64 */[ + /* hi */430227734, + /* lo */3100823752 + ], + /* int64 */[ + /* hi */506948616, + /* lo */1363258195 + ], + /* int64 */[ + /* hi */659060556, + /* lo */3750685593 + ], + /* int64 */[ + /* hi */883997877, + /* lo */3785050280 + ], + /* int64 */[ + /* hi */958139571, + /* lo */3318307427 + ], + /* int64 */[ + /* hi */1322822218, + /* lo */3812723403 + ], + /* int64 */[ + /* hi */1537002063, + /* lo */2003034995 + ], + /* int64 */[ + /* hi */1747873779, + /* lo */3602036899 + ], + /* int64 */[ + /* hi */1955562222, + /* lo */1575990012 + ], + /* int64 */[ + /* hi */2024104815, + /* lo */1125592928 + ], + /* int64 */[ + /* hi */-2067236844, + /* lo */2716904306 + ], + /* int64 */[ + /* hi */-1933114872, + /* lo */442776044 + ], + /* int64 */[ + /* hi */-1866530822, + /* lo */593698344 + ], + /* int64 */[ + /* hi */-1538233109, + /* lo */3733110249 + ], + /* int64 */[ + /* hi */-1090935817, + /* lo */2999351573 + ], + /* int64 */[ + /* hi */-965641998, + /* lo */3815920427 + ], + /* int64 */[ + /* hi */-903397682, + /* lo */3928383900 + ], + /* int64 */[ + /* hi */-779700025, + /* lo */566280711 + ], + /* int64 */[ + /* hi */-354779690, + /* lo */3454069534 + ], + /* int64 */[ + /* hi */-176337025, + /* lo */4000239992 + ], + /* int64 */[ + /* hi */116418474, + /* lo */1914138554 + ], + /* int64 */[ + /* hi */174292421, + /* lo */2731055270 + ], + /* int64 */[ + /* hi */289380356, + /* lo */3203993006 + ], + /* int64 */[ + /* hi */460393269, + /* lo */320620315 + ], + /* int64 */[ + /* hi */685471733, + /* lo */587496836 + ], + /* int64 */[ + /* hi */852142971, + /* lo */1086792851 + ], + /* int64 */[ + /* hi */1017036298, + /* lo */365543100 + ], + /* int64 */[ + /* hi */1126000580, + /* lo */2618297676 + ], + /* int64 */[ + /* hi */1288033470, + /* lo */3409855158 + ], + /* int64 */[ + /* hi */1501505948, + /* lo */4234509866 + ], + /* int64 */[ + /* hi */1607167915, + /* lo */987167468 + ], + /* int64 */[ + /* hi */1816402316, + /* lo */1246189591 + ] +]; + var initialHash = [ /* int64 */[ /* hi */1779033703, @@ -61,7 +384,22 @@ function make(message) { for(var i = 0 ,i_finish = chunks - 1 | 0; i <= i_finish; ++i){ var w = Caml_array.caml_make_vect(80, Int64.zero); for(var t = 0; t <= 15; ++t){ - Caml_array.caml_array_set(w, t, ReCrypt_Utils.int64_of_bytes(message$1, (i << 7) + (t << 3) | 0)); + for(var j = 0; j <= 7; ++j){ + var tmp; + var exit = 0; + var some; + try { + some = Caml_bytes.get(message$1, j + ((i << 7) + (t << 3) | 0) | 0); + exit = 1; + } + catch (exn){ + tmp = Int64.zero; + } + if (exit === 1) { + tmp = Caml_int64.of_int32(some & 255); + } + Caml_array.caml_array_set(w, t, Caml_int64.add(Caml_int64.lsl_(Caml_array.caml_array_get(w, t), 8), tmp)); + } } for(var t$1 = 16; t$1 <= 79; ++t$1){ Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions.Sha384_512.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions.Sha384_512.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); @@ -75,7 +413,7 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(ReCrypt_Constants.sha512, t$2)), Caml_array.caml_array_get(w, t$2)); + var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); var temp2 = Caml_int64.add(ReCrypt_Functions.Sha384_512.sum0(a), ReCrypt_Functions.Sha384_512.majority(a, b, c)); h = g; g = f; diff --git a/lib/js/src/ReCrypt/ReCrypt_Utils.bs.js b/lib/js/src/ReCrypt/ReCrypt_Utils.bs.js index c8b5432..4871274 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Utils.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Utils.bs.js @@ -2,40 +2,15 @@ 'use strict'; var Char = require("bs-platform/lib/js/char.js"); -var Int64 = require("bs-platform/lib/js/int64.js"); var $$String = require("bs-platform/lib/js/string.js"); -var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); var Caml_string = require("bs-platform/lib/js/caml_string.js"); -function int64_of_bytes(input, j) { - var v = Int64.zero; - for(var i = 0; i <= 7; ++i){ - var tmp; - var exit = 0; - var some; - try { - some = Caml_bytes.get(input, i + j | 0); - exit = 1; - } - catch (exn){ - tmp = Int64.zero; - } - if (exit === 1) { - tmp = Caml_int64.of_int32(some & 255); - } - v = Caml_int64.add(Caml_int64.lsl_(v, 8), tmp); - } - return v; -} - -function toBytes(value, bytes) { - var va = value; - for(var i = 1; i <= 15; ++i){ - bytes[bytes.length - i | 0] = Char.chr(va & 255); - va = (va >> 8); +function append(hash, value, offset) { + for(var j = 0; j <= 3; ++j){ + hash[j + offset | 0] = Char.chr((value >>> (j << 3)) & 255); } - return bytes; + return /* () */0; } function append32(hash, value, offset) { @@ -68,9 +43,8 @@ function stringToHex(input) { })); } -exports.int64_of_bytes = int64_of_bytes; +exports.append = append; exports.append32 = append32; exports.append64 = append64; -exports.toBytes = toBytes; exports.stringToHex = stringToHex; /* No side effect */ diff --git a/package.json b/package.json index a035cf1..963c96d 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "bucklescript", "crypto", "cryptography", + "md5", "sha", "sha1", "sha224", diff --git a/src/ReCrypt.re b/src/ReCrypt.re index 74171cf..2bb8147 100644 --- a/src/ReCrypt.re +++ b/src/ReCrypt.re @@ -1,3 +1,7 @@ +module MD5 = { + include ReCrypt_MD5; +}; + module Sha1 = { include ReCrypt_Sha1; }; diff --git a/src/ReCrypt.rei b/src/ReCrypt.rei index 00dad6c..bf8016d 100644 --- a/src/ReCrypt.rei +++ b/src/ReCrypt.rei @@ -1,5 +1,10 @@ /* BASED ON: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf */ +/** + Module for creating MD5 hashes + */ +module MD5: {let make: string => string;}; + /** Module for creating SHA1 hashes */ diff --git a/src/ReCrypt/ReCrypt_Constants.re b/src/ReCrypt/ReCrypt_Constants.re deleted file mode 100644 index acd5191..0000000 --- a/src/ReCrypt/ReCrypt_Constants.re +++ /dev/null @@ -1,313 +0,0 @@ -/** - * Constants [§4.2] - */ - -let sha1 = t => { - switch (t) { - | t when t >= 0 && t <= 19 => 0x5A827999l - | t when t >= 20 && t <= 39 => 0x6ED9EBA1l - | t when t >= 40 && t <= 59 => 0x8F1BBCDCl - | t when t >= 60 && t <= 79 => 0xCA62C1D6l - | _ => 0x00l - }; -}; - -let sha224 = [| - 0x428a2f98l, - 0x71374491l, - (-0x4a3f0431l), - (-0x164a245bl), - 0x3956c25bl, - 0x59f111f1l, - (-0x6dc07d5cl), - (-0x54e3a12bl), - (-0x27f85568l), - 0x12835b01l, - 0x243185bel, - 0x550c7dc3l, - 0x72be5d74l, - (-0x7f214e02l), - (-0x6423f959l), - (-0x3e640e8cl), - (-0x1b64963fl), - (-0x1041b87al), - 0x0fc19dc6l, - 0x240ca1ccl, - 0x2de92c6fl, - 0x4a7484aal, - 0x5cb0a9dcl, - 0x76f988dal, - (-0x67c1aeael), - (-0x57ce3993l), - (-0x4ffcd838l), - (-0x40a68039l), - (-0x391ff40dl), - (-0x2a586eb9l), - 0x06ca6351l, - 0x14292967l, - 0x27b70a85l, - 0x2e1b2138l, - 0x4d2c6dfcl, - 0x53380d13l, - 0x650a7354l, - 0x766a0abbl, - (-0x7e3d36d2l), - (-0x6d8dd37bl), - (-0x5d40175fl), - (-0x57e599b5l), - (-0x3db47490l), - (-0x3893ae5dl), - (-0x2e6d17e7l), - (-0x2966f9dcl), - (-0xbf1ca7bl), - 0x106aa070l, - 0x19a4c116l, - 0x1e376c08l, - 0x2748774cl, - 0x34b0bcb5l, - 0x391c0cb3l, - 0x4ed8aa4al, - 0x5b9cca4fl, - 0x682e6ff3l, - 0x748f82eel, - 0x78a5636fl, - (-0x7b3787ecl), - (-0x7338fdf8l), - (-0x6f410006l), - (-0x5baf9315l), - (-0x41065c09l), - (-0x398e870el), -|]; - -let sha256 = [| - 0x428a2f98l, - 0x71374491l, - (-0x4a3f0431l), - (-0x164a245bl), - 0x3956c25bl, - 0x59f111f1l, - (-0x6dc07d5cl), - (-0x54e3a12bl), - (-0x27f85568l), - 0x12835b01l, - 0x243185bel, - 0x550c7dc3l, - 0x72be5d74l, - (-0x7f214e02l), - (-0x6423f959l), - (-0x3e640e8cl), - (-0x1b64963fl), - (-0x1041b87al), - 0x0fc19dc6l, - 0x240ca1ccl, - 0x2de92c6fl, - 0x4a7484aal, - 0x5cb0a9dcl, - 0x76f988dal, - (-0x67c1aeael), - (-0x57ce3993l), - (-0x4ffcd838l), - (-0x40a68039l), - (-0x391ff40dl), - (-0x2a586eb9l), - 0x06ca6351l, - 0x14292967l, - 0x27b70a85l, - 0x2e1b2138l, - 0x4d2c6dfcl, - 0x53380d13l, - 0x650a7354l, - 0x766a0abbl, - (-0x7e3d36d2l), - (-0x6d8dd37bl), - (-0x5d40175fl), - (-0x57e599b5l), - (-0x3db47490l), - (-0x3893ae5dl), - (-0x2e6d17e7l), - (-0x2966f9dcl), - (-0xbf1ca7bl), - 0x106aa070l, - 0x19a4c116l, - 0x1e376c08l, - 0x2748774cl, - 0x34b0bcb5l, - 0x391c0cb3l, - 0x4ed8aa4al, - 0x5b9cca4fl, - 0x682e6ff3l, - 0x748f82eel, - 0x78a5636fl, - (-0x7b3787ecl), - (-0x7338fdf8l), - (-0x6f410006l), - (-0x5baf9315l), - (-0x41065c09l), - (-0x398e870el), -|]; - -let sha384 = [| - 0x428A2F98D728AE22L, - 0x7137449123EF65CDL, - (-0x4a3f043013b2c4d1L), - (-0x164a245a7e762444L), - 0x3956C25BF348B538L, - 0x59F111F1B605D019L, - (-0x6dc07d5b50e6b065L), - (-0x54e3a12a25927ee8L), - (-0x27f855675cfcfdbeL), - 0x12835B0145706FBEL, - 0x243185BE4EE4B28CL, - 0x550C7DC3D5FFB4E2L, - 0x72BE5D74F27B896FL, - (-0x7f214e01c4e9694fL), - (-0x6423f958da38edcbL), - (-0x3e640e8b3096d96cL), - (-0x1b64963e610eb52eL), - (-0x1041b879c7b0da1dL), - 0x0FC19DC68B8CD5B5L, - 0x240CA1CC77AC9C65L, - 0x2DE92C6F592B0275L, - 0x4A7484AA6EA6E483L, - 0x5CB0A9DCBD41FBD4L, - 0x76F988DA831153B5L, - (-0x67c1aead11992055L), - (-0x57ce3992d24bcdf0L), - (-0x4ffcd8376704dec1L), - (-0x40a680384110f11cL), - (-0x391ff40cc257703eL), - (-0x2a586eb86cf558dbL), - 0x06CA6351E003826FL, - 0x142929670A0E6E70L, - 0x27B70A8546D22FFCL, - 0x2E1B21385C26C926L, - 0x4D2C6DFC5AC42AEDL, - 0x53380D139D95B3DFL, - 0x650A73548BAF63DEL, - 0x766A0ABB3C77B2A8L, - (-0x7e3d36d1b812511aL), - (-0x6d8dd37aeb7dcac5L), - (-0x5d40175eb30efc9cL), - (-0x57e599b443bdcfffL), - (-0x3db4748f2f07686fL), - (-0x3893ae5cf9ab41d0L), - (-0x2e6d17e62910ade8L), - (-0x2966f9dbaa9a56f0L), - (-0xbf1ca7aa88edfd6L), - 0x106AA07032BBD1B8L, - 0x19A4C116B8D2D0C8L, - 0x1E376C085141AB53L, - 0x2748774CDF8EEB99L, - 0x34B0BCB5E19B48A8L, - 0x391C0CB3C5C95A63L, - 0x4ED8AA4AE3418ACBL, - 0x5B9CCA4F7763E373L, - 0x682E6FF3D6B2B8A3L, - 0x748F82EE5DEFB2FCL, - 0x78A5636F43172F60L, - (-0x7b3787eb5e0f548eL), - (-0x7338fdf7e59bc614L), - (-0x6f410005dc9ce1d8L), - (-0x5baf9314217d4217L), - (-0x41065c084d3986ebL), - (-0x398e870d1c8dacd5L), - (-0x35d8c13115d99e64L), - (-0x2e794738de3f3df9L), - (-0x15258229321f14e2L), - (-0xa82b08011912e88L), - 0x06F067AA72176FBAL, - 0x0A637DC5A2C898A6L, - 0x113F9804BEF90DAEL, - 0x1B710B35131C471BL, - 0x28DB77F523047D84L, - 0x32CAAB7B40C72493L, - 0x3C9EBE0A15C9BEBCL, - 0x431D67C49C100D4CL, - 0x4CC5D4BECB3E42B6L, - 0x597F299CFC657E2AL, - 0x5FCB6FAB3AD6FAECL, - 0x6C44198C4A475817L, -|]; - -let sha512 = [| - 0x428a2f98d728ae22L, - 0x7137449123ef65cdL, - 0xb5c0fbcfec4d3b2fL, - 0xe9b5dba58189dbbcL, - 0x3956c25bf348b538L, - 0x59f111f1b605d019L, - 0x923f82a4af194f9bL, - 0xab1c5ed5da6d8118L, - 0xd807aa98a3030242L, - 0x12835b0145706fbeL, - 0x243185be4ee4b28cL, - 0x550c7dc3d5ffb4e2L, - 0x72be5d74f27b896fL, - 0x80deb1fe3b1696b1L, - 0x9bdc06a725c71235L, - 0xc19bf174cf692694L, - 0xe49b69c19ef14ad2L, - 0xefbe4786384f25e3L, - 0x0fc19dc68b8cd5b5L, - 0x240ca1cc77ac9c65L, - 0x2de92c6f592b0275L, - 0x4a7484aa6ea6e483L, - 0x5cb0a9dcbd41fbd4L, - 0x76f988da831153b5L, - 0x983e5152ee66dfabL, - 0xa831c66d2db43210L, - 0xb00327c898fb213fL, - 0xbf597fc7beef0ee4L, - 0xc6e00bf33da88fc2L, - 0xd5a79147930aa725L, - 0x06ca6351e003826fL, - 0x142929670a0e6e70L, - 0x27b70a8546d22ffcL, - 0x2e1b21385c26c926L, - 0x4d2c6dfc5ac42aedL, - 0x53380d139d95b3dfL, - 0x650a73548baf63deL, - 0x766a0abb3c77b2a8L, - 0x81c2c92e47edaee6L, - 0x92722c851482353bL, - 0xa2bfe8a14cf10364L, - 0xa81a664bbc423001L, - 0xc24b8b70d0f89791L, - 0xc76c51a30654be30L, - 0xd192e819d6ef5218L, - 0xd69906245565a910L, - 0xf40e35855771202aL, - 0x106aa07032bbd1b8L, - 0x19a4c116b8d2d0c8L, - 0x1e376c085141ab53L, - 0x2748774cdf8eeb99L, - 0x34b0bcb5e19b48a8L, - 0x391c0cb3c5c95a63L, - 0x4ed8aa4ae3418acbL, - 0x5b9cca4f7763e373L, - 0x682e6ff3d6b2b8a3L, - 0x748f82ee5defb2fcL, - 0x78a5636f43172f60L, - 0x84c87814a1f0ab72L, - 0x8cc702081a6439ecL, - 0x90befffa23631e28L, - 0xa4506cebde82bde9L, - 0xbef9a3f7b2c67915L, - 0xc67178f2e372532bL, - 0xca273eceea26619cL, - 0xd186b8c721c0c207L, - 0xeada7dd6cde0eb1eL, - 0xf57d4f7fee6ed178L, - 0x06f067aa72176fbaL, - 0x0a637dc5a2c898a6L, - 0x113f9804bef90daeL, - 0x1b710b35131c471bL, - 0x28db77f523047d84L, - 0x32caab7b40c72493L, - 0x3c9ebe0a15c9bebcL, - 0x431d67c49c100d4cL, - 0x4cc5d4becb3e42b6L, - 0x597f299cfc657e2aL, - 0x5fcb6fab3ad6faecL, - 0x6c44198c4a475817L, -|]; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Constants.rei b/src/ReCrypt/ReCrypt_Constants.rei deleted file mode 100644 index d82fa14..0000000 --- a/src/ReCrypt/ReCrypt_Constants.rei +++ /dev/null @@ -1,5 +0,0 @@ -let sha1: int => int32; -let sha224: array(int32); -let sha256: array(int32); -let sha384: array(int64); -let sha512: array(int64); \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Functions.re b/src/ReCrypt/ReCrypt_Functions.re index d4943b5..4f17c7a 100644 --- a/src/ReCrypt/ReCrypt_Functions.re +++ b/src/ReCrypt/ReCrypt_Functions.re @@ -1,6 +1,12 @@ /** * Logical functions [§4.1] */ +module MD5 = { + let rotl = (x, n) => { + x lsl n lor x lsr (64 - n); + }; +}; + module Sha1 = { let rotl = (x, n) => { Int32.logor( diff --git a/src/ReCrypt/ReCrypt_Functions.rei b/src/ReCrypt/ReCrypt_Functions.rei index 00f213a..e417c14 100644 --- a/src/ReCrypt/ReCrypt_Functions.rei +++ b/src/ReCrypt/ReCrypt_Functions.rei @@ -1,3 +1,5 @@ +module MD5: {let rotl: (int, int) => int;}; + module Sha1: { let f: (int, int32, int32, int32) => int32; let rotl: (int32, int) => int32; diff --git a/src/ReCrypt/ReCrypt_MD5.re b/src/ReCrypt/ReCrypt_MD5.re new file mode 100644 index 0000000..3bde5fa --- /dev/null +++ b/src/ReCrypt/ReCrypt_MD5.re @@ -0,0 +1,175 @@ +let constants = [| + 0xd76aa478, + 0xe8c7b756, + 0x242070db, + 0xc1bdceee, + 0xf57c0faf, + 0x4787c62a, + 0xa8304613, + 0xfd469501, + 0x698098d8, + 0x8b44f7af, + 0xffff5bb1, + 0x895cd7be, + 0x6b901122, + 0xfd987193, + 0xa679438e, + 0x49b40821, + 0xf61e2562, + 0xc040b340, + 0x265e5a51, + 0xe9b6c7aa, + 0xd62f105d, + 0x02441453, + 0xd8a1e681, + 0xe7d3fbc8, + 0x21e1cde6, + 0xc33707d6, + 0xf4d50d87, + 0x455a14ed, + 0xa9e3e905, + 0xfcefa3f8, + 0x676f02d9, + 0x8d2a4c8a, + 0xfffa3942, + 0x8771f681, + 0x6d9d6122, + 0xfde5380c, + 0xa4beea44, + 0x4bdecfa9, + 0xf6bb4b60, + 0xbebfbc70, + 0x289b7ec6, + 0xeaa127fa, + 0xd4ef3085, + 0x04881d05, + 0xd9d4d039, + 0xe6db99e5, + 0x1fa27cf8, + 0xc4ac5665, + 0xf4292244, + 0x432aff97, + 0xab9423a7, + 0xfc93a039, + 0x655b59c3, + 0x8f0ccc92, + 0xffeff47d, + 0x85845dd1, + 0x6fa87e4f, + 0xfe2ce6e0, + 0xa3014314, + 0x4e0811a1, + 0xf7537e82, + 0xbd3af235, + 0x2ad7d2bb, + 0xeb86d391, +|]; + +let s = [|7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21|]; + +let initialHash = [|0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476|]; + +let preprocess = (msgBytes: Bytes.t) => { + let size = ref(Bytes.length(msgBytes) + 8 + 1); + while (size^ mod 64 != 0) { + size := size^ + 1; + }; + + let length = Bytes.length(msgBytes); + + let bytes = + Bytes.init(size^, index => { + switch (index) { + | i when i < length => Bytes.get(msgBytes, i) + | i when i == length => Char.chr(0x80) + | _ => Char.chr(0x00) + } + }); + + let va = ref(length * 8); + for (i in 0 to 7) { + Bytes.set(bytes, i + (size^ - 8), Char.chr(va^ land 0xFF)); + va := va^ lsr 8; + }; + + bytes; +}; + +let processMessage = (message: Bytes.t) => { + let chunks = (Bytes.length(message) + 8) asr 6; + + let a = ref(initialHash[0]); + let b = ref(initialHash[1]); + let c = ref(initialHash[2]); + let d = ref(initialHash[3]); + + let w = Array.make(16, 0); + + for (i in 0 to chunks - 1) { + for (j in 0 to 63) { + w[j lsr 2] = + Bytes.get(message, i lsl 6 + j)->Char.code lsl 24 lor w[j lsr 2] lsr 8; + }; + + let originalA = a^; + let originalB = b^; + let originalC = c^; + let originalD = d^; + + for (j in 0 to 63) { + let f = + if (0 <= j && j <= 15) { + b^ land c^ lor (lnot(b^) land d^); + } else if (16 <= j && j <= 31) { + b^ land d^ lor (c^ land lnot(d^)); + } else if (32 <= j && j <= 47) { + b^ lxor c^ lxor d^; + } else { + c^ lxor (b^ lor lnot(d^)); + }; + + let g = + if (0 <= j && j <= 15) { + j; + } else if (16 <= j && j <= 31) { + (5 * j + 1) mod 16; + } else if (32 <= j && j <= 47) { + (3 * j + 5) mod 16; + } else { + 7 * j mod 16; + }; + + let temp = + b^ + + ReCrypt_Functions.MD5.rotl( + a^ + f + w[g] + constants[j], + s[(j lsr 4) lsl 2 lor (j land 3)], + ); + + a := d^; + d := c^; + c := b^; + b := temp; + }; + + a := originalA + a^; + b := originalB + b^; + c := originalC + c^; + d := originalD + d^; + }; + + let md5 = Bytes.make(16, Char.chr(0)); + + ReCrypt_Utils.append(md5, a^, 0); + ReCrypt_Utils.append(md5, b^, 4); + ReCrypt_Utils.append(md5, c^, 8); + ReCrypt_Utils.append(md5, d^, 12); + + Bytes.to_string(md5)->ReCrypt_Utils.stringToHex; +}; + +let make = message => { + let messageBytes = Bytes.of_string(message); + let paddedMessage = preprocess(messageBytes); + processMessage(paddedMessage); +}; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_MD5.rei b/src/ReCrypt/ReCrypt_MD5.rei new file mode 100644 index 0000000..b753ff1 --- /dev/null +++ b/src/ReCrypt/ReCrypt_MD5.rei @@ -0,0 +1,4 @@ +/** + Compute a MD5 hash based on the given string + */ +let make: string => string; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Preprocess.re b/src/ReCrypt/ReCrypt_Preprocess.re index 0cad9ee..35843f1 100644 --- a/src/ReCrypt/ReCrypt_Preprocess.re +++ b/src/ReCrypt/ReCrypt_Preprocess.re @@ -19,7 +19,16 @@ module Sha1_224_256 = { } }); - ReCrypt_Utils.toBytes(length * 8, bytes); + let va = ref(length * 8); + for (i in 1 to 15) { + Bytes.set( + bytes, + Bytes.length(bytes) - i, + Char.chr(va^ land 0x000000FF), + ); + va := va^ asr 8; + }; + bytes; }; }; @@ -41,6 +50,15 @@ module Sha384_512 = { } }); - ReCrypt_Utils.toBytes(length * 8, bytes); + let va = ref(length * 8); + for (i in 1 to 15) { + Bytes.set( + bytes, + Bytes.length(bytes) - i, + Char.chr(va^ land 0x000000FF), + ); + va := va^ asr 8; + }; + bytes; }; }; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Sha1.re b/src/ReCrypt/ReCrypt_Sha1.re index 2c0902c..65c3377 100644 --- a/src/ReCrypt/ReCrypt_Sha1.re +++ b/src/ReCrypt/ReCrypt_Sha1.re @@ -1,4 +1,14 @@ /* constants [§4.2.3] */ +let constants = t => { + switch (t) { + | t when t >= 0 && t <= 19 => 0x5A827999l + | t when t >= 20 && t <= 39 => 0x6ED9EBA1l + | t when t >= 40 && t <= 59 => 0x8F1BBCDCl + | t when t >= 60 && t <= 79 => 0xCA62C1D6l + | _ => 0x00l + }; +}; + let initialHash = [| 0x67452301l, 0xEFCDAB89l, @@ -75,7 +85,7 @@ let processMessage = (message: Bytes.t) => { ReCrypt_Functions.Sha1.rotl(a^, 5) ->Int32.add(ReCrypt_Functions.Sha1.f(t, b^, c^, d^)) ->Int32.add(e^) - ->Int32.add(ReCrypt_Constants.sha1(t)) + ->Int32.add(constants(t)) ->Int32.add(w[t]); e := d^; diff --git a/src/ReCrypt/ReCrypt_Sha224.re b/src/ReCrypt/ReCrypt_Sha224.re index 989537d..8bc8221 100644 --- a/src/ReCrypt/ReCrypt_Sha224.re +++ b/src/ReCrypt/ReCrypt_Sha224.re @@ -1,4 +1,71 @@ /* constants [§4.2.3] */ +let constants = [| + 0x428a2f98l, + 0x71374491l, + (-0x4a3f0431l), + (-0x164a245bl), + 0x3956c25bl, + 0x59f111f1l, + (-0x6dc07d5cl), + (-0x54e3a12bl), + (-0x27f85568l), + 0x12835b01l, + 0x243185bel, + 0x550c7dc3l, + 0x72be5d74l, + (-0x7f214e02l), + (-0x6423f959l), + (-0x3e640e8cl), + (-0x1b64963fl), + (-0x1041b87al), + 0x0fc19dc6l, + 0x240ca1ccl, + 0x2de92c6fl, + 0x4a7484aal, + 0x5cb0a9dcl, + 0x76f988dal, + (-0x67c1aeael), + (-0x57ce3993l), + (-0x4ffcd838l), + (-0x40a68039l), + (-0x391ff40dl), + (-0x2a586eb9l), + 0x06ca6351l, + 0x14292967l, + 0x27b70a85l, + 0x2e1b2138l, + 0x4d2c6dfcl, + 0x53380d13l, + 0x650a7354l, + 0x766a0abbl, + (-0x7e3d36d2l), + (-0x6d8dd37bl), + (-0x5d40175fl), + (-0x57e599b5l), + (-0x3db47490l), + (-0x3893ae5dl), + (-0x2e6d17e7l), + (-0x2966f9dcl), + (-0xbf1ca7bl), + 0x106aa070l, + 0x19a4c116l, + 0x1e376c08l, + 0x2748774cl, + 0x34b0bcb5l, + 0x391c0cb3l, + 0x4ed8aa4al, + 0x5b9cca4fl, + 0x682e6ff3l, + 0x748f82eel, + 0x78a5636fl, + (-0x7b3787ecl), + (-0x7338fdf8l), + (-0x6f410006l), + (-0x5baf9315l), + (-0x41065c09l), + (-0x398e870el), +|]; + let initialHash = [| (-0x3efa6128l), 0x367cd507l, @@ -81,7 +148,7 @@ let processMessage = (message: Bytes.t) => { (h^) ->Int32.add(ReCrypt_Functions.Sha224_256.sum1(e^)) ->Int32.add(ReCrypt_Functions.Sha224_256.change(e^, f^, g^)) - ->Int32.add(ReCrypt_Constants.sha224[t]) + ->Int32.add(constants[t]) ->Int32.add(w[t]); let temp2 = diff --git a/src/ReCrypt/ReCrypt_Sha256.re b/src/ReCrypt/ReCrypt_Sha256.re index 6bb9161..a4b532b 100644 --- a/src/ReCrypt/ReCrypt_Sha256.re +++ b/src/ReCrypt/ReCrypt_Sha256.re @@ -1,4 +1,70 @@ /* constants [§4.2.3] */ +let constants = [| + 0x428a2f98l, + 0x71374491l, + (-0x4a3f0431l), + (-0x164a245bl), + 0x3956c25bl, + 0x59f111f1l, + (-0x6dc07d5cl), + (-0x54e3a12bl), + (-0x27f85568l), + 0x12835b01l, + 0x243185bel, + 0x550c7dc3l, + 0x72be5d74l, + (-0x7f214e02l), + (-0x6423f959l), + (-0x3e640e8cl), + (-0x1b64963fl), + (-0x1041b87al), + 0x0fc19dc6l, + 0x240ca1ccl, + 0x2de92c6fl, + 0x4a7484aal, + 0x5cb0a9dcl, + 0x76f988dal, + (-0x67c1aeael), + (-0x57ce3993l), + (-0x4ffcd838l), + (-0x40a68039l), + (-0x391ff40dl), + (-0x2a586eb9l), + 0x06ca6351l, + 0x14292967l, + 0x27b70a85l, + 0x2e1b2138l, + 0x4d2c6dfcl, + 0x53380d13l, + 0x650a7354l, + 0x766a0abbl, + (-0x7e3d36d2l), + (-0x6d8dd37bl), + (-0x5d40175fl), + (-0x57e599b5l), + (-0x3db47490l), + (-0x3893ae5dl), + (-0x2e6d17e7l), + (-0x2966f9dcl), + (-0xbf1ca7bl), + 0x106aa070l, + 0x19a4c116l, + 0x1e376c08l, + 0x2748774cl, + 0x34b0bcb5l, + 0x391c0cb3l, + 0x4ed8aa4al, + 0x5b9cca4fl, + 0x682e6ff3l, + 0x748f82eel, + 0x78a5636fl, + (-0x7b3787ecl), + (-0x7338fdf8l), + (-0x6f410006l), + (-0x5baf9315l), + (-0x41065c09l), + (-0x398e870el), +|]; let initialHash = [| 0x6a09e667l, (-0x4498517bl), @@ -81,7 +147,7 @@ let processMessage = (message: Bytes.t) => { (h^) ->Int32.add(ReCrypt_Functions.Sha224_256.sum1(e^)) ->Int32.add(ReCrypt_Functions.Sha224_256.change(e^, f^, g^)) - ->Int32.add(ReCrypt_Constants.sha256[t]) + ->Int32.add(constants[t]) ->Int32.add(w[t]); let temp2 = diff --git a/src/ReCrypt/ReCrypt_Sha384.re b/src/ReCrypt/ReCrypt_Sha384.re index 56ee2a4..e0dd31a 100644 --- a/src/ReCrypt/ReCrypt_Sha384.re +++ b/src/ReCrypt/ReCrypt_Sha384.re @@ -1,4 +1,87 @@ /* constants [§4.2.3] */ +let constants = [| + 0x428A2F98D728AE22L, + 0x7137449123EF65CDL, + (-0x4a3f043013b2c4d1L), + (-0x164a245a7e762444L), + 0x3956C25BF348B538L, + 0x59F111F1B605D019L, + (-0x6dc07d5b50e6b065L), + (-0x54e3a12a25927ee8L), + (-0x27f855675cfcfdbeL), + 0x12835B0145706FBEL, + 0x243185BE4EE4B28CL, + 0x550C7DC3D5FFB4E2L, + 0x72BE5D74F27B896FL, + (-0x7f214e01c4e9694fL), + (-0x6423f958da38edcbL), + (-0x3e640e8b3096d96cL), + (-0x1b64963e610eb52eL), + (-0x1041b879c7b0da1dL), + 0x0FC19DC68B8CD5B5L, + 0x240CA1CC77AC9C65L, + 0x2DE92C6F592B0275L, + 0x4A7484AA6EA6E483L, + 0x5CB0A9DCBD41FBD4L, + 0x76F988DA831153B5L, + (-0x67c1aead11992055L), + (-0x57ce3992d24bcdf0L), + (-0x4ffcd8376704dec1L), + (-0x40a680384110f11cL), + (-0x391ff40cc257703eL), + (-0x2a586eb86cf558dbL), + 0x06CA6351E003826FL, + 0x142929670A0E6E70L, + 0x27B70A8546D22FFCL, + 0x2E1B21385C26C926L, + 0x4D2C6DFC5AC42AEDL, + 0x53380D139D95B3DFL, + 0x650A73548BAF63DEL, + 0x766A0ABB3C77B2A8L, + (-0x7e3d36d1b812511aL), + (-0x6d8dd37aeb7dcac5L), + (-0x5d40175eb30efc9cL), + (-0x57e599b443bdcfffL), + (-0x3db4748f2f07686fL), + (-0x3893ae5cf9ab41d0L), + (-0x2e6d17e62910ade8L), + (-0x2966f9dbaa9a56f0L), + (-0xbf1ca7aa88edfd6L), + 0x106AA07032BBD1B8L, + 0x19A4C116B8D2D0C8L, + 0x1E376C085141AB53L, + 0x2748774CDF8EEB99L, + 0x34B0BCB5E19B48A8L, + 0x391C0CB3C5C95A63L, + 0x4ED8AA4AE3418ACBL, + 0x5B9CCA4F7763E373L, + 0x682E6FF3D6B2B8A3L, + 0x748F82EE5DEFB2FCL, + 0x78A5636F43172F60L, + (-0x7b3787eb5e0f548eL), + (-0x7338fdf7e59bc614L), + (-0x6f410005dc9ce1d8L), + (-0x5baf9314217d4217L), + (-0x41065c084d3986ebL), + (-0x398e870d1c8dacd5L), + (-0x35d8c13115d99e64L), + (-0x2e794738de3f3df9L), + (-0x15258229321f14e2L), + (-0xa82b08011912e88L), + 0x06F067AA72176FBAL, + 0x0A637DC5A2C898A6L, + 0x113F9804BEF90DAEL, + 0x1B710B35131C471BL, + 0x28DB77F523047D84L, + 0x32CAAB7B40C72493L, + 0x3C9EBE0A15C9BEBCL, + 0x431D67C49C100D4CL, + 0x4CC5D4BECB3E42B6L, + 0x597F299CFC657E2AL, + 0x5FCB6FAB3AD6FAECL, + 0x6C44198C4A475817L, +|]; + let initialHash = [| (-0x344462a23efa6128L), 0x629a292a367cd507L, @@ -26,7 +109,16 @@ let processMessage = (message: Bytes.t) => { let w = Array.make(80, Int64.zero); for (t in 0 to 15) { - w[t] = ReCrypt_Utils.int64_of_bytes(message, i * 128 + 8 * t); + for (j in 0 to 7) { + w[t] = + Int64.shift_left(w[t], 8) + ->Int64.add( + switch (Bytes.get(message, j + (i * 128 + 8 * t))) { + | exception _ => Int64.zero + | some => Int64.of_int(Char.code(some) land 0xff) + }, + ); + }; }; for (t in 16 to 79) { @@ -51,7 +143,7 @@ let processMessage = (message: Bytes.t) => { (h^) ->Int64.add(ReCrypt_Functions.Sha384_512.sum1(e^)) ->Int64.add(ReCrypt_Functions.Sha384_512.change(e^, f^, g^)) - ->Int64.add(ReCrypt_Constants.sha384[t]) + ->Int64.add(constants[t]) ->Int64.add(w[t]); let temp2 = diff --git a/src/ReCrypt/ReCrypt_Sha512.re b/src/ReCrypt/ReCrypt_Sha512.re index b9597d8..4e35fb6 100644 --- a/src/ReCrypt/ReCrypt_Sha512.re +++ b/src/ReCrypt/ReCrypt_Sha512.re @@ -1,4 +1,87 @@ /* constants [§4.2.3] */ +let constants = [| + 0x428a2f98d728ae22L, + 0x7137449123ef65cdL, + 0xb5c0fbcfec4d3b2fL, + 0xe9b5dba58189dbbcL, + 0x3956c25bf348b538L, + 0x59f111f1b605d019L, + 0x923f82a4af194f9bL, + 0xab1c5ed5da6d8118L, + 0xd807aa98a3030242L, + 0x12835b0145706fbeL, + 0x243185be4ee4b28cL, + 0x550c7dc3d5ffb4e2L, + 0x72be5d74f27b896fL, + 0x80deb1fe3b1696b1L, + 0x9bdc06a725c71235L, + 0xc19bf174cf692694L, + 0xe49b69c19ef14ad2L, + 0xefbe4786384f25e3L, + 0x0fc19dc68b8cd5b5L, + 0x240ca1cc77ac9c65L, + 0x2de92c6f592b0275L, + 0x4a7484aa6ea6e483L, + 0x5cb0a9dcbd41fbd4L, + 0x76f988da831153b5L, + 0x983e5152ee66dfabL, + 0xa831c66d2db43210L, + 0xb00327c898fb213fL, + 0xbf597fc7beef0ee4L, + 0xc6e00bf33da88fc2L, + 0xd5a79147930aa725L, + 0x06ca6351e003826fL, + 0x142929670a0e6e70L, + 0x27b70a8546d22ffcL, + 0x2e1b21385c26c926L, + 0x4d2c6dfc5ac42aedL, + 0x53380d139d95b3dfL, + 0x650a73548baf63deL, + 0x766a0abb3c77b2a8L, + 0x81c2c92e47edaee6L, + 0x92722c851482353bL, + 0xa2bfe8a14cf10364L, + 0xa81a664bbc423001L, + 0xc24b8b70d0f89791L, + 0xc76c51a30654be30L, + 0xd192e819d6ef5218L, + 0xd69906245565a910L, + 0xf40e35855771202aL, + 0x106aa07032bbd1b8L, + 0x19a4c116b8d2d0c8L, + 0x1e376c085141ab53L, + 0x2748774cdf8eeb99L, + 0x34b0bcb5e19b48a8L, + 0x391c0cb3c5c95a63L, + 0x4ed8aa4ae3418acbL, + 0x5b9cca4f7763e373L, + 0x682e6ff3d6b2b8a3L, + 0x748f82ee5defb2fcL, + 0x78a5636f43172f60L, + 0x84c87814a1f0ab72L, + 0x8cc702081a6439ecL, + 0x90befffa23631e28L, + 0xa4506cebde82bde9L, + 0xbef9a3f7b2c67915L, + 0xc67178f2e372532bL, + 0xca273eceea26619cL, + 0xd186b8c721c0c207L, + 0xeada7dd6cde0eb1eL, + 0xf57d4f7fee6ed178L, + 0x06f067aa72176fbaL, + 0x0a637dc5a2c898a6L, + 0x113f9804bef90daeL, + 0x1b710b35131c471bL, + 0x28db77f523047d84L, + 0x32caab7b40c72493L, + 0x3c9ebe0a15c9bebcL, + 0x431d67c49c100d4cL, + 0x4cc5d4becb3e42b6L, + 0x597f299cfc657e2aL, + 0x5fcb6fab3ad6faecL, + 0x6c44198c4a475817L, +|]; + let initialHash = [| 0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL, @@ -26,7 +109,16 @@ let processMessage = (message: Bytes.t) => { let w = Array.make(80, Int64.zero); for (t in 0 to 15) { - w[t] = ReCrypt_Utils.int64_of_bytes(message, i * 128 + 8 * t); + for (j in 0 to 7) { + w[t] = + Int64.shift_left(w[t], 8) + ->Int64.add( + switch (Bytes.get(message, j + (i * 128 + 8 * t))) { + | exception _ => Int64.zero + | some => Int64.of_int(Char.code(some) land 0xff) + }, + ); + }; }; for (t in 16 to 79) { @@ -51,7 +143,7 @@ let processMessage = (message: Bytes.t) => { (h^) ->Int64.add(ReCrypt_Functions.Sha384_512.sum1(e^)) ->Int64.add(ReCrypt_Functions.Sha384_512.change(e^, f^, g^)) - ->Int64.add(ReCrypt_Constants.sha512[t]) + ->Int64.add(constants[t]) ->Int64.add(w[t]); let temp2 = diff --git a/src/ReCrypt/ReCrypt_Utils.re b/src/ReCrypt/ReCrypt_Utils.re index 1cb456d..4b02d3e 100644 --- a/src/ReCrypt/ReCrypt_Utils.re +++ b/src/ReCrypt/ReCrypt_Utils.re @@ -1,29 +1,7 @@ -let int64_of_bytes = (input, j) => { - let v = ref(Int64.zero); - for (i in 0 to 7) { - v := - Int64.shift_left(v^, 8) - ->Int64.add( - switch (Bytes.get(input, i + j)) { - | exception _ => Int64.zero - | some => Int64.of_int(Char.code(some) land 0xff) - }, - ); - }; - v^; -}; - -let toBytes = (value, bytes) => { - let va = ref(value); - for (i in 1 to 15) { - Bytes.set( - bytes, - Bytes.length(bytes) - i, - Char.chr(va^ land 0x000000FF), - ); - va := va^ asr 8; +let append = (hash, value, offset) => { + for (j in 0 to 3) { + Bytes.set(hash, j + offset, (value lsr (j * 8) land 0xFF)->Char.chr); }; - bytes; }; let append32 = (hash, value, offset) => { diff --git a/src/ReCrypt/ReCrypt_Utils.rei b/src/ReCrypt/ReCrypt_Utils.rei index 5a73e49..2ee9398 100644 --- a/src/ReCrypt/ReCrypt_Utils.rei +++ b/src/ReCrypt/ReCrypt_Utils.rei @@ -1,7 +1,6 @@ /** - Converts the byte array input starting at index j into an int64 */ -let int64_of_bytes: (bytes, int) => int64; +let append: (bytes, int, int) => unit; /** */ @@ -11,10 +10,6 @@ let append32: (bytes, int32, int) => unit; */ let append64: (bytes, int64, int) => unit; -/** - */ -let toBytes: (int, bytes) => bytes; - /** Convert the given string into a hex representation */ -- 2.51.2