sha2.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. SHA512Summation::SHA512Summation() /*{{{*/
  19. {
  20. SHA512_Init(&ctx);
  21. Done = false;
  22. }
  23. /*}}}*/
  24. SHA512SumValue SHA512Summation::Result() /*{{{*/
  25. {
  26. if (!Done) {
  27. SHA512_Final(Sum, &ctx);
  28. Done = true;
  29. }
  30. SHA512SumValue res;
  31. res.Set(Sum);
  32. return res;
  33. }
  34. /*}}}*/
  35. bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
  36. {
  37. if (Done)
  38. return false;
  39. SHA512_Update(&ctx, inbuf, len);
  40. return true;
  41. }
  42. /*}}}*/
  43. // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. bool SHA512Summation::AddFD(int Fd,unsigned long Size)
  47. {
  48. unsigned char Buf[64 * 64];
  49. int Res = 0;
  50. int ToEOF = (Size == 0);
  51. while (Size != 0 || ToEOF)
  52. {
  53. unsigned n = sizeof(Buf);
  54. if (!ToEOF) n = min(Size,(unsigned long)n);
  55. Res = read(Fd,Buf,n);
  56. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  57. return false;
  58. if (ToEOF && Res == 0) // EOF
  59. break;
  60. Size -= Res;
  61. Add(Buf,Res);
  62. }
  63. return true;
  64. }
  65. /*}}}*/
  66. SHA256Summation::SHA256Summation() /*{{{*/
  67. {
  68. SHA256_Init(&ctx);
  69. Done = false;
  70. }
  71. /*}}}*/
  72. bool SHA256Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
  73. {
  74. if (Done)
  75. return false;
  76. SHA256_Update(&ctx, inbuf, len);
  77. return true;
  78. }
  79. /*}}}*/
  80. SHA256SumValue SHA256Summation::Result() /*{{{*/
  81. {
  82. if (!Done) {
  83. SHA256_Final(Sum, &ctx);
  84. Done = true;
  85. }
  86. SHA256SumValue res;
  87. res.Set(Sum);
  88. return res;
  89. }
  90. /*}}}*/
  91. // SHA256Summation::AddFD - Add content of file into the checksum /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* */
  94. bool SHA256Summation::AddFD(int Fd,unsigned long Size)
  95. {
  96. unsigned char Buf[64 * 64];
  97. int Res = 0;
  98. int ToEOF = (Size == 0);
  99. while (Size != 0 || ToEOF)
  100. {
  101. unsigned n = sizeof(Buf);
  102. if (!ToEOF) n = min(Size,(unsigned long)n);
  103. Res = read(Fd,Buf,n);
  104. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  105. return false;
  106. if (ToEOF && Res == 0) // EOF
  107. break;
  108. Size -= Res;
  109. Add(Buf,Res);
  110. }
  111. return true;
  112. }
  113. /*}}}*/