This library implements some hash and cryptographic algorithms.

Dependents:   mBuinoBlinky PB_Emma_Ethernet SLOTrashHTTP Garagem ... more

This library implements the following algorithms :

  • RC4
  • AES (AES-128, AES-192, AES-256)
  • DES
  • Triple DES (EDE)
  • MD2
  • MD4
  • MD5
  • SHA-1
  • SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512)

The hash algorithms have been optimized for the mbed and you should get decent performance. However, I did not optimize the ciphers. Also, I did not test extensively these algorithms : it should work but you may find some bugs. Block ciphers support two modes : ECB and CBC.

Warning

If you are using SHA-384 or SHA-512, be aware that it produces large binary files and the compilation (using the online compiler) takes much longer to execute. It may happen that the compiler stops because it timed-out. In this case, just compile again and it should work.

Computing hash

You can compute the hash of some data in two different ways. The first one is the easiest, each hash algorithm has a static method that takes some data and compute the hash from it.

Computing hash using method 1

#include "Crypto.h"
#include "mbed.h"

static const char msg[] = "mbed is great !";

int main()
{
    uint8_t hash[16];
    MD2::computeHash(hash, (uint8_t*)msg, strlen(msg));
    printf("hash: ");
    for(int i = 0; i < 16; ++i)
        printf("%02x", hash[i]);
    printf("\n");
    
    return 0;
}

The second one is slightly slower (around 2-3% slower) but it allows you to compute the hash of some data in several steps (by calling update method). This is the method you should use if you need to compute the hash from a large source and you don't have enough memory to store it in a single buffer.

Computing hash using method 2

#include "Crypto.h"
#include "mbed.h"

static const char msg[] = "mbed is great !";

int main()
{
    uint8_t hash[16];
    MD2 h;
    h.update((uint8_t*)msg, strlen(msg));
    h.finalize(hash);
    printf("hash: ");
    for(int i = 0; i < 16; ++i)
        printf("%02x", hash[i]);
    printf("\n");
    
    return 0;
}

TODO

  • optimize ciphers
  • add doc

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Thu Sep 12 16:03:43 2013 +0000
Parent:
5:06cd9c8afa0b
Child:
7:2dbbdfb08123
Commit message:
change public API for hash + small improvements for hash + rearrange code

Changed in this revision

HashAlgorithm.h Show annotated file Show diff for this revision Revisions of this file
MD2.cpp Show annotated file Show diff for this revision Revisions of this file
MD2.h Show annotated file Show diff for this revision Revisions of this file
MD5.cpp Show annotated file Show diff for this revision Revisions of this file
MD5.h Show annotated file Show diff for this revision Revisions of this file
SHA1.cpp Show annotated file Show diff for this revision Revisions of this file
SHA1.h Show annotated file Show diff for this revision Revisions of this file
SHA224.cpp Show annotated file Show diff for this revision Revisions of this file
SHA224.h Show annotated file Show diff for this revision Revisions of this file
SHA256.cpp Show annotated file Show diff for this revision Revisions of this file
SHA256.h Show annotated file Show diff for this revision Revisions of this file
SHA2_32.cpp Show annotated file Show diff for this revision Revisions of this file
SHA2_32.h Show annotated file Show diff for this revision Revisions of this file
SHA2_64.cpp Show annotated file Show diff for this revision Revisions of this file
SHA2_64.h Show annotated file Show diff for this revision Revisions of this file
SHA384.cpp Show annotated file Show diff for this revision Revisions of this file
SHA384.h Show annotated file Show diff for this revision Revisions of this file
SHA512.cpp Show annotated file Show diff for this revision Revisions of this file
SHA512.h Show annotated file Show diff for this revision Revisions of this file
--- a/HashAlgorithm.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/HashAlgorithm.h	Thu Sep 12 16:03:43 2013 +0000
@@ -8,9 +8,10 @@
     public :
     
         virtual ~HashAlgorithm();
-        virtual void update(uint8_t *in, uint32_t length) = 0;
-        virtual void finalize(uint8_t *out) = 0;
+        
         virtual uint8_t outputSize() const = 0;
+        virtual void update(uint8_t *data, uint32_t length) = 0;
+        virtual void finalize(uint8_t *hash) = 0;
 };
 
 #endif
--- a/MD2.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/MD2.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -36,6 +36,92 @@
     memset(x, 0, 16);
 }
 
