Skip to content

Make std.digest.hmac @safe and runnable#5587

Merged
wilzbach merged 2 commits intodlang:masterfrom
wilzbach:digest-runnable
Jul 11, 2017
Merged

Make std.digest.hmac @safe and runnable#5587
wilzbach merged 2 commits intodlang:masterfrom
wilzbach:digest-runnable

Conversation

@wilzbach
Copy link
Contributor

When I first encountered the version(StdDdoc) unittest in std.digest.hmac, I tried to go "the easy way" and ignore versioned unittest in the tests_extractor (-> dlang/tools#249).
However @CyberShadow correctly reminded me that it makes more sense to fix the outlier and I even discovered another problem about the version(StdDdoc) approach - it's not part of the testsuite and apparently at some point in the time std.digest.hmac was @safe (it's not anymore). I fixed that as well and as a consequence std.digest.hmac is now completely @safe 🎉
So I am really happy about @CyberShadow's remark to avoid working around the outlier with the tool!

CC @jpf91

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

import std.digest.hmac, std.digest.sha, std.stdio;
import std.ascii : LetterCase;
import std.digest.digest : toHexString;
import std.digest.sha : SHA1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's time we move on with #5013 and add a package.d to std.digest ...

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

{
import std.digest.hmac, std.digest.sha, std.stdio;
import std.ascii : LetterCase;
import std.digest.digest : toHexString;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#5013 was merged and as far as I understand, this can be changed to: import std.digest : toHexString

auto hash = HMAC!(H, blockSize)(secret);
foreach (datum; data)
copy(datum, &hash);
put(hash, datum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any difference in performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None, we even save a function call on bad optimizers as copy does exactly the same:

TargetRange copy(SourceRange, TargetRange)(SourceRange source, TargetRange target)
if (!areCopyCompatibleArrays!(SourceRange, TargetRange) &&
    isInputRange!SourceRange &&
    isOutputRange!(TargetRange, ElementType!SourceRange))
{
    // Specialize for 2 random access ranges.
    // Typically 2 random access ranges are faster iterated by common
    // index than by x.popFront(), y.popFront() pair
    static if (isRandomAccessRange!SourceRange &&
               hasLength!SourceRange &&
               hasSlicing!TargetRange &&
               isRandomAccessRange!TargetRange &&
               hasLength!TargetRange)
    {
        auto len = source.length;
        foreach (idx; 0 .. len)
            target[idx] = source[idx];
        return target[len .. target.length];
    }
    else
    {
        put(target, source);
        return target;
    }
}

The output rangeHMAC doesn't have a length, nor slicing or random access.

@wilzbach
Copy link
Contributor Author

#5013 was merged and as far as I understand, this can be changed to: import std.digest : toHexString

Yes - 🎉

if (allSatisfy!(isDigestibleRange, typeof(data)))
{
import std.algorithm.mutation : copy;
import std.range : put;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be specified to import std.range.primitives : put;

Copy link
Contributor

@jpf91 jpf91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wilzbach wilzbach merged commit 4e14bef into dlang:master Jul 11, 2017
@wilzbach wilzbach deleted the digest-runnable branch July 11, 2017 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants