W3cubDocs

/D

std.digest.ripemd

Computes RIPEMD-160 hashes of arbitrary data. RIPEMD-160 hashes are 20 byte quantities that are like a checksum or CRC, but are more robust.



Category Functions
Template API RIPEMD160
OOP API RIPEMD160Digest
Helpers ripemd160Of


This module conforms to the APIs defined in std.digest. To understand the differences between the template and the OOP API, see std.digest.

This module publicly imports std.digest and can be used as a stand-alone module.
License:
Boost License 1.0.
CTFE
Digests do not work in CTFE
Authors:
Kai Nacke
The algorithm was designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel.
The D implementation is a direct translation of the ANSI C implementation by Antoon Bosselaers.
References
Source
std/digest/ripemd.d
Examples:
//Template API
import std.digest.md;

ubyte[20] hash = ripemd160Of("abc");
writeln(toHexString(hash)); // "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC"

//Feeding data
ubyte[1024] data;
RIPEMD160 md;
md.start();
md.put(data[]);
md.start(); //Start again
md.put(data[]);
hash = md.finish();
Examples:
//OOP API
import std.digest.md;

auto md = new RIPEMD160Digest();
ubyte[] hash = md.digest("abc");
writeln(toHexString(hash)); // "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC"

//Feeding data
ubyte[1024] data;
md.put(data[]);
md.reset(); //Start again
md.put(data[]);
hash = md.finish();
struct RIPEMD160

Template API RIPEMD160 implementation. See std.digest for differences between template and OOP API.

Examples:
//Simple example, hashing a string using ripemd160Of helper function
ubyte[20] hash = ripemd160Of("abc");
//Let's get a hash string
writeln(toHexString(hash)); // "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC"
Examples:
//Using the basic API
RIPEMD160 hash;
hash.start();
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[20] result = hash.finish();
Examples:
//Let's use the template features:
void doSomething(T)(ref T hash)
if (isDigest!T)
{
    hash.put(cast(ubyte) 0);
}
RIPEMD160 md;
md.start();
doSomething(md);
writeln(toHexString(md.finish())); // "C81B94933420221A7AC004A90242D8B1D3E5070D"
Examples:
//Simple example
RIPEMD160 hash;
hash.start();
hash.put(cast(ubyte) 0);
ubyte[20] result = hash.finish();
writeln(toHexString(result)); // "C81B94933420221A7AC004A90242D8B1D3E5070D"
pure nothrow @nogc @trusted void put(scope const(ubyte)[] data...)

Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

Example
RIPEMD160 dig;
dig.put(cast(ubyte) 0); //single ubyte
dig.put(cast(ubyte) 0, cast(ubyte) 0); //variadic
ubyte[10] buf;
dig.put(buf); //buffer
pure nothrow @nogc @safe void start()

Used to (re)initialize the RIPEMD160 digest.

Note
For this RIPEMD160 Digest implementation calling start after default construction is not necessary. Calling start is only necessary to reset the Digest.
Generic code which deals with different Digest types should always call start though.
Example
RIPEMD160 digest;
//digest.start(); //Not necessary
digest.put(0);
pure nothrow @nogc @trusted ubyte[20] finish()

Returns the finished RIPEMD160 hash. This also calls start to reset the internal state.

Example
//Simple example
RIPEMD160 hash;
hash.start();
hash.put(cast(ubyte) 0);
ubyte[20] result = hash.finish();
assert(toHexString(result) == "C81B94933420221A7AC004A90242D8B1D3E5070D");
auto ripemd160Of(T...)(T data)

This is a convenience alias for std.digest.digest using the RIPEMD160 implementation.

Examples:
ubyte[20] hash = ripemd160Of("abc");
writeln(hash); // digest!RIPEMD160("abc")
alias RIPEMD160Digest = std.digest.WrapperDigest!(RIPEMD160).WrapperDigest

OOP API RIPEMD160 implementation. See std.digest for differences between template and OOP API.

This is an alias for std.digest.WrapperDigest!RIPEMD160, see there for more information.

Examples:
//Simple example, hashing a string using Digest.digest helper function
auto md = new RIPEMD160Digest();
ubyte[] hash = md.digest("abc");
//Let's get a hash string
writeln(toHexString(hash)); // "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC"
Examples:
//Let's use the OOP features:
void test(Digest dig)
{
  dig.put(cast(ubyte) 0);
}
auto md = new RIPEMD160Digest();
test(md);

//Let's use a custom buffer:
ubyte[20] buf;
ubyte[] result = md.finish(buf[]);
writeln(toHexString(result)); // "C81B94933420221A7AC004A90242D8B1D3E5070D"

© 1999–2017 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_digest_ripemd.html