+uint8_t MD2::outputSize() const
+{
+    return 16;
+}
+
+void MD2::update(uint8_t *data, uint32_t length)
+{ 
+    if(bufferLength == 0)
+    {
+        while(length >= 16)
+        {
+            computeBlock(checksum, x, &l, data);
+            length -= 16;
+            data += 16;
+        }
+        bufferLength = length;
+        memcpy(buffer, data, length);
+    }
+    else if(length < 16-bufferLength)
+    {
+        memcpy(&buffer[bufferLength], data, length);
+        bufferLength += length;
+    }
+    else
+    {
+        int offset = 16-bufferLength;
+        memcpy(&buffer[bufferLength], data, offset);
+        computeBlock(checksum, x, &l, buffer);
+        data += offset;
+        length -= offset;
+        while(length >= 16)
+        {
+            computeBlock(checksum, x, &l, data);
+            data += 16;
+            length -= 16;
+        }
+        bufferLength = length;
+        memcpy(buffer, &data, length);
+    }
+    
+}
+
+void MD2::finalize(uint8_t *hash)
+{
+    // compute what's left data the buffer
+    int padding = 16 - bufferLength;
+    memset(&buffer[bufferLength], padding, padding);
+    computeBlock(checksum, x, &l, buffer);
+    computeBlock(checksum, x, &l, checksum);
+    memcpy(hash, x, 16);
+
+    uint32_t *x2 = (uint32_t*)x;
+    uint32_t *checksum2 = (uint32_t*)checksum;
+
+    // reset state
+    bufferLength = 0;
+    l = 0;
+    checksum2[0] = x2[0] = 0;
+    checksum2[1] = x2[1] = 0;
+    checksum2[2] = x2[2] = 0;
+    checksum2[3] = x2[3] = 0;
+}
+
+void MD2::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint8_t x[48];
+    uint8_t checksum[16];
+    uint8_t buffer[16];
+    memset(x, 0, 16);
+    memset(checksum, 0, 16);
+    uint8_t l = 0;
+    while(length >= 16)
+    {
+        computeBlock(checksum, x, &l, data);
+        length -= 16;
+        data += 16;
+    }
+
+    memcpy(buffer, data, length);
+    uint8_t padding = 16-length;
+    memset(&buffer[length], padding, padding);
+    computeBlock(checksum, x, &l, buffer);
+    computeBlock(checksum,x, &l, checksum);
+    memcpy(hash, x, 16);
+}
+
 void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
 {
     if(checksum2 != buffer2)
@@ -112,90 +198,4 @@
 
         t += j;
     }
-}
-
-void MD2::update(uint8_t *in, uint32_t length)
-{ 
-    if(bufferLength == 0)
-    {
-        while(length >= 16)
-        {
-            computeBlock(checksum, x, &l, in);
-            length -= 16;
-            in += 16;
-        }
-        bufferLength = length;
-        memcpy(buffer, in, length);
-    }
-    else if(length < 16-bufferLength)
-    {
-        memcpy(&buffer[bufferLength], in, length);
-        bufferLength += length;
-    }
-    else
-    {
-        int offset = 16-bufferLength;
-        memcpy(&buffer[bufferLength], in, offset);
-        computeBlock(checksum, x, &l, buffer);
-        in += offset;
-        length -= offset;
-        while(length >= 16)
-        {
-            computeBlock(checksum, x, &l, in);
-            in += 16;
-            length -= 16;
-        }
-        bufferLength = length;
-        memcpy(buffer, &in, length);
-    }
-    
-}
-
-void MD2::finalize(uint8_t *digest)
-{
-    // compute what's left in the buffer
-    int padding = 16 - bufferLength;
-    memset(&buffer[bufferLength], padding, padding);
-    computeBlock(checksum, x, &l, buffer);
-    computeBlock(checksum, x, &l, checksum);
-    memcpy(digest, x, 16);
-
-    uint32_t *x2 = (uint32_t*)x;
-    uint32_t *checksum2 = (uint32_t*)checksum;
-
-    // reset state
-    bufferLength = 0;
-    l = 0;
-    checksum2[0] = x2[0] = 0;
-    checksum2[1] = x2[1] = 0;
-    checksum2[2] = x2[2] = 0;
-    checksum2[3] = x2[3] = 0;
-}
-
-uint8_t MD2::outputSize() const
-{
-    return 16;
-}
-
-void MD2::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
-{
-    uint8_t x[48];
-    uint8_t checksum[16];
-    uint8_t buffer[16];
-    memset(x, 0, 16);
-    memset(checksum, 0, 16);
-    uint8_t l = 0;
-    while(length >= 16)
-    {
-        computeBlock(checksum, x, &l, in);
-        length -= 16;
-        in += 16;
-    }
-
-    memcpy(buffer, in, length);
-    uint8_t padding = 16-length;
-    memset(&buffer[length], padding, padding);
-    computeBlock(checksum, x, &l, buffer);
-    computeBlock(checksum,x, &l, checksum);
-    memcpy(digest, x, 16);
-}
+}
\ No newline at end of file
--- a/MD2.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/MD2.h	Thu Sep 12 16:03:43 2013 +0000
@@ -9,11 +9,11 @@
     
         MD2();
         
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
         
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
         
     private :
           
