sha2.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Cryptographic API. {{{
  3. *
  4. * SHA-512, as specified in
  5. * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. */ /*}}}*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/sha2.h"
  15. #endif
  16. #include <apt-pkg/sha2.h>
  17. #include <apt-pkg/strutl.h>
  18. // SHA2Summation::AddFD - Add content of file into the checksum /*{{{*/
  19. // ---------------------------------------------------------------------
  20. /* */
  21. bool SHA2SummationBase::AddFD(int Fd,unsigned long Size){
  22. unsigned char Buf[64 * 64];
  23. int Res = 0;
  24. int ToEOF = (Size == 0);
  25. while (Size != 0 || ToEOF)
  26. {
  27. unsigned n = sizeof(Buf);
  28. if (!ToEOF) n = min(Size,(unsigned long)n);
  29. Res = read(Fd,Buf,n);
  30. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  31. return false;
  32. if (ToEOF && Res == 0) // EOF
  33. break;
  34. Size -= Res;
  35. Add(Buf,Res);
  36. }
  37. return true;
  38. }
  39. /*}}}*/