File: hsh.c

package info (click to toggle)
jose 14-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,884 kB
  • sloc: ansic: 10,235; javascript: 987; sh: 586; makefile: 9
file content (79 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include "misc.h"
#include "hsh.h"

#include <jose/b64.h>
#include "hooks.h"

json_t *
hsh(jose_cfg_t *cfg, const char *alg, const void *data, size_t dlen)
{
    jose_io_auto_t *_hsh = NULL;
    jose_io_auto_t *enc = NULL;
    jose_io_auto_t *buf = NULL;
    char b[1024] = {};
    size_t l = sizeof(b);

    buf = jose_io_buffer(cfg, b, &l);
    enc = jose_b64_enc_io(buf);
    _hsh = hsh_io(cfg, alg, enc);
    if (!buf || !enc || !_hsh || !_hsh->feed(_hsh, data, dlen) || !_hsh->done(_hsh))
        return NULL;

    return json_stringn(b, l);
}

jose_io_t *
hsh_io(jose_cfg_t *cfg, const char *alg, jose_io_t *next)
{
    const jose_hook_alg_t *a = NULL;

    a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, alg);
    if (!a)
        return NULL;

    return a->hash.hsh(a, cfg, next);
}

size_t
hsh_buf(jose_cfg_t *cfg, const char *alg,
        const void *data, size_t dlen, void *hash, size_t hlen)
{
    const jose_hook_alg_t *a = NULL;
    jose_io_auto_t *_hsh = NULL;
    jose_io_auto_t *buf = NULL;

    a = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, alg);
    if (!a)
        return SIZE_MAX;

    if (!hash || hlen == 0)
        return a->hash.size;

    if (hlen < a->hash.size)
        return SIZE_MAX;

    buf = jose_io_buffer(cfg, hash, &hlen);
    _hsh = a->hash.hsh(a, cfg, buf);
    if (!buf || !_hsh || !_hsh->feed(_hsh, data, dlen) || !_hsh->done(_hsh))
        return SIZE_MAX;

    return hlen;
}