--- a/MD5.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/MD5.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -50,33 +50,34 @@
     return 16;
 }
 
-void MD5::update(uint8_t *in, uint32_t length)
+void MD5::update(uint8_t *data, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
-        memcpy(&buffer[bufferLength], in, length);
+        memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
         totalBufferLength += length;
         return;
     }
     int offset = 64-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
+    memcpy(&buffer[bufferLength], data, offset);
     computeRounds(&a, &b, &c, &d, buffer);
     while(length-offset > 64)
     {
-        memcpy(buffer, &in[offset], 64);
+        memcpy(buffer, &data[offset], 64);
         computeRounds(&a, &b, &c, &d, buffer);
         offset += 64;
     }
     if(offset > length)
         offset -= 64;
     bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    memcpy(buffer, &data[offset], bufferLength);
     totalBufferLength += length;
 }
 
-void MD5::finalize(uint8_t *digest)
+void MD5::finalize(uint8_t *hash)
 {
+    uint32_t *hash2 = (uint32_t*)hash;
     uint16_t padding;
     if(totalBufferLength % 64 < 56)
         padding = 56 - (totalBufferLength % 64);
@@ -99,10 +100,10 @@
     memcpy(&buffer[60], &lengthBitHigh, 4);
     computeRounds(&a, &b, &c, &d, buffer);
 
-    memcpy(digest, &a, 4);
-    memcpy(&digest[4], &b, 4);
-    memcpy(&digest[8], &c, 4);
-    memcpy(&digest[12], &d, 4);
+    hash2[0] = a;
+    hash2[1] = b;
+    hash2[2] = c;
+    hash2[3] = d;
     // reset state
     a = A;
     b = B;
@@ -112,9 +113,52 @@
     bufferLength = 0;
 }
 
+
+void MD5::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint32_t *hash2 = (uint32_t*)hash;
+    uint64_t lengthBit = length << 3;
+    uint16_t padding;
+    if(length % 64 < 56)
+        padding = 56 - (length % 64);
+    else
+        padding = 56 + (64 - (length % 64));
+        
+    uint32_t a = A, b = B, c = C, d = D;
+    while(length >= 64)
+    {
+        computeRounds(&a, &b, &c, &d, data);
+        data += 64;
+        length -= 64;
+    }
+    uint8_t buffer[64];
+    memcpy(buffer, data, length);
+    buffer[length++] = 0x80;
+    padding--;
+    if(padding+length == 56)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 64-length);
+        computeRounds(&a, &b, &c, &d, data);
+        memset(buffer, 0, 56);
+    }
+
+    uint32_t lengthBitLow = lengthBit;
+    uint32_t lengthBitHigh = lengthBit >> 32;
+    memcpy(&buffer[56], &lengthBitLow, 4);
+    memcpy(&buffer[60], &lengthBitHigh, 4);
+    
+    computeRounds(&a, &b, &c, &d, buffer);
+    
+    hash2[0] = a;
+    hash2[1] = b;
+    hash2[2] = c;
+    hash2[3] = d;
+}
+
 void MD5::computeRounds(uint32_t *a2, uint32_t *b2, uint32_t *c2, uint32_t *d2, uint8_t *buffer)
 {
-
     uint32_t a = *a2, b = *b2, c = *c2, d = *d2;
     uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d;
 
@@ -152,45 +196,3 @@
     *c2 = c + tmpC;
     *d2 = d + tmpD;
 }
-
-void MD5::computeDigest(uint8_t *digest, uint8_t *msg, uint32_t length)
-{
-    uint64_t lengthBit = length << 3;
-    uint16_t padding;
-    if(length % 64 < 56)
-        padding = 56 - (length % 64);
-    else
-        padding = 56 + (64 - (length % 64));
-        
-    uint32_t a = A, b = B, c = C, d = D;
-    while(length >= 64)
-    {
-        computeRounds(&a, &b, &c, &d, msg);
-        msg += 64;
-        length -= 64;
-    }
-    uint8_t buffer[64];
-    memcpy(buffer, msg, length);
-    buffer[length++] = 0x80;
-    padding--;
-    if(padding+length == 56)
-        memset(&buffer[length], 0, padding);
-    else
-    {
-        memset(&buffer[length], 0, 64-length);
-        computeRounds(&a, &b, &c, &d, msg);
-        memset(buffer, 0, 56);
-    }
-
-    uint32_t lengthBitLow = lengthBit;
-    uint32_t lengthBitHigh = lengthBit >> 32;
-    memcpy(&buffer[56], &lengthBitLow, 4);
-    memcpy(&buffer[60], &lengthBitHigh, 4);
-    
-    computeRounds(&a, &b, &c, &d, buffer);
-    
-    memcpy(digest, &a, 4);
-    memcpy(&digest[4], &b, 4);
-    memcpy(&digest[8], &c, 4);
-    memcpy(&digest[12], &d, 4);
-}
\ No newline at end of file
--- a/MD5.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/MD5.h	Thu Sep 12 16:03:43 2013 +0000
@@ -10,11 +10,11 @@
     
         MD5();
         
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
         
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
         
     private :
     
--- a/SHA1.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA1.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -59,34 +59,34 @@
     return 20;
 }
 
-void SHA1::update(uint8_t *in, uint32_t length)
+void SHA1::update(uint8_t *data, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
-        memcpy(&buffer[bufferLength], in, length);
+        memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
         totalBufferLength += length;
         return;
     }
     int offset = 64-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
+    memcpy(&buffer[bufferLength], data, offset);
     computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
     while(length-offset > 64)
     {
-        memcpy(buffer, &in[offset], 64);
+        memcpy(buffer, &data[offset], 64);
         computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
         offset += 64;
     }
     if(offset > length)
         offset -= 64;
     bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    memcpy(buffer, &data[offset], bufferLength);
     totalBufferLength += length;
 }
 
-void SHA1::finalize(uint8_t *digest)
+void SHA1::finalize(uint8_t *hash)
 {
-    uint32_t *digest2 = (uint32_t*)digest;
+    uint32_t *hash2 = (uint32_t*)hash;
     uint16_t padding;
     if(totalBufferLength % 64 < 56)
         padding = 56 - (totalBufferLength % 64);
@@ -113,11 +113,11 @@
     memcpy(&buffer[60], &lengthBitLow, 4);
     computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
     
-    digest2[0] = __rev(h0);
-    digest2[1] = __rev(h1);
-    digest2[2] = __rev(h2);
-    digest2[3] = __rev(h3);
-    digest2[4] = __rev(h4);
+    hash2[0] = __rev(h0);
+    hash2[1] = __rev(h1);
+    hash2[2] = __rev(h2);
+    hash2[3] = __rev(h3);
+    hash2[4] = __rev(h4);
     
     // reset state
     h0 = H0;
@@ -129,6 +129,54 @@
     bufferLength = 0;
 }
 
+
+void SHA1::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint32_t *hash2 = (uint32_t*)hash;
+    uint64_t lengthBit = length << 3;
+    uint32_t padding;
+    if(length % 64 < 56)
+        padding = 56 - (length % 64);
+    else
+        padding = 56 + (64 - (length % 64));
+        
+    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
+    while(length >= 64)
+    {
+        computeBlock(&h0,&h1,&h2,&h3,&h4, data);
+        length -= 64;
+        data += 64;
+    }
+   
+    uint8_t buffer[64];
+    memcpy(buffer, data, length);
+    buffer[length++] = 0x80;
+    padding--;
+    if(padding+length+8 == 64)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 64-length);
+        computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
+        memset(buffer, 0, 56);
+    }
+
+    uint32_t lengthBitLow = lengthBit;
+    uint32_t lengthBitHigh = lengthBit >> 32;
+    lengthBitLow = __rev(lengthBitLow);
+    lengthBitHigh = __rev(lengthBitHigh);
+    memcpy(&buffer[60], &lengthBitLow, 4);
+    memcpy(&buffer[56], &lengthBitHigh, 4);
+    
+    computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
+
+    hash2[0] = __rev(h0);
+    hash2[1] = __rev(h1);
+    hash2[2] = __rev(h2);
+    hash2[3] = __rev(h3);
+    hash2[4] = __rev(h4);
+}
+
 void SHA1::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, uint32_t *h42, uint8_t *buffer)
 {
     uint32_t *buffer2 = (uint32_t*)buffer;
@@ -171,58 +219,3 @@
     *h42 += e;
 }
 
