sha512.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/sha512.h"
  15. #endif
  16. #include <apt-pkg/sha512.h>
  17. #include <apt-pkg/sha2.h>
  18. #include <apt-pkg/strutl.h>
  19. SHA512Summation::SHA512Summation() /*{{{*/
  20. {
  21. SHA512_Init(&ctx);
  22. Done = false;
  23. }
  24. /*}}}*/
  25. bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
  26. {
  27. if (Done)
  28. return false;
  29. SHA512_Update(&ctx, inbuf, len);
  30. return true;
  31. }
  32. /*}}}*/
  33. SHA512SumValue SHA512Summation::Result() /*{{{*/
  34. {
  35. if (!Done) {
  36. SHA512_Final(Sum, &ctx);
  37. Done = true;
  38. }
  39. SHA512SumValue res;
  40. res.Set(Sum);
  41. return res;
  42. }
  43. /*}}}*/
  44. // SHA512SumValue::SHA512SumValue - Constructs the sum from a string /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* The string form of a SHA512 is a 64 character hex number */
  47. SHA512SumValue::SHA512SumValue(string Str)
  48. {
  49. memset(Sum,0,sizeof(Sum));
  50. Set(Str);
  51. }
  52. /*}}}*/
  53. // SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* Sets the value to 0 */
  56. SHA512SumValue::SHA512SumValue()
  57. {
  58. memset(Sum,0,sizeof(Sum));
  59. }
  60. /*}}}*/
  61. // SHA512SumValue::Set - Set the sum from a string /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* Converts the hex string into a set of chars */
  64. bool SHA512SumValue::Set(string Str)
  65. {
  66. return Hex2Num(Str,Sum,sizeof(Sum));
  67. }
  68. /*}}}*/
  69. // SHA512SumValue::Value - Convert the number into a string /*{{{*/
  70. // ---------------------------------------------------------------------
  71. /* Converts the set of chars into a hex string in lower case */
  72. string SHA512SumValue::Value() const
  73. {
  74. char Conv[16] =
  75. { '0','1','2','3','4','5','6','7','8','9','a','b',
  76. 'c','d','e','f'
  77. };
  78. char Result[129];
  79. Result[128] = 0;
  80. // Convert each char into two letters
  81. int J = 0;
  82. int I = 0;
  83. for (; I != 128; J++,I += 2)
  84. {
  85. Result[I] = Conv[Sum[J] >> 4];
  86. Result[I + 1] = Conv[Sum[J] & 0xF];
  87. }
  88. return string(Result);
  89. }
  90. /*}}}*/
  91. // SHA512SumValue::operator == - Comparator /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* Call memcmp on the buffer */
  94. bool SHA512SumValue::operator == (const SHA512SumValue & rhs) const
  95. {
  96. return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
  97. }
  98. /*}}}*/
  99. // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* */
  102. bool SHA512Summation::AddFD(int Fd,unsigned long Size)
  103. {
  104. unsigned char Buf[64 * 64];
  105. int Res = 0;
  106. int ToEOF = (Size == 0);
  107. while (Size != 0 || ToEOF)
  108. {
  109. unsigned n = sizeof(Buf);
  110. if (!ToEOF) n = min(Size,(unsigned long)n);
  111. Res = read(Fd,Buf,n);
  112. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  113. return false;
  114. if (ToEOF && Res == 0) // EOF
  115. break;
  116. Size -= Res;
  117. Add(Buf,Res);
  118. }
  119. return true;
  120. }
  121. /*}}}*/