File: hash.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (139 lines) | stat: -rw-r--r-- 4,969 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef AWS_CAL_HASH_H_
#define AWS_CAL_HASH_H_
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/cal/exports.h>

#include <aws/common/byte_buf.h>
#include <aws/common/common.h>

AWS_PUSH_SANE_WARNING_LEVEL

#define AWS_SHA256_LEN 32
#define AWS_SHA1_LEN 20
#define AWS_MD5_LEN 16

struct aws_hash;

struct aws_hash_vtable {
    const char *alg_name;
    const char *provider;
    void (*destroy)(struct aws_hash *hash);
    int (*update)(struct aws_hash *hash, const struct aws_byte_cursor *buf);
    int (*finalize)(struct aws_hash *hash, struct aws_byte_buf *out);
};

struct aws_hash {
    struct aws_allocator *allocator;
    struct aws_hash_vtable *vtable;
    size_t digest_size;
    bool good;
    void *impl;
};

typedef struct aws_hash *(aws_hash_new_fn)(struct aws_allocator *allocator);

AWS_EXTERN_C_BEGIN
/**
 * Allocates and initializes a sha256 hash instance.
 */
AWS_CAL_API struct aws_hash *aws_sha256_new(struct aws_allocator *allocator);
/**
 * Allocates and initializes a sha1 hash instance.
 */
AWS_CAL_API struct aws_hash *aws_sha1_new(struct aws_allocator *allocator);
/**
 * Allocates and initializes an md5 hash instance.
 */
AWS_CAL_API struct aws_hash *aws_md5_new(struct aws_allocator *allocator);

/**
 * Cleans up and deallocates hash.
 */
AWS_CAL_API void aws_hash_destroy(struct aws_hash *hash);
/**
 * Updates the running hash with to_hash. this can be called multiple times.
 */
AWS_CAL_API int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash);
/**
 * Completes the hash computation and writes the final digest to output.
 * Allocation of output is the caller's responsibility. If you specify
 * truncate_to to something other than 0, the output will be truncated to that
 * number of bytes. For example, if you want a SHA256 digest as the first 16
 * bytes, set truncate_to to 16. If you want the full digest size, just set this
 * to 0.
 */
AWS_CAL_API int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *output, size_t truncate_to);

/**
 * Computes the md5 hash over input and writes the digest output to 'output'.
 * Use this if you don't need to stream the data you're hashing and you can load
 * the entire input to hash into memory.
 */
AWS_CAL_API int aws_md5_compute(
    struct aws_allocator *allocator,
    const struct aws_byte_cursor *input,
    struct aws_byte_buf *output,
    size_t truncate_to);

/**
 * Computes the sha256 hash over input and writes the digest output to 'output'.
 * Use this if you don't need to stream the data you're hashing and you can load
 * the entire input to hash into memory. If you specify truncate_to to something
 * other than 0, the output will be truncated to that number of bytes. For
 * example, if you want a SHA256 digest as the first 16 bytes, set truncate_to
 * to 16. If you want the full digest size, just set this to 0.
 */
AWS_CAL_API int aws_sha256_compute(
    struct aws_allocator *allocator,
    const struct aws_byte_cursor *input,
    struct aws_byte_buf *output,
    size_t truncate_to);

/**
 * Computes the sha1 hash over input and writes the digest output to 'output'.
 * Use this if you don't need to stream the data you're hashing and you can load
 * the entire input to hash into memory. If you specify truncate_to to something
 * other than 0, the output will be truncated to that number of bytes. For
 * example, if you want a SHA1 digest as the first 16 bytes, set truncate_to
 * to 16. If you want the full digest size, just set this to 0.
 */
AWS_CAL_API int aws_sha1_compute(
    struct aws_allocator *allocator,
    const struct aws_byte_cursor *input,
    struct aws_byte_buf *output,
    size_t truncate_to);

/**
 * Set the implementation of md5 to use. If you compiled without BYO_CRYPTO,
 * you do not need to call this. However, if use this, we will honor it,
 * regardless of compile options. This may be useful for testing purposes. If
 * you did set BYO_CRYPTO, and you do not call this function you will
 * segfault.
 */
AWS_CAL_API void aws_set_md5_new_fn(aws_hash_new_fn *fn);

/**
 * Set the implementation of sha256 to use. If you compiled without
 * BYO_CRYPTO, you do not need to call this. However, if use this, we will
 * honor it, regardless of compile options. This may be useful for testing
 * purposes. If you did set BYO_CRYPTO, and you do not call this function
 * you will segfault.
 */
AWS_CAL_API void aws_set_sha256_new_fn(aws_hash_new_fn *fn);

/**
 * Set the implementation of sha1 to use. If you compiled without
 * BYO_CRYPTO, you do not need to call this. However, if use this, we will
 * honor it, regardless of compile options. This may be useful for testing
 * purposes. If you did set BYO_CRYPTO, and you do not call this function
 * you will segfault.
 */
AWS_CAL_API void aws_set_sha1_new_fn(aws_hash_new_fn *fn);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

#endif /* AWS_CAL_HASH_H_ */