-
-
-/* method 1 */
-void SHA1::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
-{
-    uint64_t lengthBit = length << 3;
-    uint32_t padding;
-    if(length % 64 < 56)
-        padding = 56 - (length % 64);
-    else
-        padding = 56 + (64 - (length % 64));
-        
-    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
-    while(length >= 64)
-    {
-        computeBlock(&h0,&h1,&h2,&h3,&h4, in);
-        length -= 64;
-        in += 64;
-    }
-   
-    uint8_t buffer[64];
-    memcpy(buffer, in, length);
-    buffer[length++] = 0x80;
-    padding--;
-    if(padding+length+8 == 64)
-        memset(&buffer[length], 0, padding);
-    else
-    {
-        memset(&buffer[length], 0, 64-length);
-        computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
-        memset(buffer, 0, 56);
-    }
-
-    uint32_t lengthBitLow = lengthBit;
-    uint32_t lengthBitHigh = lengthBit >> 32;
-    lengthBitLow = __rev(lengthBitLow);
-    lengthBitHigh = __rev(lengthBitHigh);
-    memcpy(&buffer[60], &lengthBitLow, 4);
-    memcpy(&buffer[56], &lengthBitHigh, 4);
-    
-    computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
-
-    h0 = __rev(h0);
-    h1 = __rev(h1);
-    h2 = __rev(h2);
-    h3 = __rev(h3);
-    h4 = __rev(h4);
-    
-    memcpy(digest, &h0, 4);
-    memcpy(&digest[4], &h1, 4);
-    memcpy(&digest[8], &h2, 4);
-    memcpy(&digest[12], &h3, 4);
-    memcpy(&digest[16], &h4, 4);
-}
-
--- a/SHA1.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA1.h	Thu Sep 12 16:03:43 2013 +0000
@@ -9,12 +9,12 @@
     public :
     
         SHA1();
+
+        virtual uint8_t outputSize() const;        
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
         
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
-        virtual uint8_t outputSize() const;
-        
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
         
     private :
         static void computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, uint32_t *h42, uint8_t *buffer);
--- a/SHA224.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA224.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -7,22 +7,22 @@
 {
 }
 
-void SHA224::update(uint8_t *in, uint32_t length)
-{
-    algo.update(in, length);
-}
-
-void SHA224::finalize(uint8_t *out)
-{
-    algo.finalize(out);
-}
-
 uint8_t SHA224::outputSize() const
 {
     return 28;
 }
 
-void SHA224::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
+void SHA224::update(uint8_t *data, uint32_t length)
+{
+    algo.update(data, length);
+}
+
+void SHA224::finalize(uint8_t *hash)
 {
-    SHA2_32::computeDigest(SHA_224, digest, in, length);
+    algo.finalize(hash);
 }
+
+void SHA224::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    SHA2_32::computeHash(SHA_224, hash, data, length);
+}
--- a/SHA224.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA224.h	Thu Sep 12 16:03:43 2013 +0000
@@ -9,11 +9,12 @@
     public :
 
         SHA224();
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
+
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
 
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
 
     private :
     
--- a/SHA256.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA256.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -7,22 +7,22 @@
 {
 }
 
-void SHA256::update(uint8_t *in, uint32_t length)
-{
-    algo.update(in, length);
-}
-
-void SHA256::finalize(uint8_t *out)
-{
-    algo.finalize(out);
-}
-
 uint8_t SHA256::outputSize() const
 {
     return 32;
 }
 
-void SHA256::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
+void SHA256::update(uint8_t *data, uint32_t length)
+{
+    algo.update(data, length);
+}
+
+void SHA256::finalize(uint8_t *hash)
 {
-    SHA2_32::computeDigest(SHA_256, digest, in, length);
+    algo.finalize(hash);
 }
+
+void SHA256::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    SHA2_32::computeHash(SHA_256, hash, data, length);
+}
--- a/SHA256.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA256.h	Thu Sep 12 16:03:43 2013 +0000
@@ -10,11 +10,12 @@
     public :
 
         SHA256();
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
+        
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
 
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
 
     private :
     
--- a/SHA2_32.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA2_32.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -66,33 +66,34 @@
     }
 }
 
-void SHA2_32::update(uint8_t *in, uint32_t length)
+void SHA2_32::update(uint8_t *data, uint32_t length)
 {
     if(length < 64-bufferLength)
     {
-        memcpy(&buffer[bufferLength], in, length);
+        memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
         totalBufferLength += length;
         return;
     }
     int offset = 64-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
+    memcpy(&buffer[bufferLength], data, offset);
     computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
     while(length-offset > 64)
     {
-        memcpy(buffer, &in[offset], 64);
+        memcpy(buffer, &data[offset], 64);
         computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
         offset += 64;
     }
     if(offset > length)
         offset -= 64;
     bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    memcpy(buffer, &data[offset], bufferLength);
     totalBufferLength += length;
 }
 
