digitalmars.D.learn - How to hash SHA256 from string?
- zoujiaqing (30/30) Dec 02 2023 ```D
- user1234 (17/47) Dec 02 2023 sign is binary, you have to use the toHexString utility :
- user1234 (5/23) Dec 02 2023 and a last error I have not initially catch, use writefln:
- An Pham (3/8) Dec 02 2023 Your data has garbage at the end; try
- Sergey (3/4) Dec 02 2023 Sorry for OT, but don’t know different place to reach you out.
- zoujiaqing (16/16) Dec 03 2023 Thank u every one ;)
- Jacob Shtokolov (13/14) Dec 05 2023 Well, what about:
```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
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
On Saturday, 2 December 2023 at 16:17:08 UTC, user1234 wrote:On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:and a last error I have not initially catch, use writefln: ```d writefln("sign: %s", toHexString(sign)); ```[...]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
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
On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:SHASorry 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
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
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