hash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "hash.h"
  22. #include "adler32.h"
  23. #include "crc.h"
  24. #include "md5.h"
  25. #include "murmur3.h"
  26. #include "ripemd.h"
  27. #include "sha.h"
  28. #include "sha512.h"
  29. #include "avstring.h"
  30. #include "error.h"
  31. #include "intreadwrite.h"
  32. #include "mem.h"
  33. enum hashtype {
  34. MD5,
  35. MURMUR3,
  36. RIPEMD128,
  37. RIPEMD160,
  38. RIPEMD256,
  39. RIPEMD320,
  40. SHA160,
  41. SHA224,
  42. SHA256,
  43. SHA512_224,
  44. SHA512_256,
  45. SHA384,
  46. SHA512,
  47. CRC32,
  48. ADLER32,
  49. NUM_HASHES
  50. };
  51. typedef struct AVHashContext {
  52. void *ctx;
  53. enum hashtype type;
  54. const AVCRC *crctab;
  55. uint32_t crc;
  56. } AVHashContext;
  57. struct {
  58. const char *name;
  59. int size;
  60. } hashdesc[] = {
  61. [MD5] = {"MD5", 16},
  62. [MURMUR3] = {"murmur3", 16},
  63. [RIPEMD128] = {"RIPEMD128", 16},
  64. [RIPEMD160] = {"RIPEMD160", 20},
  65. [RIPEMD256] = {"RIPEMD256", 32},
  66. [RIPEMD320] = {"RIPEMD320", 40},
  67. [SHA160] = {"SHA160", 20},
  68. [SHA224] = {"SHA224", 28},
  69. [SHA256] = {"SHA256", 32},
  70. [SHA512_224] = {"SHA512/224", 28},
  71. [SHA512_256] = {"SHA512/256", 32},
  72. [SHA384] = {"SHA384", 48},
  73. [SHA512] = {"SHA512", 64},
  74. [CRC32] = {"CRC32", 4},
  75. [ADLER32] = {"adler32", 4},
  76. };
  77. const char *av_hash_names(int i)
  78. {
  79. if (i < 0 || i >= NUM_HASHES) return NULL;
  80. return hashdesc[i].name;
  81. }
  82. const char *av_hash_get_name(const AVHashContext *ctx)
  83. {
  84. return hashdesc[ctx->type].name;
  85. }
  86. int av_hash_get_size(const AVHashContext *ctx)
  87. {
  88. return hashdesc[ctx->type].size;
  89. }
  90. int av_hash_alloc(AVHashContext **ctx, const char *name)
  91. {
  92. AVHashContext *res;
  93. int i;
  94. *ctx = NULL;
  95. for (i = 0; i < NUM_HASHES; i++)
  96. if (av_strcasecmp(name, hashdesc[i].name) == 0)
  97. break;
  98. if (i >= NUM_HASHES) return AVERROR(EINVAL);
  99. res = av_mallocz(sizeof(*res));
  100. if (!res) return AVERROR(ENOMEM);
  101. res->type = i;
  102. switch (i) {
  103. case MD5: res->ctx = av_md5_alloc(); break;
  104. case MURMUR3: res->ctx = av_murmur3_alloc(); break;
  105. case RIPEMD128:
  106. case RIPEMD160:
  107. case RIPEMD256:
  108. case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
  109. case SHA160:
  110. case SHA224:
  111. case SHA256: res->ctx = av_sha_alloc(); break;
  112. case SHA512_224:
  113. case SHA512_256:
  114. case SHA384:
  115. case SHA512: res->ctx = av_sha512_alloc(); break;
  116. case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
  117. case ADLER32: break;
  118. }
  119. if (i != ADLER32 && i != CRC32 && !res->ctx) {
  120. av_free(res);
  121. return AVERROR(ENOMEM);
  122. }
  123. *ctx = res;
  124. return 0;
  125. }
  126. void av_hash_init(AVHashContext *ctx)
  127. {
  128. switch (ctx->type) {
  129. case MD5: av_md5_init(ctx->ctx); break;
  130. case MURMUR3: av_murmur3_init(ctx->ctx); break;
  131. case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
  132. case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
  133. case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
  134. case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
  135. case SHA160: av_sha_init(ctx->ctx, 160); break;
  136. case SHA224: av_sha_init(ctx->ctx, 224); break;
  137. case SHA256: av_sha_init(ctx->ctx, 256); break;
  138. case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
  139. case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
  140. case SHA384: av_sha512_init(ctx->ctx, 384); break;
  141. case SHA512: av_sha512_init(ctx->ctx, 512); break;
  142. case CRC32: ctx->crc = UINT32_MAX; break;
  143. case ADLER32: ctx->crc = 1; break;
  144. }
  145. }
  146. void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
  147. {
  148. switch (ctx->type) {
  149. case MD5: av_md5_update(ctx->ctx, src, len); break;
  150. case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
  151. case RIPEMD128:
  152. case RIPEMD160:
  153. case RIPEMD256:
  154. case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
  155. case SHA160:
  156. case SHA224:
  157. case SHA256: av_sha_update(ctx->ctx, src, len); break;
  158. case SHA512_224:
  159. case SHA512_256:
  160. case SHA384:
  161. case SHA512: av_sha512_update(ctx->ctx, src, len); break;
  162. case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
  163. case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
  164. }
  165. }
  166. void av_hash_final(AVHashContext *ctx, uint8_t *dst)
  167. {
  168. switch (ctx->type) {
  169. case MD5: av_md5_final(ctx->ctx, dst); break;
  170. case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
  171. case RIPEMD128:
  172. case RIPEMD160:
  173. case RIPEMD256:
  174. case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
  175. case SHA160:
  176. case SHA224:
  177. case SHA256: av_sha_final(ctx->ctx, dst); break;
  178. case SHA512_224:
  179. case SHA512_256:
  180. case SHA384:
  181. case SHA512: av_sha512_final(ctx->ctx, dst); break;
  182. case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
  183. case ADLER32: AV_WB32(dst, ctx->crc); break;
  184. }
  185. }
  186. void av_hash_freep(AVHashContext **ctx)
  187. {
  188. if (*ctx)
  189. av_freep(&(*ctx)->ctx);
  190. av_freep(ctx);
  191. }