-void SHA2_32::finalize(uint8_t *digest)
+void SHA2_32::finalize(uint8_t *hash)
 {
+    uint32_t *hash2 = (uint32_t*)hash;
     uint16_t padding;
     if(totalBufferLength % 64 < 56)
         padding = 56 - (totalBufferLength % 64);
@@ -119,26 +120,17 @@
     memcpy(&buffer[56], &lengthBitHigh, 4);    
     computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
 
-    h0 = __rev(h0);
-    h1 = __rev(h1);
-    h2 = __rev(h2);
-    h3 = __rev(h3);
-    h4 = __rev(h4);
-    h5 = __rev(h5);
-    h6 = __rev(h6);
-    memcpy(digest, &h0, 4);
-    memcpy(&digest[4], &h1, 4);
-    memcpy(&digest[8], &h2, 4);
-    memcpy(&digest[12], &h3, 4);
-    memcpy(&digest[16], &h4, 4);
-    memcpy(&digest[20], &h5, 4);
-    memcpy(&digest[24], &h6, 4);
+    hash2[0] = __rev(h0);
+    hash2[1] = __rev(h1);
+    hash2[2] = __rev(h2);
+    hash2[3] = __rev(h3);
+    hash2[4] = __rev(h4);
+    hash2[5] = __rev(h5);
+    hash2[6] = __rev(h6);
+
     
     if(type == SHA_256)
-    {
-        h7 = __rev(h7);
-        memcpy(&digest[28], &h7, 4);
-    }
+        hash2[7] = __rev(h7);
     
     // reset state
     switch(type)
@@ -169,6 +161,58 @@
     bufferLength = 0;
 }
 
+void SHA2_32::computeHash(SHA_32_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint32_t *hash2 = (uint32_t*)hash;
+    uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
+    uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
+    uint64_t lengthBit = length << 3;
+    uint16_t padding;
+    if(length % 64 < 56)
+        padding = 56 - (length % 64);
+    else
+        padding = 56 + (64 - (length % 64));
+        
+    while(length >= 64)
+    {
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, data);
+        length -= 64;
+        data += 64;
+    }
+    uint8_t buffer[64];
+    memcpy(buffer, data,length); 
+    buffer[length++] = 0x80;
+    padding--;
+    if(padding+length == 56)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 64-length);
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+        memset(buffer, 0, 56);
+    }
+    
+    uint32_t lengthBitLow = lengthBit;
+    uint32_t lengthBitHigh = lengthBit >> 32;
+    lengthBitLow = __rev(lengthBitLow);
+    memcpy(&buffer[60], &lengthBitLow, 4);
+    lengthBitHigh = __rev(lengthBitHigh);
+    memcpy(&buffer[56], &lengthBitHigh, 4);    
+    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+
+    hash2[0] = __rev(h0);
+    hash2[1] = __rev(h1);
+    hash2[2] = __rev(h2);
+    hash2[3] = __rev(h3);
+    hash2[4] = __rev(h4);
+    hash2[5] = __rev(h5);
+    hash2[6] = __rev(h6);
+
+    
+    if(type == SHA_256)
+        hash2[7] = __rev(h7);
+}
+
 void SHA2_32::computeBlock(uint32_t *h02, 
                         uint32_t *h12, 
                         uint32_t *h22, 
@@ -268,64 +312,3 @@
     *h72 += h;
 }
 
-void SHA2_32::computeDigest(SHA_32_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length)
-{
-    uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
-    uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
-    uint64_t lengthBit = length << 3;
-    uint16_t padding;
-    if(length % 64 < 56)
-        padding = 56 - (length % 64);
-    else
-        padding = 56 + (64 - (length % 64));
-        
-    while(length >= 64)
-    {
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in);
-        length -= 64;
-        in += 64;
-    }
-    uint8_t buffer[64];
-    memcpy(buffer, in,length); 
-    buffer[length++] = 0x80;
-    padding--;
-    if(padding+length == 56)
-        memset(&buffer[length], 0, padding);
-    else
-    {
-        memset(&buffer[length], 0, 64-length);
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, 56);
-    }
-    
-    uint32_t lengthBitLow = lengthBit;
-    uint32_t lengthBitHigh = lengthBit >> 32;
-    lengthBitLow = __rev(lengthBitLow);
-    memcpy(&buffer[60], &lengthBitLow, 4);
-    lengthBitHigh = __rev(lengthBitHigh);
-    memcpy(&buffer[56], &lengthBitHigh, 4);    
-    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-
-    h0 = __rev(h0);
-    h1 = __rev(h1);
-    h2 = __rev(h2);
-    h3 = __rev(h3);
-    h4 = __rev(h4);
-    h5 = __rev(h5);
-    h6 = __rev(h6);
-
-    memcpy(digest, &h0, 4);
-    memcpy(&digest[4], &h1, 4);
-    memcpy(&digest[8], &h2, 4);
-    memcpy(&digest[12], &h3, 4);
-    memcpy(&digest[16], &h4, 4);
-    memcpy(&digest[20], &h5, 4);
-    memcpy(&digest[24], &h6, 4);
-
-
-    if(type == SHA_256)
-    {
-        h7 = __rev(h7);
-        memcpy(&digest[28], &h7, 4);
-    }
-}
--- a/SHA2_32.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA2_32.h	Thu Sep 12 16:03:43 2013 +0000
@@ -14,9 +14,10 @@
     public :
     
         SHA2_32(SHA_32_TYPE type);
-        void update(uint8_t *in, uint32_t length);
+        void update(uint8_t *data, uint32_t length);
         void finalize(uint8_t *digest);
-        static void computeDigest(SHA_32_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length);
+        
+        static void computeHash(SHA_32_TYPE type, uint8_t *digest, uint8_t *data, uint32_t length);
         
     private : 
     
--- a/SHA2_64.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA2_64.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -65,33 +65,34 @@
     }
 }
 
-void SHA2_64::update(uint8_t *in, uint32_t length)
+void SHA2_64::update(uint8_t *data, uint32_t length)
 {
     if(length < 128-bufferLength)
     {
-        memcpy(&buffer[bufferLength], in, length);
+        memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
         totalBufferLength += length;
         return;
     }
     int offset = 128-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
+    memcpy(&buffer[bufferLength], data, offset);
     computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
     while(length-offset > 128)
     {
-        memcpy(buffer, &in[offset], 128);
+        memcpy(buffer, &data[offset], 128);
         computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
         offset += 128;
     }
     if(offset > length)
         offset -= 128;
     bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    memcpy(buffer, &data[offset], bufferLength);
     totalBufferLength += length;
 }
 
-void SHA2_64::finalize(uint8_t *digest)
+void SHA2_64::finalize(uint8_t *hash)
 {
+    uint64_t *hash2 = (uint64_t*)hash;
     uint64_t lengthBit = totalBufferLength << 3;
     uint32_t padding;
     if(totalBufferLength % 128 < 112)
@@ -116,27 +117,18 @@
     computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
 
 
-    h0 = revWord(h0);
-    h1 = revWord(h1);
-    h2 = revWord(h2);
-    h3 = revWord(h3);
-    h4 = revWord(h4);
-    h5 = revWord(h5);
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
+    hash2[5] = revWord(h5);
 
-    
-    memcpy(digest, &h0, 8);
-    memcpy(&digest[8], &h1, 8);
-    memcpy(&digest[16], &h2, 8);
-    memcpy(&digest[24], &h3, 8);
-    memcpy(&digest[32], &h4, 8);
-    memcpy(&digest[40], &h5, 8);
 
     if(type == SHA_512)
     {
-        h6 = revWord(h6);
-        h7 = revWord(h7);
-        memcpy(&digest[48], &h6, 8);
-        memcpy(&digest[56], &h7, 8);
+        hash2[6] = revWord(h6);
+        hash2[7] = revWord(h7);
     }
     
     // reset state
@@ -168,6 +160,60 @@
     bufferLength = 0;
 }
 
+void SHA2_64::computeHash(SHA2_64_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint64_t *hash2 = (uint64_t*)hash;
+    uint64_t lengthBit = length * 8;
+    uint64_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
+    uint64_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
+    
+    int padding;
+    if(length % 128 < 112)
+        padding = 112 - (length % 128);
+    else
+        padding = 112 + (128 - (length % 128));
+        
+    while(length >= 128)
+    {
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, data);
+        data += 128;
+        length -= 128;
+    }
+    uint8_t buffer[128];
+    memcpy(buffer, data,length); 
+    buffer[length] = 0x80;
+    length++;
+    padding--;
+
+    if(padding+length == 112)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 128-length);
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+        memset(buffer, 0, 112);
+    }
+    
+    lengthBit = revWord(lengthBit);
+    memset(&buffer[112], 0, 8); 
+    memcpy(&buffer[120], &lengthBit, 8);
+    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
+    hash2[5] = revWord(h5);
+
+
+    if(type == SHA_512)
+    {
+        hash2[6] = revWord(h6);
+        hash2[7] = revWord(h7);
+    }
+}
+
 void SHA2_64::computeBlock(uint64_t *h02, 
                      uint64_t *h12, 
                      uint64_t *h22, 
@@ -306,66 +352,3 @@
     *h62 += g;
     *h72 += h;
 }
