2
0

hash.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. static const 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. #if FF_API_CRYPTO_SIZE_T
  148. void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
  149. #else
  150. void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
  151. #endif
  152. {
  153. switch (ctx->type) {
  154. case MD5: av_md5_update(ctx->ctx, src, len); break;
  155. case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
  156. case RIPEMD128:
  157. case RIPEMD160:
  158. case RIPEMD256:
  159. case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
  160. case SHA160:
  161. case SHA224:
  162. case SHA256: av_sha_update(ctx->ctx, src, len); break;
  163. case SHA512_224:
  164. case SHA512_256:
  165. case SHA384:
  166. case SHA512: av_sha512_update(ctx->ctx, src, len); break;
  167. case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
  168. case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
  169. }
  170. }
  171. void av_hash_final(AVHashContext *ctx, uint8_t *dst)
  172. {
  173. switch (ctx->type) {
  174. case MD5: av_md5_final(ctx->ctx, dst); break;
  175. case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
  176. case RIPEMD128:
  177. case RIPEMD160:
  178. case RIPEMD256:
  179. case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
  180. case SHA160:
  181. case SHA224:
  182. case SHA256: av_sha_final(ctx->ctx, dst); break;
  183. case SHA512_224:
  184. case SHA512_256:
  185. case SHA384:
  186. case SHA512: av_sha512_final(ctx->ctx, dst); break;
  187. case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
  188. case ADLER32: AV_WB32(dst, ctx->crc); break;
  189. }
  190. }
  191. void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
  192. {
  193. uint8_t buf[AV_HASH_MAX_SIZE];
  194. unsigned rsize = av_hash_get_size(ctx);
  195. av_hash_final(ctx, buf);
  196. memcpy(dst, buf, FFMIN(size, rsize));
  197. if (size > rsize)
  198. memset(dst + rsize, 0, size - rsize);
  199. }
  200. void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
  201. {
  202. uint8_t buf[AV_HASH_MAX_SIZE];
  203. unsigned rsize = av_hash_get_size(ctx), i;
  204. av_hash_final(ctx, buf);
  205. for (i = 0; i < FFMIN(rsize, size / 2); i++)
  206. snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]);
  207. }
  208. void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
  209. {
  210. uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)];
  211. unsigned rsize = av_hash_get_size(ctx), osize;
  212. av_hash_final(ctx, buf);
  213. av_base64_encode(b64, sizeof(b64), buf, rsize);
  214. osize = AV_BASE64_SIZE(rsize);
  215. memcpy(dst, b64, FFMIN(osize, size));
  216. if (size < osize)
  217. dst[size - 1] = 0;
  218. }
  219. void av_hash_freep(AVHashContext **ctx)
  220. {
  221. if (*ctx)
  222. av_freep(&(*ctx)->ctx);
  223. av_freep(ctx);
  224. }