hash.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "base64.h"
  31. #include "error.h"
  32. #include "intreadwrite.h"
  33. #include "mem.h"
  34. enum hashtype {
  35. MD5,
  36. MURMUR3,
  37. RIPEMD128,
  38. RIPEMD160,
  39. RIPEMD256,
  40. RIPEMD320,
  41. SHA160,
  42. SHA224,
  43. SHA256,
  44. SHA512_224,
  45. SHA512_256,
  46. SHA384,
  47. SHA512,
  48. CRC32,
  49. ADLER32,
  50. NUM_HASHES
  51. };
  52. typedef struct AVHashContext {
  53. void *ctx;
  54. enum hashtype type;
  55. const AVCRC *crctab;
  56. uint32_t crc;
  57. } AVHashContext;
  58. struct {
  59. const char *name;
  60. int size;
  61. } hashdesc[] = {
  62. [MD5] = {"MD5", 16},
  63. [MURMUR3] = {"murmur3", 16},
  64. [RIPEMD128] = {"RIPEMD128", 16},
  65. [RIPEMD160] = {"RIPEMD160", 20},
  66. [RIPEMD256] = {"RIPEMD256", 32},
  67. [RIPEMD320] = {"RIPEMD320", 40},
  68. [SHA160] = {"SHA160", 20},
  69. [SHA224] = {"SHA224", 28},
  70. [SHA256] = {"SHA256", 32},
  71. [SHA512_224] = {"SHA512/224", 28},
  72. [SHA512_256] = {"SHA512/256", 32},
  73. [SHA384] = {"SHA384", 48},
  74. [SHA512] = {"SHA512", 64},
  75. [CRC32] = {"CRC32", 4},
  76. [ADLER32] = {"adler32", 4},
  77. };
  78. const char *av_hash_names(int i)
  79. {
  80. if (i < 0 || i >= NUM_HASHES) return NULL;
  81. return hashdesc[i].name;
  82. }
  83. const char *av_hash_get_name(const AVHashContext *ctx)
  84. {
  85. return hashdesc[ctx->type].name;
  86. }
  87. int av_hash_get_size(const AVHashContext *ctx)
  88. {
  89. return hashdesc[ctx->type].size;
  90. }
  91. int av_hash_alloc(AVHashContext **ctx, const char *name)
  92. {
  93. AVHashContext *res;
  94. int i;
  95. *ctx = NULL;
  96. for (i = 0; i < NUM_HASHES; i++)
  97. if (av_strcasecmp(name, hashdesc[i].name) == 0)
  98. break;
  99. if (i >= NUM_HASHES) return AVERROR(EINVAL);
  100. res = av_mallocz(sizeof(*res));
  101. if (!res) return AVERROR(ENOMEM);
  102. res->type = i;
  103. switch (i) {
  104. case MD5: res->ctx = av_md5_alloc(); break;
  105. case MURMUR3: res->ctx = av_murmur3_alloc(); break;
  106. case RIPEMD128:
  107. case RIPEMD160:
  108. case RIPEMD256:
  109. case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
  110. case SHA160:
  111. case SHA224:
  112. case SHA256: res->ctx = av_sha_alloc(); break;
  113. case SHA512_224:
  114. case SHA512_256:
  115. case SHA384:
  116. case SHA512: res->ctx = av_sha512_alloc(); break;
  117. case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
  118. case ADLER32: break;
  119. }
  120. if (i != ADLER32 && i != CRC32 && !res->ctx) {
  121. av_free(res);
  122. return AVERROR(ENOMEM);
  123. }
  124. *ctx = res;
  125. return 0;
  126. }
  127. void av_hash_init(AVHashContext *ctx)
  128. {
  129. switch (ctx->type) {
  130. case MD5: av_md5_init(ctx->ctx); break;
  131. case MURMUR3: av_murmur3_init(ctx->ctx); break;
  132. case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
  133. case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
  134. case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
  135. case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
  136. case SHA160: av_sha_init(ctx->ctx, 160); break;
  137. case SHA224: av_sha_init(ctx->ctx, 224); break;
  138. case SHA256: av_sha_init(ctx->ctx, 256); break;
  139. case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
  140. case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
  141. case SHA384: av_sha512_init(ctx->ctx, 384); break;
  142. case SHA512: av_sha512_init(ctx->ctx, 512); break;
  143. case CRC32: ctx->crc = UINT32_MAX; break;
  144. case ADLER32: ctx->crc = 1; break;
  145. }
  146. }
  147. void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
  148. {
  149. switch (ctx->type) {
  150. case MD5: av_md5_update(ctx->ctx, src, len); break;
  151. case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
  152. case RIPEMD128:
  153. case RIPEMD160:
  154. case RIPEMD256:
  155. case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
  156. case SHA160:
  157. case SHA224:
  158. case SHA256: av_sha_update(ctx->ctx, src, len); break;
  159. case SHA512_224:
  160. case SHA512_256:
  161. case SHA384:
  162. case SHA512: av_sha512_update(ctx->ctx, src, len); break;
  163. case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
  164. case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
  165. }
  166. }
  167. void av_hash_final(AVHashContext *ctx, uint8_t *dst)
  168. {
  169. switch (ctx->type) {
  170. case MD5: av_md5_final(ctx->ctx, dst); break;
  171. case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
  172. case RIPEMD128:
  173. case RIPEMD160:
  174. case RIPEMD256:
  175. case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
  176. case SHA160:
  177. case SHA224:
  178. case SHA256: av_sha_final(ctx->ctx, dst); break;
  179. case SHA512_224:
  180. case SHA512_256:
  181. case SHA384:
  182. case SHA512: av_sha512_final(ctx->ctx, dst); break;
  183. case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
  184. case ADLER32: AV_WB32(dst, ctx->crc); break;
  185. }
  186. }
  187. void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
  188. {
  189. uint8_t buf[AV_HASH_MAX_SIZE];
  190. unsigned rsize = av_hash_get_size(ctx);
  191. av_hash_final(ctx, buf);
  192. memcpy(dst, buf, FFMIN(size, rsize));
  193. if (size > rsize)
  194. memset(dst + rsize, 0, size - rsize);
  195. }
  196. void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
  197. {
  198. uint8_t buf[AV_HASH_MAX_SIZE];
  199. unsigned rsize = av_hash_get_size(ctx), i;
  200. av_hash_final(ctx, buf);
  201. for (i = 0; i < FFMIN(rsize, size / 2); i++)
  202. snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]);
  203. }
  204. void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
  205. {
  206. uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)];
  207. unsigned rsize = av_hash_get_size(ctx), osize;
  208. av_hash_final(ctx, buf);
  209. av_base64_encode(b64, sizeof(b64), buf, rsize);
  210. osize = AV_BASE64_SIZE(rsize);
  211. memcpy(dst, b64, FFMIN(osize, size));
  212. if (size < osize)
  213. dst[size - 1] = 0;
  214. }
  215. void av_hash_freep(AVHashContext **ctx)
  216. {
  217. if (*ctx)
  218. av_freep(&(*ctx)->ctx);
  219. av_freep(ctx);
  220. }