[haiku-development] Re: Haiku "Crypto Kit"

  • From: "Alexander von Gluck IV" <kallisti5@xxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Mon, 11 Jan 2016 16:39:32 +0000

January 11 2016 9:26 AM, "Alexander von Gluck IV" <kallisti5@xxxxxxxxxxx> wrote:

January 11 2016 4:27 AM, "Ingo Weinhold" <ingo_weinhold@xxxxxx> wrote:

On 11.01.2016 03:51, Alexander von Gluck IV wrote:

Feedback *highly* welcome.
I've only invested a day or so into it... so if everyone hates the idea
it's no big deal :-)

When creating new Haiku APIs that already exist in mature tool kits, I can
wholeheartedly recommend
looking at and steal^Hborrowing ideas from those. In this case e.g. Java [1]
and Qt [2]. Two issues
are:

* You don't want to have a one-shot method only. When hashing a file or any
kind of stream one
usually processes the data iteratively. Therefore Java and Qt offer separate
methods for processing
data iteratively (update()/addData()) and computing the final hash
(digest()/result()). Additional
one-shot convenience methods (possibly even static ones) certainly don't
harm, though.

* You don't want to return the hash as a hex string (only/by default). That
is a representation
mostly used for human consumption. Unfortunately lacking an appropriate
Haiku API class there isn't
a convenient way to return byte arrays of unknown size (I pre-allocated the
array and added a
method to return the size in headers/private/shared/SHA256.h). Obviously a
method to convert a byte
array to a BString is needed as well, but it would fit better as a (static)
utility method in
BString.

CU, Ingo

[1]
https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html
[2] http://doc.qt.io/qt-5/qcryptographichash.html

Definitely good points. I had already planned to add more access methods, but
following
roughly something like the Qt API would make it more flexible.

It looks like QT handles multiple input methods by storing whatever data needs
hashed in the class before hashing... that is likely a lot more flexible than
the current
"tell us where to look and we'll go hash it"

I'll make some adjustments to the API to be more Qt like and will re-post.
This is why
I only implemented one hash type so far, I figured the API would need some
work ;-)

Going in a better direction?
Like you said, no real great API to returning a bunch of hex data minus a char*


class BCryptoHash {
public:
BCryptoHash(algorithm_type algorithm);
~BCryptoHash();

BString Name();

status_t AddData(void* input, size_t length);
status_t AddData(BFile* file);
status_t AddData(area_id id);

status_t Result(BString* hash);

status_t Reset();

private:
algorithm_type fType;
BPrivate::HashAlgorithm* fAlgorithm;
};



-- Alex

Other related posts: