www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to print ubyte*

reply "brad clawsie" <brad b7j0c.org> writes:
hi, I'm back again with another openssl related question.

given this program

--------------

   import std.stdio;
   import deimos.openssl.hmac;
   import deimos.openssl.evp;

   void main() {
       HMAC_CTX *ctx = new HMAC_CTX;
       HMAC_CTX_init(ctx);
       auto key = "123456";
       auto s = "hello";

       auto digest = HMAC(EVP_sha1(),
                          cast(void *) key,
                          cast(int) key.length,
                          cast(ubyte*) s,
                          cast(int) s.length,
                          null,null);
   }

--------------

"digest" should be of type ubyte*

does anyone know how to print this out as ascii?

thanks!
brad
Apr 30 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
brad clawsie:

       auto digest = HMAC(EVP_sha1(),
                          cast(void *) key,
Better to attach the * to void.
                          cast(int) key.length,
                          cast(ubyte*) s,
Here you are casting a struct of pointer to immutable plus length to a mutable ubyte pointer.
                          cast(int) s.length,
                          null,null);
This whole function call is quite bug-prone.
 "digest" should be of type ubyte*

 does anyone know how to print this out as ascii?
Do you mean in hex? Perhaps something like this? But hardcodes the hash function output length: writefln("%-(%02x%)", digest[0 .. 40]) Bye, bearophile
Apr 30 2014
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wed, 30 Apr 2014 07:27:23 +0000
brad clawsie via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 hi, I'm back again with another openssl related question.
 
 given this program
 
 --------------
 
    import std.stdio;
    import deimos.openssl.hmac;
    import deimos.openssl.evp;
 
    void main() {
        HMAC_CTX *ctx = new HMAC_CTX;
        HMAC_CTX_init(ctx);
        auto key = "123456";
        auto s = "hello";
 
        auto digest = HMAC(EVP_sha1(),
                           cast(void *) key,
                           cast(int) key.length,
                           cast(ubyte*) s,
                           cast(int) s.length,
                           null,null);
    }
 
 --------------
 
 "digest" should be of type ubyte*
 
 does anyone know how to print this out as ascii?
If you want to print a ubyte*, then you can do something like auto str = cast(char[])digest[0 .. lengthOfDigest]; writeln(str); Slicing the pointer results in an array, and you can cast ubyte[] to char[], which will print as characters rather than their integral values. - Jonathan M Davis
Apr 30 2014