]>
git.proxmox.com Git - rustc.git/blob - vendor/openssl/src/sha.rs
1 //! The SHA family of hashes.
3 //! SHA, or Secure Hash Algorithms, are a family of cryptographic hashing algorithms published by
4 //! the National Institute of Standards and Technology (NIST). Hash algorithms such as those in
5 //! the SHA family are used to map data of an arbitrary size to a fixed-size string of bytes.
6 //! As cryptographic hashing algorithms, these mappings have the property of being irreversible.
7 //! This property makes hash algorithms like these excellent for uses such as verifying the
8 //! contents of a file- if you know the hash you expect beforehand, then you can verify that the
9 //! data you have is correct if it hashes to the same value.
13 //! When dealing with data that becomes available in chunks, such as while buffering data from IO,
14 //! you can create a hasher that you can repeatedly update to add bytes to.
19 //! let mut hasher = sha::Sha256::new();
21 //! hasher.update(b"Hello, ");
22 //! hasher.update(b"world");
24 //! let hash = hasher.finish();
25 //! println!("Hashed \"Hello, world\" to {}", hex::encode(hash));
28 //! On the other hand, if you already have access to all of the data you would like to hash, you
29 //! may prefer to use the slightly simpler method of simply calling the hash function corresponding
30 //! to the algorithm you want to use.
33 //! use openssl::sha::sha256;
35 //! let hash = sha256(b"your data or message");
36 //! println!("Hash = {}", hex::encode(hash));
40 use openssl_macros
::corresponds
;
41 use std
::mem
::MaybeUninit
;
43 /// Computes the SHA1 hash of some data.
47 /// SHA1 is known to be insecure - it should not be used unless required for
48 /// compatibility with existing systems.
51 pub fn sha1(data
: &[u8]) -> [u8; 20] {
53 let mut hash
= MaybeUninit
::<[u8; 20]>::uninit();
54 ffi
::SHA1(data
.as_ptr(), data
.len(), hash
.as_mut_ptr() as *mut _
);
59 /// Computes the SHA224 hash of some data.
60 #[corresponds(SHA224)]
62 pub fn sha224(data
: &[u8]) -> [u8; 28] {
64 let mut hash
= MaybeUninit
::<[u8; 28]>::uninit();
65 ffi
::SHA224(data
.as_ptr(), data
.len(), hash
.as_mut_ptr() as *mut _
);
70 /// Computes the SHA256 hash of some data.
71 #[corresponds(SHA256)]
73 pub fn sha256(data
: &[u8]) -> [u8; 32] {
75 let mut hash
= MaybeUninit
::<[u8; 32]>::uninit();
76 ffi
::SHA256(data
.as_ptr(), data
.len(), hash
.as_mut_ptr() as *mut _
);
81 /// Computes the SHA384 hash of some data.
82 #[corresponds(SHA384)]
84 pub fn sha384(data
: &[u8]) -> [u8; 48] {
86 let mut hash
= MaybeUninit
::<[u8; 48]>::uninit();
87 ffi
::SHA384(data
.as_ptr(), data
.len(), hash
.as_mut_ptr() as *mut _
);
92 /// Computes the SHA512 hash of some data.
93 #[corresponds(SHA512)]
95 pub fn sha512(data
: &[u8]) -> [u8; 64] {
97 let mut hash
= MaybeUninit
::<[u8; 64]>::uninit();
98 ffi
::SHA512(data
.as_ptr(), data
.len(), hash
.as_mut_ptr() as *mut _
);
104 if #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] {
105 /// An object which calculates a SHA1 hash of some data.
109 /// SHA1 is known to be insecure - it should not be used unless required for
110 /// compatibility with existing systems.
112 pub struct Sha1(ffi
::SHA_CTX
);
114 impl Default
for Sha1
{
116 fn default() -> Sha1
{
122 /// Creates a new hasher.
123 #[corresponds(SHA1_Init)]
125 pub fn new() -> Sha1
{
127 let mut ctx
= MaybeUninit
::uninit();
128 ffi
::SHA1_Init( ctx
.as_mut_ptr());
129 Sha1(ctx
.assume_init())
133 /// Feeds some data into the hasher.
135 /// This can be called multiple times.
136 #[corresponds(SHA1_Update)]
138 pub fn update(&mut self, buf
: &[u8]) {
140 ffi
::SHA1_Update(&mut self.0, buf
.as_ptr() as *const c_void
, buf
.len());
144 /// Returns the hash of the data.
145 #[corresponds(SHA1_Final)]
147 pub fn finish(mut self) -> [u8; 20] {
149 let mut hash
= MaybeUninit
::<[u8; 20]>::uninit();
150 ffi
::SHA1_Final(hash
.as_mut_ptr() as *mut _
, &mut self.0);
156 /// An object which calculates a SHA224 hash of some data.
158 pub struct Sha224(ffi
::SHA256_CTX
);
160 impl Default
for Sha224
{
162 fn default() -> Sha224
{
168 /// Creates a new hasher.
169 #[corresponds(SHA224_Init)]
171 pub fn new() -> Sha224
{
173 let mut ctx
= MaybeUninit
::uninit();
174 ffi
::SHA224_Init(ctx
.as_mut_ptr());
175 Sha224(ctx
.assume_init())
179 /// Feeds some data into the hasher.
181 /// This can be called multiple times.
182 #[corresponds(SHA224_Update)]
184 pub fn update(&mut self, buf
: &[u8]) {
186 ffi
::SHA224_Update(&mut self.0, buf
.as_ptr() as *const c_void
, buf
.len());
190 /// Returns the hash of the data.
191 #[corresponds(SHA224_Final)]
193 pub fn finish(mut self) -> [u8; 28] {
195 let mut hash
= MaybeUninit
::<[u8; 28]>::uninit();
196 ffi
::SHA224_Final(hash
.as_mut_ptr() as *mut _
, &mut self.0);
202 /// An object which calculates a SHA256 hash of some data.
204 pub struct Sha256(ffi
::SHA256_CTX
);
206 impl Default
for Sha256
{
208 fn default() -> Sha256
{
214 /// Creates a new hasher.
215 #[corresponds(SHA256_Init)]
217 pub fn new() -> Sha256
{
219 let mut ctx
= MaybeUninit
::uninit();
220 ffi
::SHA256_Init(ctx
.as_mut_ptr());
221 Sha256(ctx
.assume_init())
225 /// Feeds some data into the hasher.
227 /// This can be called multiple times.
228 #[corresponds(SHA256_Update)]
230 pub fn update(&mut self, buf
: &[u8]) {
232 ffi
::SHA256_Update(&mut self.0, buf
.as_ptr() as *const c_void
, buf
.len());
236 /// Returns the hash of the data.
237 #[corresponds(SHA256_Final)]
239 pub fn finish(mut self) -> [u8; 32] {
241 let mut hash
= MaybeUninit
::<[u8; 32]>::uninit();
242 ffi
::SHA256_Final(hash
.as_mut_ptr() as *mut _
, &mut self.0);
248 /// An object which calculates a SHA384 hash of some data.
250 pub struct Sha384(ffi
::SHA512_CTX
);
252 impl Default
for Sha384
{
254 fn default() -> Sha384
{
260 /// Creates a new hasher.
261 #[corresponds(SHA384_Init)]
263 pub fn new() -> Sha384
{
265 let mut ctx
= MaybeUninit
::uninit();
266 ffi
::SHA384_Init(ctx
.as_mut_ptr());
267 Sha384(ctx
.assume_init())
271 /// Feeds some data into the hasher.
273 /// This can be called multiple times.
274 #[corresponds(SHA384_Update)]
276 pub fn update(&mut self, buf
: &[u8]) {
278 ffi
::SHA384_Update(&mut self.0, buf
.as_ptr() as *const c_void
, buf
.len());
282 /// Returns the hash of the data.
283 #[corresponds(SHA384_Final)]
285 pub fn finish(mut self) -> [u8; 48] {
287 let mut hash
= MaybeUninit
::<[u8; 48]>::uninit();
288 ffi
::SHA384_Final(hash
.as_mut_ptr() as *mut _
, &mut self.0);
294 /// An object which calculates a SHA512 hash of some data.
296 pub struct Sha512(ffi
::SHA512_CTX
);
298 impl Default
for Sha512
{
300 fn default() -> Sha512
{
306 /// Creates a new hasher.
307 #[corresponds(SHA512_Init)]
309 pub fn new() -> Sha512
{
311 let mut ctx
= MaybeUninit
::uninit();
312 ffi
::SHA512_Init(ctx
.as_mut_ptr());
313 Sha512(ctx
.assume_init())
317 /// Feeds some data into the hasher.
319 /// This can be called multiple times.
320 #[corresponds(SHA512_Update)]
322 pub fn update(&mut self, buf
: &[u8]) {
324 ffi
::SHA512_Update(&mut self.0, buf
.as_ptr() as *const c_void
, buf
.len());
328 /// Returns the hash of the data.
329 #[corresponds(SHA512_Final)]
331 pub fn finish(mut self) -> [u8; 64] {
333 let mut hash
= MaybeUninit
::<[u8; 64]>::uninit();
334 ffi
::SHA512_Final(hash
.as_mut_ptr() as *mut _
, &mut self.0);
349 let expected
= "a9993e364706816aba3e25717850c26c9cd0d89d";
351 assert_eq
!(hex
::encode(sha1(data
)), expected
);
355 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
357 let expected
= "a9993e364706816aba3e25717850c26c9cd0d89d";
359 let mut hasher
= Sha1
::new();
361 hasher
.update(b
"bc");
362 assert_eq
!(hex
::encode(hasher
.finish()), expected
);
366 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
367 fn cloning_allows_incremental_hashing() {
368 let expected
= "a9993e364706816aba3e25717850c26c9cd0d89d";
370 let mut hasher
= Sha1
::new();
373 let mut incr_hasher
= hasher
.clone();
374 incr_hasher
.update(b
"bc");
376 assert_eq
!(hex
::encode(incr_hasher
.finish()), expected
);
377 assert_ne
!(hex
::encode(hasher
.finish()), expected
);
381 fn standalone_224() {
383 let expected
= "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
385 assert_eq
!(hex
::encode(sha224(data
)), expected
);
389 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
391 let expected
= "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
393 let mut hasher
= Sha224
::new();
395 hasher
.update(b
"bc");
396 assert_eq
!(hex
::encode(hasher
.finish()), expected
);
400 fn standalone_256() {
402 let expected
= "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
404 assert_eq
!(hex
::encode(sha256(data
)), expected
);
408 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
410 let expected
= "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
412 let mut hasher
= Sha256
::new();
414 hasher
.update(b
"bc");
415 assert_eq
!(hex
::encode(hasher
.finish()), expected
);
419 fn standalone_384() {
422 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\
423 7cc2358baeca134c825a7";
425 assert_eq
!(hex
::encode(&sha384(data
)[..]), expected
);
429 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
432 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\
433 7cc2358baeca134c825a7";
435 let mut hasher
= Sha384
::new();
437 hasher
.update(b
"bc");
438 assert_eq
!(hex
::encode(&hasher
.finish()[..]), expected
);
442 fn standalone_512() {
445 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\
446 fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
448 assert_eq
!(hex
::encode(&sha512(data
)[..]), expected
);
452 #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))]
455 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\
456 fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
458 let mut hasher
= Sha512
::new();
460 hasher
.update(b
"bc");
461 assert_eq
!(hex
::encode(&hasher
.finish()[..]), expected
);