forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.c
More file actions
80 lines (67 loc) · 2.87 KB
/
Copy pathHash.c
File metadata and controls
80 lines (67 loc) · 2.87 KB
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
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-bindings/hashlib/Hash.h"
#include "py/obj.h"
#include "py/objproperty.h"
#include "py/objstr.h"
#include "py/runtime.h"
//| class Hash:
//| """In progress hash algorithm. This object is always created by a `hashlib.new()`. It has no
//| user-visible constructor."""
//|
//| digest_size: int
//| """Digest size in bytes"""
//|
static mp_obj_t hashlib_hash_digest_size_get(mp_obj_t self_in) {
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_hashlib_hash_get_digest_size(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(hashlib_hash_digest_size_get_obj, hashlib_hash_digest_size_get);
MP_PROPERTY_GETTER(hashlib_hash_digest_size_obj, (mp_obj_t)&hashlib_hash_digest_size_get_obj);
//| def update(self, data: ReadableBuffer) -> None:
//| """Update the hash with the given bytes.
//|
//| :param ~circuitpython_typing.ReadableBuffer data: Update the hash from data in this buffer
//| """
//| ...
//|
mp_obj_t hashlib_hash_update(mp_obj_t self_in, mp_obj_t buf_in) {
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
common_hal_hashlib_hash_update(self, bufinfo.buf, bufinfo.len);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(hashlib_hash_update_obj, hashlib_hash_update);
//| def digest(self) -> bytes:
//| """Returns the current digest as bytes() with a length of `hashlib.Hash.digest_size`."""
//| ...
//|
//|
static mp_obj_t hashlib_hash_digest(mp_obj_t self_in) {
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
size_t size = common_hal_hashlib_hash_get_digest_size(self);
mp_obj_t obj = mp_obj_new_bytes_of_zeros(size);
mp_obj_str_t *o = MP_OBJ_TO_PTR(obj);
common_hal_hashlib_hash_digest(self, (uint8_t *)o->data, size);
return obj;
}
static MP_DEFINE_CONST_FUN_OBJ_1(hashlib_hash_digest_obj, hashlib_hash_digest);
static const mp_rom_map_elem_t hashlib_hash_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_PTR(&hashlib_hash_digest_size_obj) },
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_hash_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_hash_digest_obj) },
};
static MP_DEFINE_CONST_DICT(hashlib_hash_locals_dict, hashlib_hash_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
hashlib_hash_type,
MP_QSTR_Hash,
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
locals_dict, &hashlib_hash_locals_dict
);