Thread overview
How to hash SHA256 from string?
Dec 02
user1234
Dec 02
user1234
Dec 02
An Pham
Dec 02
Sergey
December 02
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
December 02

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

>
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 :

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.

December 02

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 :

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:

writefln("sign: %s", toHexString(sign));
December 02

On Saturday, 2 December 2023 at 15:30:39 UTC, zoujiaqing wrote:

>
    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])

December 02

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?

December 03

Thank u every one ;)

Use botan so easy:

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);
}
December 05

On Sunday, 3 December 2023 at 13:42:53 UTC, zoujiaqing wrote:

>

Use botan so easy:

Well, what about:

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 🤷‍♂️️