Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <stdint.h> |
19 | | |
20 | | #include <libavutil/hash.h> |
21 | | |
22 | | #include "common/common.h" |
23 | | #include "misc/hash.h" |
24 | | |
25 | | bstr mp_hash_to_bstr(void *talloc_ctx, const uint8_t *data, size_t len, const char *algorithm) |
26 | 509k | { |
27 | 509k | struct AVHashContext *ctx = NULL; |
28 | 509k | mp_require(av_hash_alloc(&ctx, algorithm) == 0); |
29 | 509k | int size = av_hash_get_size(ctx); |
30 | 509k | uint8_t hash[AV_HASH_MAX_SIZE] = {0}; |
31 | | |
32 | 509k | av_hash_init(ctx); |
33 | 509k | av_hash_update(ctx, data, len); |
34 | 509k | av_hash_final(ctx, hash); |
35 | 509k | av_hash_freep(&ctx); |
36 | | |
37 | 509k | bstr ret = {0}; |
38 | 8.66M | for (int n = 0; n < size; n++) |
39 | 8.15M | bstr_xappend_asprintf(talloc_ctx, &ret, "%02X", hash[n]); |
40 | 509k | return ret; |
41 | 509k | } |