# \[sha2 crate\] obtain hashing output as a String

**URL:** <https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091>\
**Category:** help\
**Created:** [August 10, 2020, 2:28am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091 "2020-08-10T02:28:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjpe](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jjpe/32/28767_2.png) [@jjpe](https://users.rust-lang.org/u/jjpe)\
**Post date:** [August 10, 2020, 2:28am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/1 "2020-08-10T02:28:38Z")

</div>

I'm trying to use the `sha2` crate from the rust crypto project.  
Most of it works, but the output hash is a value of `GenericArray<u8, _>`:

```rust
use sha2::{Digest, Sha512};

let input = String::from("hello");

let mut hasher = Sha512::new();
hasher.update(input);
let hash_result = hasher.finalize(); // <-- GenericArray<u8, _>

```

However, what I need is not a byte array but a `String` as output.  
I tried converting it with `std::str::from_utf8(&hash_result[..])` but that errors out with `Err(Utf8Error { valid_up_to: 0, error_len: Some(1) })` as an error value. This indicates that the byte array does in fact not contain valid UTF-8.

Does anyone know how I can get a valid UTF-8 string from `hash_result`?

---

<div class="post-metadata">

**Author:** ![gregwtmtno](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gregwtmtno/32/12424_2.png) [@gregwtmtno](https://users.rust-lang.org/u/gregwtmtno)\
**Post date:** [August 10, 2020, 2:50am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/2 "2020-08-10T02:50:37Z")

</div>

You probably want to convert it to a hex representation. The type implements UpperHex and LowerHex.

You could do:

```
let string = format!("{:x}", hash_result);

```

from\_utf8 would be used when you have a byte array of utf8 characters.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [August 10, 2020, 3:38am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/3 "2020-08-10T03:38:06Z")

</div>

Note that this solution will not work after migration to const generics, so I would recommend to use the `hex` crate instead.

Judging by this [issue](https://github.com/RustCrypto/hashes/issues/185), the author has found a satisfying solution.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jjpe/32/28767_2.png) [@jjpe](https://users.rust-lang.org/u/jjpe)\
**Post date:** [August 10, 2020, 7:24am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/4 "2020-08-10T07:24:42Z")

</div>

> [@newpavlov](#):
>
> Note that this solution will not work after migration to const generics, so I would recommend to use the `hex` crate instead.

That's too bad. By using the accepted solution I was hoping to precisely not have to use the `hex` crate.  
I like the simplicity of just hex debug printing it.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [August 10, 2020, 7:49am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/5 "2020-08-10T07:49:46Z")

</div>

It could be implemented in future Rust versions, see the following issues:  
[https://github.com/rust-lang/rust/issues/33127](https://github.com/rust-lang/rust/issues/33127)  
[https://github.com/rust-lang/rfcs/issues/1916](https://github.com/rust-lang/rfcs/issues/1916)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [November 8, 2020, 7:49am UTC](https://users.rust-lang.org/t/sha2-crate-obtain-hashing-output-as-a-string/47091/6 "2020-11-08T07:49:56Z")

</div>

Closed on November 8, 2020, 7:49am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
