Mercurial > projects > dcrypt
diff dcrypt/crypto/hashes/SHA384.d @ 0:0e08791a1418
Initial import.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sun, 10 Aug 2008 14:20:17 -0400 |
parents | |
children | 71aae178f89a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dcrypt/crypto/hashes/SHA384.d Sun Aug 10 14:20:17 2008 -0400 @@ -0,0 +1,93 @@ +/** + * This file is part of the dcrypt project. + * + * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. + * License: MIT + * Authors: Thomas Dixon + */ + +module dcrypt.crypto.hashes.SHA384; + +import dcrypt.crypto.hashes.SHA512; + +/** Implementation of SHA-384. */ +class SHA384 : SHA512 { + this (void[] input_=null) { + reset(); + super(input_); + } + + uint digestSize() { + return 48; + } + + char[] name() { + return "SHA384"; + } + + ubyte[] digest() { + padMessage(MODE_SHA); + ubyte[] result = new ubyte[digestSize]; + + Util.ulongToUbytesBig(h0, result, 0); + Util.ulongToUbytesBig(h1, result, 8); + Util.ulongToUbytesBig(h2, result, 16); + Util.ulongToUbytesBig(h3, result, 24); + Util.ulongToUbytesBig(h4, result, 32); + Util.ulongToUbytesBig(h5, result, 40); + + reset(); + return result; + } + + void reset() { + super.reset(); + h0 = 0xcbbb9d5dc1059ed8u, + h1 = 0x629a292a367cd507u, + h2 = 0x9159015a3070dd17u, + h3 = 0x152fecd8f70e5939u, + h4 = 0x67332667ffc00b31u, + h5 = 0x8eb44a8768581511u, + h6 = 0xdb0c2e0d64f98fa7u, + h7 = 0x47b5481dbefa4fa4u; + } + + version (UnitTest) { + unittest { + static const char[][] test_inputs = [ + "", + "abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "a" + ]; + + static const int[] test_repeat = [ + 1, 1, 1, 1000000 + ]; + + static const char[][] test_results = [ + "38b060a751ac96384cd9327eb1b1e36a21fdb71114be0743"~ + "4c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", + + "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"~ + "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", + + "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"~ + "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", + + "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"~ + "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985" + ]; + + SHA384 h = new SHA384(); + foreach (uint i, char[] input; test_inputs) { + for (int j = 0; j < test_repeat[i]; j++) + h.update(input); + char[] digest = h.hexDigest(); + assert(digest == test_results[i], + h.name()~": ("~digest~") != ("~test_results[i]~")"); + } + } + } +}