Re: [Interest] hmac sha256

2018-10-16 Thread Jérôme Godbout
Ah, sorry about that, I made a typo, the 3rd one is giving the proper
result when I don't make a mistake by copy/paste (I did swap the api_key
and api_secret when calling my function, d'oh).

This is indeed correct:
QMessageAuthenticationCode::hash(payload, m_api_secret,
QCryptographicHash::Sha256).toHex();

Thanks and sorry about that.

On Mon, 15 Oct 2018 at 18:11, Hamish Moffatt 
wrote:

> On 16/10/18 08:16, Jérôme Godbout wrote:
>
> Hi,
> I'm trying to find the C++ equivalent of this python:
> *import hashlib*
> *import hmac*
> *hmac.new(bytearray(m_api_secret, 'utf-8'), bytearray(payload, 'utf-8'),
> hashlib.sha256).hexdigest()*
>
> Or my Javascript equivalent (both give the same results):
> crypto.createHmac("sha256", m_api_secret).update(payload).digest("hex");
>
> I have try the following:
>
>- QMessageAuthenticationCode::hash(payload,
>QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
>- QMessageAuthenticationCode::hash(payload,
>QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha3_256).toHex();
>- QMessageAuthenticationCode::hash(payload, m_api_secret,
>QCryptographicHash::Sha256).toHex();
>- QMessageAuthenticationCode::hash(payload, m_api_secret,
>QCryptographicHash::Sha3_256).toHex();
>- QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
>QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
>- QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
>QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha3_256).toHex();
>
> But I never get the same results as the Python or Javascript one. Maybe
> I'm using the wrong class here. Anybody have a good pointer on how to do
> this?
> Any good hmac sha256 class to perform this?
>
>
> In Python you encoded the key and payload as utf-8 before hashing, but you
> didn't mention the encoding in C++.
>
> What's in m_api_secret and payload? If they are hex strings then you
> definitely need the fromHex.
>
>
> Hamish
>
>
> ___
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>


-- 



RAPPROCHEZ LA DISTANCE


*Jérôme Godbout*Senior Software Developer

*p:* +1 (418) 800-1073 ext.:109

*m:* +1 (581) 777-0050

amotus.ca 
statum-iot.com
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] hmac sha256

2018-10-15 Thread Hamish Moffatt
On 16/10/18 08:16, Jérôme Godbout wrote:
> Hi,
> I'm trying to find the C++ equivalent of this python:
> /import hashlib/
> /import hmac/
> /hmac.new(bytearray(m_api_secret, 'utf-8'), bytearray(payload, 
> 'utf-8'), hashlib.sha256).hexdigest()/
> /
> /
> Or my Javascript equivalent (both give the same results):
> crypto.createHmac("sha256", m_api_secret).update(payload).digest("hex");
> /
> /
> I have try the following:
>
>   * QMessageAuthenticationCode::hash(payload,
> QByteArray::fromHex(m_api_secret),
> QCryptographicHash::Sha256).toHex();
>   * QMessageAuthenticationCode::hash(payload,
> QByteArray::fromHex(m_api_secret),
> QCryptographicHash::Sha3_256).toHex();
>   * QMessageAuthenticationCode::hash(payload, m_api_secret,
> QCryptographicHash::Sha256).toHex();
>   * QMessageAuthenticationCode::hash(payload, m_api_secret,
> QCryptographicHash::Sha3_256).toHex();
>   * QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
> QByteArray::fromHex(m_api_secret),
> QCryptographicHash::Sha256).toHex();
>   * QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
> QByteArray::fromHex(m_api_secret),
> QCryptographicHash::Sha3_256).toHex();
>
> But I never get the same results as the Python or Javascript one. 
> Maybe I'm using the wrong class here. Anybody have a good pointer on 
> how to do this?
> Any good hmac sha256 class to perform this?

In Python you encoded the key and payload as utf-8 before hashing, but 
you didn't mention the encoding in C++.

What's in m_api_secret and payload? If they are hex strings then you 
definitely need the fromHex.


Hamish


___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] hmac sha256

2018-10-15 Thread Henry Skoglund

On 2018-10-15 23:16, Jérôme Godbout wrote:

Hi,
I'm trying to find the C++ equivalent of this python:
/import hashlib/
/import hmac/
/hmac.new(bytearray(m_api_secret, 'utf-8'), bytearray(payload, 'utf-8'), 
hashlib.sha256).hexdigest()/

/
/
Or my Javascript equivalent (both give the same results):
crypto.createHmac("sha256", m_api_secret).update(payload).digest("hex");
/
/
I have try the following:

  * QMessageAuthenticationCode::hash(payload,
QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
  * QMessageAuthenticationCode::hash(payload,
QByteArray::fromHex(m_api_secret),
QCryptographicHash::Sha3_256).toHex();
  * QMessageAuthenticationCode::hash(payload, m_api_secret,
QCryptographicHash::Sha256).toHex();
  * QMessageAuthenticationCode::hash(payload, m_api_secret,
QCryptographicHash::Sha3_256).toHex();
  * QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
  * QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
QByteArray::fromHex(m_api_secret),
QCryptographicHash::Sha3_256).toHex();

But I never get the same results as the Python or Javascript one. Maybe 
I'm using the wrong class here. Anybody have a good pointer on how to do 
this?

Any good hmac sha256 class to perform this?



Hi, the QMessageAuthenticationCode class should work fine, and your #3 
attempt should be correct.


Just tested with key = "my secret key" and payload "my message":

Python3.6.6 on Ubuntu:
>>> hmac.new(bytearray('my secret key', 'utf-8'), bytearray('my 
message', 'utf-8'), hashlib.sha256).hexdigest()

'8cb6f28c5bd8e02fef83fdb7cc00b2116f5aefd868c3c4bed4db3b99b0450372'

Qt 5.11.2 on Win7:
qDebug() << QMessageAuthenticationCode::hash("my message","my 
secret key", QCryptographicHash::Sha256).toHex();


23:43:52: Starting 
C:\Projects\build-untitled-Desktop_Qt_5_11_2_MSVC2015_32bit-Release\release\untitled...

"8cb6f28c5bd8e02fef83fdb7cc00b2116f5aefd868c3c4bed4db3b99b0450372"

Rgrds Henry

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


[Interest] hmac sha256

2018-10-15 Thread Jérôme Godbout
Hi,
I'm trying to find the C++ equivalent of this python:
*import hashlib*
*import hmac*
*hmac.new(bytearray(m_api_secret, 'utf-8'), bytearray(payload, 'utf-8'),
hashlib.sha256).hexdigest()*

Or my Javascript equivalent (both give the same results):
crypto.createHmac("sha256", m_api_secret).update(payload).digest("hex");

I have try the following:

   - QMessageAuthenticationCode::hash(payload,
   QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
   - QMessageAuthenticationCode::hash(payload,
   QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha3_256).toHex();
   - QMessageAuthenticationCode::hash(payload, m_api_secret,
   QCryptographicHash::Sha256).toHex();
   - QMessageAuthenticationCode::hash(payload, m_api_secret,
   QCryptographicHash::Sha3_256).toHex();
   - QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
   QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha256).toHex();
   - QMessageAuthenticationCode::hash(QByteArray::fromHex(payload),
   QByteArray::fromHex(m_api_secret), QCryptographicHash::Sha3_256).toHex();

But I never get the same results as the Python or Javascript one. Maybe I'm
using the wrong class here. Anybody have a good pointer on how to do this?
Any good hmac sha256 class to perform this?

-- 



RAPPROCHEZ LA DISTANCE


*Jérôme Godbout*Senior Software Developer

*p:* +1 (418) 800-1073 ext.:109

*m:* +1 (581) 777-0050

amotus.ca 
statum-iot.com
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest