www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to hash SHA256 from string?

reply zoujiaqing <zoujiaqing gmail.com> writes:
```D
import std.stdio;
import std.digest.sha;

void main()
{

     SHA256 sha256;
     sha256.start();
     string appKey = 
"11111111111111111111111111111111111111111111111111111";
     ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
     sha256.put(data);
     ubyte[32] sign = sha256.finish();

     string sign1 = cast(string) sign[0..$];
     writeln("sign: %s", sign1);
}
```

The result varies when you run the code repeatedly and the 
display is garbled:
```
zoujiaqing mac test % ./test
Getui access sign: %s>tM?a?j,???ߥm?8l~??uzU?|9?~ˡ
zoujiaqing mac test % ./test
Getui access sign: %s1-??U?
?d<3^3??נ? ??P%u/Iv
zoujiaqing mac test % ./test
Getui access sign: %s1?ϻN?????ށ?`O?p!?O?4U
:8J~%ʬ
zoujiaqing mac test % ./test
Getui access sign: %s??????k#O?;?ڋ?5T?"=??;???e
```
Dec 02 2023
next sibling parent reply user1234 <user1234 12.de> writes:
On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:
 ```D
 import std.stdio;
 import std.digest.sha;

 void main()
 {

     SHA256 sha256;
     sha256.start();
     string appKey = 
 "11111111111111111111111111111111111111111111111111111";
     ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
     sha256.put(data);
     ubyte[32] sign = sha256.finish();

     string sign1 = cast(string) sign[0..$];
     writeln("sign: %s", sign1);
 }
 ```

 The result varies when you run the code repeatedly and the 
 display is garbled:
 ```
 zoujiaqing mac test % ./test
 Getui access sign: %s>tM?a?j,???ߥm?8l~??uzU?|9?~ˡ
 zoujiaqing mac test % ./test
 Getui access sign: %s1-??U?
 ?d<3^3??נ? ??P%u/Iv
 zoujiaqing mac test % ./test
 Getui access sign: %s1?ϻN?????ށ?`O?p!?O?4U
 :8J~%ʬ
 zoujiaqing mac test % ./test
 Getui access sign: %s??????k#O?;?ڋ?5T?"=??;???e
 ```
sign is binary, you have to use the toHexString utility : ```d import std.stdio; import std.digest.sha; void main() { SHA256 sha256; sha256.start(); string appKey = "11111111111111111111111111111111111111111111111111111"; sha256.put(cast(ubyte[])appKey); ubyte[32] sign = sha256.finish(); writeln("sign: %s", toHexString(sign)); } ``` also you add a range error on data assignment.
Dec 02 2023
parent user1234 <user1234 12.de> writes:
On Saturday, 2 December 2023 at 16:17:08 UTC, user1234 wrote:
 On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:
 [...]
sign is binary, you have to use the toHexString utility : ```d import std.stdio; import std.digest.sha; void main() { SHA256 sha256; sha256.start(); string appKey = "11111111111111111111111111111111111111111111111111111"; sha256.put(cast(ubyte[])appKey); ubyte[32] sign = sha256.finish(); writeln("sign: %s", toHexString(sign)); } ``` also you add a range error on data assignment.
and a last error I have not initially catch, use writefln: ```d writefln("sign: %s", toHexString(sign)); ```
Dec 02 2023
prev sibling next sibling parent An Pham <home home.com> writes:
On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:
 ```D
     string appKey = 
 "11111111111111111111111111111111111111111111111111111";
     ubyte[1024] data = cast(ubyte[])(appKey.dup[0..$]);
     sha256.put(data);
Your data has garbage at the end; try sha256.put(data[0..appKey.length])
Dec 02 2023
prev sibling next sibling parent Sergey <kornburn yandex.ru> writes:
On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:
 SHA
Sorry for OT, but don’t know different place to reach you out. What is the status of Archttp? Is it discontinued/abandoned?
Dec 02 2023
prev sibling parent reply zoujiaqing <zoujiaqing gmail.com> writes:
Thank u every one ;)

Use botan so easy:
```D
import std.stdio;
import botan;

void main()
{
     string appKey = 
"11111111111111111111111111111111111111111111111111111";

     auto sha256 = retrieveHash("SHA-256").clone();
     sha256.update(appKey);

     string sign = 
sha256.finished()[].toHexString!(LetterCase.lower);

     writeln("sign: %s", sign);
}
```
Dec 03 2023
parent Jacob Shtokolov <jacob.100205 gmail.com> writes:
On Sunday, 3 December 2023 at 13:42:53 UTC, zoujiaqing wrote:
 Use botan so easy:
Well, what about: ```D import std.digest.sha; import std.stdio; void main() { string appKey = "11111111111111111111111111111111111111111111111111111"; appKey.sha256Of.toHexString.writeln; } ``` Not sure if it's really more complicated than Botan 🤷‍♂️️
Dec 05 2023