2 * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
4 * This file is part of FFmpeg.
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.
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.
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
34 #include "intreadwrite.h"
56 typedef struct AVHashContext {
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},
84 const char *av_hash_names(int i)
86 if (i < 0 || i >= NUM_HASHES) return NULL;
87 return hashdesc[i].name;
90 const char *av_hash_get_name(const AVHashContext *ctx)
92 return hashdesc[ctx->type].name;
95 int av_hash_get_size(const AVHashContext *ctx)
97 return hashdesc[ctx->type].size;
100 int av_hash_alloc(AVHashContext **ctx, const char *name)
105 for (i = 0; i < NUM_HASHES; i++)
106 if (av_strcasecmp(name, hashdesc[i].name) == 0)
108 if (i >= NUM_HASHES) return AVERROR(EINVAL);
109 res = av_mallocz(sizeof(*res));
110 if (!res) return AVERROR(ENOMEM);
113 case MD5: res->ctx = av_md5_alloc(); break;
114 case MURMUR3: res->ctx = av_murmur3_alloc(); break;
118 case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
121 case SHA256: res->ctx = av_sha_alloc(); break;
125 case SHA512: res->ctx = av_sha512_alloc(); break;
126 case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
129 if (i != ADLER32 && i != CRC32 && !res->ctx) {
131 return AVERROR(ENOMEM);
137 void av_hash_init(AVHashContext *ctx)
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;
158 #if FF_API_CRYPTO_SIZE_T
159 void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
161 void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
165 case MD5: av_md5_update(ctx->ctx, src, len); break;
166 case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
170 case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
173 case SHA256: av_sha_update(ctx->ctx, src, len); break;
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;
183 void av_hash_final(AVHashContext *ctx, uint8_t *dst)
186 case MD5: av_md5_final(ctx->ctx, dst); break;
187 case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
191 case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
194 case SHA256: av_sha_final(ctx->ctx, dst); break;
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;
204 void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
206 uint8_t buf[AV_HASH_MAX_SIZE];
207 unsigned rsize = av_hash_get_size(ctx);
209 av_hash_final(ctx, buf);
210 memcpy(dst, buf, FFMIN(size, rsize));
212 memset(dst + rsize, 0, size - rsize);
215 void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
217 uint8_t buf[AV_HASH_MAX_SIZE];
218 unsigned rsize = av_hash_get_size(ctx), i;
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]);
225 void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
227 uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)];
228 unsigned rsize = av_hash_get_size(ctx), osize;
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));
238 void av_hash_freep(AVHashContext **ctx)
241 av_freep(&(*ctx)->ctx);