Remove deprecated memory.op.cmp and Entropy class
This commit is contained in:
parent
c293c6c809
commit
03e21d4368
@ -18,10 +18,6 @@ import std.digest.sha;
|
||||
import tanya.memory;
|
||||
import tanya.typecons;
|
||||
|
||||
/// Block size of entropy accumulator (SHA-512).
|
||||
deprecated
|
||||
enum blockSize = 64;
|
||||
|
||||
/// Maximum amount gathered from the entropy sources.
|
||||
enum maxGather = 128;
|
||||
|
||||
@ -339,176 +335,3 @@ static if (is(PlatformEntropySource)) @nogc @system unittest
|
||||
assert(source.threshold == 32);
|
||||
assert(source.strong);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pseudorandom number generator.
|
||||
* ---
|
||||
* auto entropy = defaultAllocator.make!Entropy();
|
||||
*
|
||||
* ubyte[blockSize] output;
|
||||
*
|
||||
* output = entropy.random;
|
||||
*
|
||||
* defaultAllocator.dispose(entropy);
|
||||
* ---
|
||||
*/
|
||||
deprecated
|
||||
class Entropy
|
||||
{
|
||||
/// Entropy sources.
|
||||
protected EntropySource[] sources;
|
||||
|
||||
private ubyte sourceCount_;
|
||||
|
||||
/// Entropy accumulator.
|
||||
protected SHA!(maxGather * 8, 512) accumulator;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* maxSources = Maximum amount of entropy sources can be set.
|
||||
* allocator = Allocator to allocate entropy sources available on the
|
||||
* system.
|
||||
*/
|
||||
this(const size_t maxSources = 20,
|
||||
shared Allocator allocator = defaultAllocator) @nogc
|
||||
in
|
||||
{
|
||||
assert(maxSources > 0 && maxSources <= ubyte.max);
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
allocator.resize(sources, maxSources);
|
||||
|
||||
static if (is(PlatformEntropySource))
|
||||
{
|
||||
this ~= allocator.make!PlatformEntropySource;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Amount of the registered entropy sources.
|
||||
*/
|
||||
@property ubyte sourceCount() const @nogc nothrow pure @safe
|
||||
{
|
||||
return sourceCount_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an entropy source.
|
||||
*
|
||||
* Params:
|
||||
* source = Entropy source.
|
||||
*
|
||||
* Returns: $(D_PSYMBOL this).
|
||||
*
|
||||
* See_Also:
|
||||
* $(D_PSYMBOL EntropySource)
|
||||
*/
|
||||
Entropy opOpAssign(string op)(EntropySource source)
|
||||
@nogc nothrow pure @safe
|
||||
if (op == "~")
|
||||
in
|
||||
{
|
||||
assert(sourceCount_ <= sources.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
sources[sourceCount_++] = source;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Generated random sequence.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL EntropyException) if no strong entropy source was
|
||||
* registered or it failed.
|
||||
*/
|
||||
@property ubyte[blockSize] random() @nogc
|
||||
in
|
||||
{
|
||||
assert(sourceCount_ > 0, "No entropy sources defined.");
|
||||
}
|
||||
do
|
||||
{
|
||||
bool haveStrong;
|
||||
ushort done;
|
||||
ubyte[blockSize] output;
|
||||
|
||||
do
|
||||
{
|
||||
ubyte[maxGather] buffer;
|
||||
|
||||
// Run through our entropy sources
|
||||
for (ubyte i; i < sourceCount; ++i)
|
||||
{
|
||||
auto outputLength = sources[i].poll(buffer);
|
||||
|
||||
if (!outputLength.isNothing)
|
||||
{
|
||||
if (outputLength > 0)
|
||||
{
|
||||
update(i, buffer, outputLength);
|
||||
sources[i].size = cast(ushort) (sources[i].size + outputLength);
|
||||
}
|
||||
if (sources[i].size < sources[i].threshold)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (sources[i].strong)
|
||||
{
|
||||
haveStrong = true;
|
||||
}
|
||||
}
|
||||
done = 257;
|
||||
}
|
||||
}
|
||||
while (++done < 256);
|
||||
|
||||
if (!haveStrong)
|
||||
{
|
||||
throw defaultAllocator.make!EntropyException("No strong entropy source defined.");
|
||||
}
|
||||
|
||||
output = accumulator.finish();
|
||||
|
||||
// Reset accumulator and counters and recycle existing entropy
|
||||
accumulator.start();
|
||||
|
||||
// Perform second SHA-512 on entropy
|
||||
output = sha512Of(output);
|
||||
|
||||
for (ubyte i; i < sourceCount; ++i)
|
||||
{
|
||||
sources[i].size = 0;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update entropy accumulator.
|
||||
*
|
||||
* Params:
|
||||
* sourceId = Entropy source index in $(D_PSYMBOL sources).
|
||||
* data = Data got from the entropy source.
|
||||
* length = Length of the received data.
|
||||
*/
|
||||
protected void update(in ubyte sourceId,
|
||||
ref ubyte[maxGather] data,
|
||||
ubyte length) @nogc nothrow pure @safe
|
||||
{
|
||||
ubyte[2] header;
|
||||
|
||||
if (length > blockSize)
|
||||
{
|
||||
data[0 .. 64] = sha512Of(data);
|
||||
length = blockSize;
|
||||
}
|
||||
|
||||
header[0] = sourceId;
|
||||
header[1] = length;
|
||||
|
||||
accumulator.put(header);
|
||||
accumulator.put(data[0 .. length]);
|
||||
}
|
||||
}
|
||||
|
@ -224,41 +224,6 @@ do
|
||||
assert(equal(r1, r2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two memory areas $(D_PARAM r1) and $(D_PARAM r2).
|
||||
*
|
||||
* $(D_PSYMBOL cmp) returns a positive integer if
|
||||
* $(D_INLINECODE r1.length > r2.length) or the first `n` compared bytes of
|
||||
* $(D_PARAM r1) found to be greater than the first `n` bytes of $(D_PARAM r2),
|
||||
*
|
||||
* $(D_PSYMBOL cmp) returns a negative integer if
|
||||
* $(D_INLINECODE r2.length > r1.length) or the first `n` compared bytes of
|
||||
* $(D_PARAM r1) found to be less than the first `n` bytes of $(D_PARAM r2),
|
||||
*
|
||||
* `0` is returned otherwise.
|
||||
*
|
||||
* Returns: Positive integer if $(D_INLINECODE r1 > r2),
|
||||
* negative integer if $(D_INLINECODE r2 > r1),
|
||||
* `0` if $(D_INLINECODE r1 == r2).
|
||||
*/
|
||||
deprecated("Use tanya.memory.op.equal() or tanya.algorithm.comparison.compare() instead")
|
||||
int cmp(const void[] r1, const void[] r2) @nogc nothrow pure @trusted
|
||||
in
|
||||
{
|
||||
assert(r1.length == 0 || r1.ptr !is null);
|
||||
assert(r2.length == 0 || r2.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
import core.stdc.string : memcmp;
|
||||
|
||||
if (r1.length > r2.length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return r1.length < r2.length ? -1 : memcmp(r1.ptr, r2.ptr, r1.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the first occurrence of $(D_PARAM needle) in $(D_PARAM haystack) if
|
||||
* any.
|
||||
|
Loading…
Reference in New Issue
Block a user