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

  • From: "Alexander von Gluck IV" <kallisti5@xxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 15 Jan 2016 21:59:09 +0000

January 12 2016 9:34 PM, "Alexander von Gluck IV" <kallisti5@xxxxxxxxxxx> wrote:

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 ;-)

Looking a bit better?

https://github.com/kallisti5/haiku/blob/crypto/headers/os/crypto/CryptoHash.h

Now we can "import" various types of data to be hashed, then clear the buffer
and do it all over again :-)

Ok, things are pretty solid at this point.

Currently the crypto kit can hash MD4 and BLAKE256 from various sources such as
BString, BFile, or memory pointers.

Adding new algorithms is easy (adding MD4 support)
https://github.com/kallisti5/haiku/commit/0e44ce02b95a920f679acf41a4c896b3679650c9

Kit API:
https://github.com/kallisti5/haiku/blob/crypto/headers/os/crypto/CryptoHash.h

BCryptoHash code:
https://github.com/kallisti5/haiku/blob/crypto/src/kits/crypto/CryptoHash.cpp


The "BCrypto" part that I plan to do encryption with isn't done yet.
I'm no crypto expert, help is welcome there :-)


-- Alex

Other related posts: