deno.land / std@0.157.0 / crypto / _wasm_crypto / src / lib.rs

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.use derive_more::{From, Into};use js_sys::Uint8Array;use wasm_bindgen::prelude::*;
mod digest;
/// Returns the digest of the given `data` using the given hash `algorithm`.////// `length` will usually be left `undefined` to use the default length for/// the algorithm. For algorithms with variable-length output, it can be used/// to specify a non-negative integer number of bytes.////// An error will be thrown if `algorithm` is not a supported hash algorithm or/// `length` is not a supported length for the algorithm.#[wasm_bindgen]pub fn digest( algorithm: String, data: js_sys::Uint8Array, length: Option<usize>,) -> Result<Box<[u8]>, JsValue> { let mut context = DigestContext::new(algorithm)?; context.update(data)?; context.digest_and_drop(length)}
/// A context for incrementally computing a digest using a given hash algorithm.#[wasm_bindgen]#[derive(Clone, Into, From)]pub struct DigestContext(digest::Context);
#[wasm_bindgen]impl DigestContext { /// Creates a new context incrementally computing a digest using the given /// hash algorithm. /// /// An error will be thrown if `algorithm` is not a supported hash algorithm. #[wasm_bindgen(constructor)] pub fn new(algorithm: String) -> Result<DigestContext, JsValue> { Ok( digest::Context::new(&algorithm) .map_err(|message| JsValue::from(js_sys::TypeError::new(message)))? .into(), ) }
/// Update the digest's internal state with the additional input `data`. /// /// If the `data` array view is large, it will be split into subarrays (via /// JavaScript bindings) which will be processed sequentially in order to /// limit the amount of memory that needs to be allocated in the Wasm heap. #[wasm_bindgen] pub fn update(&mut self, data: js_sys::Uint8Array) -> Result<(), JsValue> { // Every method call on `data` has to go through the JavaScript bindings, so // try to minimize them. Splitting on the JavaScript side could be more // efficient, but this is called from multiple places in JavaScript so it // seems simpler to keep it here for now.
let length = data.byte_length(); let base_offset = data.byte_offset(); let buffer = data.buffer();
let chunk_size: u32 = 65_536;
if length <= chunk_size { let chunk = data.to_vec(); self.0.update(&chunk); } else { let mut offset = 0; while offset < length { let chunk = Uint8Array::new_with_byte_offset_and_length( &buffer, base_offset + offset, chunk_size.min(length - offset), ) .to_vec(); self.0.update(&chunk); offset += chunk_size; } }
Ok(()) }
/// Returns the digest of the input data so far. This may be called repeatedly /// without side effects. /// /// `length` will usually be left `undefined` to use the default length for /// the algorithm. For algorithms with variable-length output, it can be used /// to specify a non-negative integer number of bytes. /// /// An error will be thrown if `algorithm` is not a supported hash algorithm or /// `length` is not a supported length for the algorithm. #[wasm_bindgen] pub fn digest(&self, length: Option<usize>) -> Result<Box<[u8]>, JsValue> { self .0 .digest(length) .map_err(|message| JsValue::from(js_sys::TypeError::new(message))) }
/// Returns the digest of the input data so far, and resets this context to /// its initial state, as though it has not yet been provided with any input /// data. (It will still use the same algorithm.) /// /// `length` will usually be left `undefined` to use the default length for /// the algorithm. For algorithms with variable-length output, it can be used /// to specify a non-negative integer number of bytes. /// /// An error will be thrown if `algorithm` is not a supported hash algorithm or /// `length` is not a supported length for the algorithm. #[wasm_bindgen(js_name=digestAndReset)] pub fn digest_and_reset( &mut self, length: Option<usize>, ) -> Result<Box<[u8]>, JsValue> { self .0 .digest_and_reset(length) .map_err(|message| JsValue::from(js_sys::TypeError::new(message))) }
/// Returns the digest of the input data so far, and then drops the context /// from memory on the Wasm side. This context must no longer be used, and any /// further method calls will result in null pointer errors being thrown. /// https://github.com/rustwasm/wasm-bindgen/blob/bf39cfd8/crates/backend/src/codegen.rs#L186 /// /// `length` will usually be left `undefined` to use the default length for /// the algorithm. For algorithms with variable-length output, it can be used /// to specify a non-negative integer number of bytes. /// /// An error will be thrown if `algorithm` is not a supported hash algorithm or /// `length` is not a supported length for the algorithm. #[wasm_bindgen(js_name=digestAndDrop)] pub fn digest_and_drop( mut self, length: Option<usize>, ) -> Result<Box<[u8]>, JsValue> { self .0 .digest_and_reset(length) .map_err(|message| JsValue::from(js_sys::TypeError::new(message))) }
/// Resets this context to its initial state, as though it has not yet been /// provided with any input data. (It will still use the same algorithm.) #[wasm_bindgen] pub fn reset(&mut self) -> Result<(), JsValue> { self.0.reset();
Ok(()) }
/// Returns a new `DigestContext` that is a copy of this one, i.e., using the /// same algorithm and with a copy of the same internal state. /// /// This may be a more efficient option for computing multiple digests that /// start with a common prefix. #[wasm_bindgen] #[allow(clippy::should_implement_trait)] pub fn clone(&self) -> DigestContext { Clone::clone(self) }}
std

Version Info

Tagged at
a year ago