SHA1

3 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
SHA1

I am trying to use utility.library's SHA1 hash functions. I thought I had it all correct, but I only get garbage for the final hash.

  1. char SAK_SHA1Hash[20];
  2. struct MessageDigest_SHA *MD_SHA1;
  3.  
  4. if (!(MD_SHA1=(struct MessageDigest_SHA *)AllocVecTags(sizeof(struct MessageDigest_SHA),
  5. AVT_Type, MEMF_SHARED,
  6. AVT_ClearWithValue, 0,
  7. TAG_DONE)))
  8. {
  9. SAK_EasyRequest(Objects[OID_WINDOW],MainWindow,RequesterTitle,REQIMAGE_ERROR,0,SAK_LocaleString(MSG_COULD_NOT_ALLOCATE_MEMORY),SAK_LocaleString(MSG_TERMINATE));
  10.  
  11. return(FALSE);
  12. }
  13.  
  14.  
  15. BOOL
  16. SAK_GetFileSHA1(STRPTR Path)
  17. {
  18. unsigned char buf[1024];
  19. int n;
  20. FILE *fp;
  21.  
  22. MessageDigest_SHA_Init(MD_SHA1);
  23.  
  24. if (!(fp=fopen(Path,"r")))
  25. return(FALSE);
  26.  
  27. while((n=fread(buf,1,sizeof(buf),fp))>0)
  28. MessageDigest_SHA_Update(MD_SHA1,buf,n);
  29. fclose(fp);
  30.  
  31. MessageDigest_SHA_Final(MD_SHA1);
  32.  
  33. if (ferror(fp))
  34. return(FALSE);
  35.  
  36. Strlcpy((STRPTR)SAK_SHA1Hash,(STRPTR)MD_SHA1->mdsha_Code,20);
  37.  
  38. return(TRUE);
  39. }
  40.  
  41. ..............
  42.  
  43. SAK_GetFileSHA1("C:ftp");
  44. Printf((STRPTR)SAK_SHA1Hash);

The final out is just random garbage characters. What do I have wrong?

salass00
salass00's picture
Offline
Last seen: 7 months 1 week ago
Joined: 2011-02-03 11:27
First off the returned hash

First off the returned hash is binary so you can't print it directly like you are doing. Also it is not NUL-terminated so by using Strlcpy() instead of memcpy() or CopyMem() you lose the last byte of the hash.

The following code calculates the hash of a file and prints it out as a hexadecimal string:

  1. /* Compile with:
  2.   ppc-amigaos-gcc -O2 -g -Wall -Werror -Wwrite-strings -o sha1sum sha1sum.c
  3.  */
  4.  
  5. #include <utility/message_digest.h>
  6. #include <proto/exec.h>
  7. #include <proto/dos.h>
  8. #include <proto/utility.h>
  9.  
  10. BOOL GetFileSHA1(CONST_STRPTR path, uint8 *hash) {
  11. BPTR file;
  12. int nb;
  13. uint8 buffer[1024];
  14. struct MessageDigest_SHA mdsha;
  15.  
  16. file = IDOS->Open(path, MODE_OLDFILE);
  17. if (file == ZERO)
  18. return FALSE;
  19.  
  20. IUtility->MessageDigest_SHA_Init(&mdsha);
  21.  
  22. while ((nb = IDOS->Read(file, buffer, sizeof(buffer))) > 0)
  23. IUtility->MessageDigest_SHA_Update(&mdsha, buffer, nb);
  24.  
  25. IDOS->Close(file);
  26.  
  27. if (nb < 0)
  28. return FALSE;
  29.  
  30. IUtility->MessageDigest_SHA_Final(&mdsha);
  31.  
  32. IExec->CopyMem(mdsha.mdsha_Code, hash, 20);
  33.  
  34. return TRUE;
  35. }
  36.  
  37. int main(int argc, char **argv) {
  38. uint8 hash[20];
  39. int i;
  40.  
  41. if (argc != 2) {
  42. IDOS->PutStr("Usage: sha1sum <file>\n");
  43. return 0;
  44. }
  45.  
  46. if (GetFileSHA1(argv[1], hash)) {
  47. IDOS->PutStr("SHA1: ");
  48.  
  49. for (i = 0; i < sizeof(hash); i++)
  50. IDOS->Printf("%02lx", hash[i]);
  51.  
  52. IDOS->PutStr("\n");
  53. } else
  54. IDOS->PrintFault(IDOS->IoErr(), argv[1]);
  55.  
  56. return 0;
  57. }
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
I had a hunch about it being

I had a hunch about it being in HEX. Didn't know about using memcpy(). I couldn't find any example; maybe you should put your entire reply in the Autodocs for the next person.

Thank you!

Log in or register to post comments