add md5sum command for checking file intergrity and sdcard reads
authorJim Morris <morris@wolfman.com>
Sun, 30 Aug 2015 06:30:38 +0000 (23:30 -0700)
committerJim Morris <morris@wolfman.com>
Sun, 30 Aug 2015 06:30:38 +0000 (23:30 -0700)
src/libs/md5.cpp
src/modules/utils/simpleshell/SimpleShell.cpp
src/modules/utils/simpleshell/SimpleShell.h

index 3fc27fd..9ed1d97 100644 (file)
@@ -35,6 +35,7 @@ documentation and/or software.
 
 /* system implementation headers */
 #include <string.h>
+#include <stdio.h>
 
 // Constants for MD5Transform routine.
 #define S11 7
@@ -338,18 +339,18 @@ MD5 &MD5::finalize()
 //////////////////////////////
 
 // return hex representation of digest as string
-// std::string MD5::hexdigest() const
-// {
-//     if (!finalized)
-//         return "";
+std::string MD5::hexdigest() const
+{
+    if (!finalized)
+        return "";
 
-//     char buf[33];
-//     for (int i = 0; i < 16; i++)
-//         sprintf(buf + i * 2, "%02x", digest[i]);
-//     buf[32] = 0;
+    char buf[33];
+    for (int i = 0; i < 16; i++)
+        sprintf(buf + i * 2, "%02x", digest[i]);
+    buf[32] = 0;
 
-//     return std::string(buf);
-// }
+    return std::string(buf);
+}
 
 // return the slected number of bytes from the digest
 void MD5::bindigest(void *buf, int len) const
@@ -359,9 +360,9 @@ void MD5::bindigest(void *buf, int len) const
 
 //////////////////////////////
 
-std::string md5(const std::string str)
-{
-    MD5 md5 = MD5(str);
+// std::string md5(const std::string str)
+// {
+//     MD5 md5 = MD5(str);
 
-    return md5.hexdigest();
-}
+//     return md5.hexdigest();
+// }
index 3db984f..7e66473 100644 (file)
@@ -31,6 +31,7 @@
 #include "SwitchPublicAccess.h"
 #include "SDFAT.h"
 #include "Thermistor.h"
+#include "md5.h"
 
 #include "system_LPC17xx.h"
 #include "LPC17xx.h"
@@ -73,6 +74,7 @@ const SimpleShell::ptentry_t SimpleShell::commands_table[] = {
     {"remount",  SimpleShell::remount_command},
     {"calc_thermistor", SimpleShell::calc_thermistor_command},
     {"thermistors", SimpleShell::print_thermistors_command},
+    {"md5sum",   SimpleShell::md5sum_command},
 
     // unknown command
     {NULL, NULL}
@@ -659,6 +661,29 @@ void SimpleShell::switch_command( string parameters, StreamOutput *stream)
     }
 }
 
+void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
+{
+    string filename = absolute_from_relative(parameters);
+
+    // Open file
+    FILE *lp = fopen(filename.c_str(), "r");
+    if (lp == NULL) {
+        stream->printf("File not found: %s\r\n", filename.c_str());
+        return;
+    }
+    MD5 md5;
+    uint8_t buf[64];
+    do {
+        size_t n= fread(buf, 1, sizeof buf, lp);
+        if(n > 0) md5.update(buf, n);
+    } while(!feof(lp));
+
+    stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
+    fclose(lp);
+}
+
+
+
 void SimpleShell::help_command( string parameters, StreamOutput *stream )
 {
     stream->printf("Commands:\r\n");
@@ -688,5 +713,6 @@ void SimpleShell::help_command( string parameters, StreamOutput *stream )
     stream->printf("upload filename - saves a stream of text to the named file\r\n");
     stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
     stream->printf("thermistors - print out the predefined thermistors\r\n");
+    stream->printf("md5sum file - prints md5 sum of the given file\r\n");
 }
 
index cb7c35d..e1ec86d 100644 (file)
@@ -46,6 +46,7 @@ private:
     static void set_temp_command(string parameters, StreamOutput *stream );
     static void calc_thermistor_command( string parameters, StreamOutput *stream);
     static void print_thermistors_command( string parameters, StreamOutput *stream);
+    static void md5sum_command( string parameters, StreamOutput *stream);
 
     static void switch_command(string parameters, StreamOutput *stream );
     static void mem_command(string parameters, StreamOutput *stream );