-
-void SHA2_64::computeDigest(SHA2_64_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length)
-{
-    uint64_t lengthBit = length * 8;
-    uint64_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
-    uint64_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
-    
-    int padding;
-    if(length % 128 < 112)
-        padding = 112 - (length % 128);
-    else
-        padding = 112 + (128 - (length % 128));
-        
-    while(length >= 128)
-    {
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in);
-        in += 128;
-        length -= 128;
-    }
-    uint8_t buffer[128];
-    memcpy(buffer, in,length); 
-    buffer[length] = 0x80;
-    length++;
-    padding--;
-
-    if(padding+length == 112)
-        memset(&buffer[length], 0, padding);
-    else
-    {
-        memset(&buffer[length], 0, 128-length);
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, 112);
-    }
-    
-    lengthBit = revWord(lengthBit);
-    memset(&buffer[112], 0, 8); 
-    memcpy(&buffer[120], &lengthBit, 8);
-    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-
-    h0 = revWord(h0);
-    h1 = revWord(h1);
-    h2 = revWord(h2);
-    h3 = revWord(h3);
-    h4 = revWord(h4);
-    h5 = revWord(h5);
-
-    
-    memcpy(digest, &h0, 8);
-    memcpy(&digest[8], &h1, 8);
-    memcpy(&digest[16], &h2, 8);
-    memcpy(&digest[24], &h3, 8);
-    memcpy(&digest[32], &h4, 8);
-    memcpy(&digest[40], &h5, 8);
-
-    if(type == SHA_512)
-    {
-        h6 = revWord(h6);
-        h7 = revWord(h7);
-        memcpy(&digest[48], &h6, 8);
-        memcpy(&digest[56], &h7, 8);
-    }
-}
-
--- a/SHA2_64.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA2_64.h	Thu Sep 12 16:03:43 2013 +0000
@@ -14,9 +14,11 @@
     public :
 
         SHA2_64(SHA2_64_TYPE type);
-        void update(uint8_t *in, uint32_t length);
-        void finalize(uint8_t *digest);
-        static void computeDigest(SHA2_64_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length);
+        
+        void update(uint8_t *data, uint32_t length);
+        void finalize(uint8_t *hash);
+        
+        static void computeHash(SHA2_64_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length);
 
     private :
     
--- a/SHA384.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA384.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -7,22 +7,22 @@
 {
 }
 
-void SHA384::update(uint8_t *in, uint32_t length)
-{
-    algo.update(in, length);
-}
-
-void SHA384::finalize(uint8_t *out)
-{
-    algo.finalize(out);
-}
-
 uint8_t SHA384::outputSize() const
 {
     return 48;
 }
 
-void SHA384::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
+void SHA384::update(uint8_t *data, uint32_t length)
+{
+    algo.update(data, length);
+}
+
+void SHA384::finalize(uint8_t *hash)
 {
-    SHA2_64::computeDigest(SHA_384, digest, in, length);
+    algo.finalize(hash);
 }
+
+void SHA384::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    SHA2_64::computeHash(SHA_384, hash, data, length);
+}
--- a/SHA384.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA384.h	Thu Sep 12 16:03:43 2013 +0000
@@ -10,11 +10,12 @@
     public :
 
         SHA384();
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
+        
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
 
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
 
     private :
     
--- a/SHA512.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA512.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -7,22 +7,22 @@
 {
 }
 
-void SHA512::update(uint8_t *in, uint32_t length)
-{
-    algo.update(in, length);
-}
-
-void SHA512::finalize(uint8_t *out)
-{
-    algo.finalize(out);
-}
-
 uint8_t SHA512::outputSize() const
 {
     return 64;
 }
 
-void SHA512::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
+void SHA512::update(uint8_t *data, uint32_t length)
+{
+    algo.update(data, length);
+}
+
+void SHA512::finalize(uint8_t *hash)
 {
-    SHA2_64::computeDigest(SHA_512, digest, in, length);
+    algo.finalize(hash);
 }
+
+void SHA512::computeHash(uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    SHA2_64::computeHash(SHA_512, hash, data, length);
+}
--- a/SHA512.h	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA512.h	Thu Sep 12 16:03:43 2013 +0000
@@ -10,11 +10,12 @@
     public :
 
         SHA512();
-        virtual void update(uint8_t *in, uint32_t length);
-        virtual void finalize(uint8_t *out);
+        
         virtual uint8_t outputSize() const;
+        virtual void update(uint8_t *data, uint32_t length);
+        virtual void finalize(uint8_t *hash);
 
-        static void computeDigest(uint8_t *digest, uint8_t *in, uint32_t length);
+        static void computeHash(uint8_t *hash, uint8_t *data, uint32_t length);
 
     private :