Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Toybox.Lang;
- import Toybox.Math;
- import Toybox.System;
- import Toybox.Cryptography;
- import Toybox.Time;
- module Cryptography2 {
- static const _opad = [0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
- 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
- 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
- 0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C]b as ByteArray;
- static const _ipad = [0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
- 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
- 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
- 0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36]b as ByteArray;
- function HMAC_SHA1(secretKey as ByteArray, message as ByteArray) as ByteArray {
- var _key = new [0]b;
- var _sha1Hash = new Cryptography.Hash({:algorithm => Cryptography.HASH_SHA1});
- if(secretKey.size() < 64) { //sh1a blocksize
- var _pad = new [64-secretKey.size()]b;
- _key.addAll(secretKey); //deep copy secretKey to _key to prevent mutation
- _key.addAll(_pad); //if less than blocksize pad the key with zeros to the blocksize
- }else{
- _sha1Hash.update(secretKey);
- _key = _sha1Hash.digest(); //if key is greater than blocksize hashed with SHA1
- }
- //xor key with ipad then concatonate with messsage, then hash to calculate inner hash
- var _ipadKey = byteArrayXOR(_ipad , _key,{:endianness => ENDIAN_BIG});
- _ipadKey.addAll(message);
- _sha1Hash.update(_ipadKey);
- var _innerHash = _sha1Hash.digest();
- //xor key with opad then concatonate with inner hash
- var _opadKey = byteArrayXOR(_opad , _key,{:endianness => ENDIAN_BIG});
- _opadKey.addAll(_innerHash);
- _sha1Hash.update(_opadKey);
- return _sha1Hash.digest();
- }
- function byteArrayXOR(byteArray1 as ByteArray, byteArray2 as ByteArray, options as {:endianness as Endian}) as ByteArray{
- //for little edianess
- //find byte array sizes
- var _result = new [0]b;
- _result.addAll(byteArray1.size() > byteArray2.size() ? byteArray1 : byteArray2);
- if(options[:endianness] == ENDIAN_BIG){
- if(byteArray1.size() > byteArray2.size()){
- for(var i = 0; i < byteArray2.size(); i++){
- _result[i] ^= byteArray2[i];
- }
- }else{
- for(var i = 0;i < byteArray1.size();i++){
- _result[i] ^= byteArray1[i];
- }
- }
- }else{
- if(byteArray1.size() > byteArray2.size()){
- var _offset = byteArray1.size() - byteArray2.size(); //cacluate the start of the array pos to xor
- for(var i = 0; i < byteArray2.size(); i++){
- _result[i+_offset] ^= byteArray2[i];
- }
- }else{
- var _offset = byteArray2.size() - byteArray1.size(); //cacluate the start of the array pos to xor
- for(var i = 0;i < byteArray1.size();i++){
- _result[i+_offset] ^= byteArray1[i];
- }
- }
- }
- return _result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment