FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hash.c
Go to the documentation of this file.
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 
23 #include "adler32.h"
24 #include "crc.h"
25 #include "md5.h"
26 #include "murmur3.h"
27 #include "ripemd.h"
28 #include "sha.h"
29 #include "sha512.h"
30 
31 #include "avstring.h"
32 #include "base64.h"
33 #include "error.h"
34 #include "intreadwrite.h"
35 #include "mem.h"
36 
37 enum hashtype {
38  MD5,
54 };
55 
56 typedef struct AVHashContext {
57  void *ctx;
58  enum hashtype type;
59  const AVCRC *crctab;
60  uint32_t crc;
62 
63 static const struct {
64  const char *name;
65  int size;
66 } hashdesc[] = {
67  [MD5] = {"MD5", 16},
68  [MURMUR3] = {"murmur3", 16},
69  [RIPEMD128] = {"RIPEMD128", 16},
70  [RIPEMD160] = {"RIPEMD160", 20},
71  [RIPEMD256] = {"RIPEMD256", 32},
72  [RIPEMD320] = {"RIPEMD320", 40},
73  [SHA160] = {"SHA160", 20},
74  [SHA224] = {"SHA224", 28},
75  [SHA256] = {"SHA256", 32},
76  [SHA512_224] = {"SHA512/224", 28},
77  [SHA512_256] = {"SHA512/256", 32},
78  [SHA384] = {"SHA384", 48},
79  [SHA512] = {"SHA512", 64},
80  [CRC32] = {"CRC32", 4},
81  [ADLER32] = {"adler32", 4},
82 };
83 
84 const char *av_hash_names(int i)
85 {
86  if (i < 0 || i >= NUM_HASHES) return NULL;
87  return hashdesc[i].name;
88 }
89 
90 const char *av_hash_get_name(const AVHashContext *ctx)
91 {
92  return hashdesc[ctx->type].name;
93 }
94 
96 {
97  return hashdesc[ctx->type].size;
98 }
99 
100 int av_hash_alloc(AVHashContext **ctx, const char *name)
101 {
102  AVHashContext *res;
103  int i;
104  *ctx = NULL;
105  for (i = 0; i < NUM_HASHES; i++)
106  if (av_strcasecmp(name, hashdesc[i].name) == 0)
107  break;
108  if (i >= NUM_HASHES) return AVERROR(EINVAL);
109  res = av_mallocz(sizeof(*res));
110  if (!res) return AVERROR(ENOMEM);
111  res->type = i;
112  switch (i) {
113  case MD5: res->ctx = av_md5_alloc(); break;
114  case MURMUR3: res->ctx = av_murmur3_alloc(); break;
115  case RIPEMD128:
116  case RIPEMD160:
117  case RIPEMD256:
118  case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
119  case SHA160:
120  case SHA224:
121  case SHA256: res->ctx = av_sha_alloc(); break;
122  case SHA512_224:
123  case SHA512_256:
124  case SHA384:
125  case SHA512: res->ctx = av_sha512_alloc(); break;
126  case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
127  case ADLER32: break;
128  }
129  if (i != ADLER32 && i != CRC32 && !res->ctx) {
130  av_free(res);
131  return AVERROR(ENOMEM);
132  }
133  *ctx = res;
134  return 0;
135 }
136 
138 {
139  switch (ctx->type) {
140  case MD5: av_md5_init(ctx->ctx); break;
141  case MURMUR3: av_murmur3_init(ctx->ctx); break;
142  case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
143  case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
144  case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
145  case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
146  case SHA160: av_sha_init(ctx->ctx, 160); break;
147  case SHA224: av_sha_init(ctx->ctx, 224); break;
148  case SHA256: av_sha_init(ctx->ctx, 256); break;
149  case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
150  case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
151  case SHA384: av_sha512_init(ctx->ctx, 384); break;
152  case SHA512: av_sha512_init(ctx->ctx, 512); break;
153  case CRC32: ctx->crc = UINT32_MAX; break;
154  case ADLER32: ctx->crc = 1; break;
155  }
156 }
157 
158 #if FF_API_CRYPTO_SIZE_T
160 #else
161 void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
162 #endif
163 {
164  switch (ctx->type) {
165  case MD5: av_md5_update(ctx->ctx, src, len); break;
166  case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
167  case RIPEMD128:
168  case RIPEMD160:
169  case RIPEMD256:
170  case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
171  case SHA160:
172  case SHA224:
173  case SHA256: av_sha_update(ctx->ctx, src, len); break;
174  case SHA512_224:
175  case SHA512_256:
176  case SHA384:
177  case SHA512: av_sha512_update(ctx->ctx, src, len); break;
178  case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
179  case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
180  }
181 }
182 
184 {
185  switch (ctx->type) {
186  case MD5: av_md5_final(ctx->ctx, dst); break;
187  case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
188  case RIPEMD128:
189  case RIPEMD160:
190  case RIPEMD256:
191  case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
192  case SHA160:
193  case SHA224:
194  case SHA256: av_sha_final(ctx->ctx, dst); break;
195  case SHA512_224:
196  case SHA512_256:
197  case SHA384:
198  case SHA512: av_sha512_final(ctx->ctx, dst); break;
199  case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
200  case ADLER32: AV_WB32(dst, ctx->crc); break;
201  }
202 }
203 
204 void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
205 {
207  unsigned rsize = av_hash_get_size(ctx);
208 
209  av_hash_final(ctx, buf);
210  memcpy(dst, buf, FFMIN(size, rsize));
211  if (size > rsize)
212  memset(dst + rsize, 0, size - rsize);
213 }
214 
215 void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
216 {
218  unsigned rsize = av_hash_get_size(ctx), i;
219 
220  av_hash_final(ctx, buf);
221  for (i = 0; i < FFMIN(rsize, size / 2); i++)
222  snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]);
223 }
224 
225 void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
226 {
228  unsigned rsize = av_hash_get_size(ctx), osize;
229 
230  av_hash_final(ctx, buf);
231  av_base64_encode(b64, sizeof(b64), buf, rsize);
232  osize = AV_BASE64_SIZE(rsize);
233  memcpy(dst, b64, FFMIN(osize, size));
234  if (size < osize)
235  dst[size - 1] = 0;
236 }
237 
239 {
240  if (*ctx)
241  av_freep(&(*ctx)->ctx);
242  av_freep(ctx);
243 }
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:345
#define NULL
Definition: coverity.c:32
void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the Base64 representation of the actual hash value as a string...
Definition: hash.c:225
void av_ripemd_update(AVRIPEMD *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: ripemd.c:514
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
Memory handling functions.
Public header for RIPEMD hash function implementation.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
Definition: hash.c:49
Definition: hash.c:41
Public header for MurmurHash3 hash function implementation.
#define src
Definition: vp8dsp.c:254
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:154
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
Definition: hash.h:157
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:48
void av_ripemd_final(AVRIPEMD *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: ripemd.c:544
const char * name
Definition: hash.c:64
uint8_t
Generic hashing API.
Definition: hash.c:45
Definition: hash.c:39
unsigned long av_adler32_update(unsigned long adler, const uint8_t *buf, unsigned int len)
Calculate the Adler32 checksum of a buffer.
Definition: adler32.c:44
Public header for CRC hash function implementation.
Definition: hash.c:38
uint32_t crc
Definition: hash.c:60
struct AVSHA512 * av_sha512_alloc(void)
Allocate an AVSHA512 context.
Definition: sha512.c:43
void av_murmur3_init(AVMurMur3 *c)
Initialize or reinitialize an AVMurMur3 hash context.
Definition: murmur3.c:43
ptrdiff_t size
Definition: opengl_enc.c:101
void av_hash_init(AVHashContext *ctx)
Initialize or reset a hash context.
Definition: hash.c:137
error code definitions
#define AVERROR(e)
Definition: error.h:43
Definition: hash.c:42
void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the actual hash value in a buffer.
Definition: hash.c:204
void av_hash_final(AVHashContext *ctx, uint8_t *dst)
Finalize a hash context and compute the actual hash value.
Definition: hash.c:183
int av_hash_alloc(AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
Definition: hash.c:100
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:138
struct AVRIPEMD * av_ripemd_alloc(void)
Allocate an AVRIPEMD context.
Definition: ripemd.c:43
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition: base64.h:66
#define FFMIN(a, b)
Definition: common.h:96
const char * av_hash_names(int i)
Get the names of available hash algorithms.
Definition: hash.c:84
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:315
Definition: hash.c:40
AVFormatContext * ctx
Definition: movenc.c:48
const char * av_hash_get_name(const AVHashContext *ctx)
Definition: hash.c:90
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
Definition: hash.c:51
Public header for Adler-32 hash function implementation.
Definition: hash.c:44
int av_hash_get_size(const AVHashContext *ctx)
Definition: hash.c:95
void av_sha512_update(AVSHA512 *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha512.c:243
Public header for SHA-1 & SHA-256 hash function implementations.
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
Update a hash context with additional data.
Definition: hash.c:159
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:143
void * buf
Definition: avisynth_c.h:690
av_cold int av_sha512_init(AVSHA512 *ctx, int bits)
Initialize SHA-2 512 hashing.
Definition: sha512.c:191
void av_hash_freep(AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
Definition: hash.c:238
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the hexadecimal representation of the actual hash value as a string...
Definition: hash.c:215
#define snprintf
Definition: snprintf.h:34
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:192
enum hashtype type
Definition: hash.c:58
Definition: hash.c:43
Definition: hash.c:50
Definition: hash.c:46
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
av_cold int av_ripemd_init(AVRIPEMD *ctx, int bits)
Initialize RIPEMD hashing.
Definition: ripemd.c:463
Public header for SHA-512 implementation.
void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, int len)
Update hash context with new data.
Definition: murmur3.c:93
const AVCRC * crctab
Definition: hash.c:59
static const struct @280 hashdesc[]
int size
Definition: hash.c:65
void * ctx
Definition: hash.c:57
#define av_free(p)
hashtype
Definition: hash.c:37
int len
void av_sha512_final(AVSHA512 *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha512.c:273
Public header for MD5 hash function implementation.
#define av_freep(p)
AVMurMur3 * av_murmur3_alloc(void)
Allocate an AVMurMur3 hash context.
Definition: murmur3.c:32
void av_murmur3_final(AVMurMur3 *c, uint8_t dst[16])
Finish hashing and output digest value.
Definition: murmur3.c:145
uint32_t AVCRC
Definition: crc.h:47
Definition: hash.c:52
const char * name
Definition: opengl_enc.c:103