Guest User

Monkey C HMAC SHA1

a guest
Nov 10th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. import Toybox.Lang;
  2. import Toybox.Math;
  3. import Toybox.System;
  4. import Toybox.Cryptography;
  5. import Toybox.Time;
  6.  
  7. module  Cryptography2 {
  8.     static const _opad =    [0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
  9.                             0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
  10.                             0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,
  11.                             0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C,0x5C]b as ByteArray;
  12.     static const _ipad =    [0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  13.                             0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  14.                             0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,
  15.                             0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36]b as ByteArray;                        
  16.  
  17.     function HMAC_SHA1(secretKey as ByteArray, message as ByteArray) as ByteArray {
  18.         var _key = new [0]b;
  19.         var _sha1Hash = new Cryptography.Hash({:algorithm => Cryptography.HASH_SHA1});
  20.         if(secretKey.size() < 64) { //sh1a blocksize
  21.             var _pad = new [64-secretKey.size()]b;
  22.             _key.addAll(secretKey); //deep copy secretKey to _key to prevent mutation
  23.             _key.addAll(_pad); //if less than blocksize pad the key with zeros to the blocksize
  24.         }else{
  25.             _sha1Hash.update(secretKey);
  26.             _key = _sha1Hash.digest(); //if key is greater than blocksize hashed with SHA1
  27.         }
  28.         //xor key with ipad then concatonate with messsage, then hash to calculate inner hash
  29.         var _ipadKey = byteArrayXOR(_ipad , _key,{:endianness => ENDIAN_BIG});
  30.         _ipadKey.addAll(message);
  31.         _sha1Hash.update(_ipadKey);
  32.         var _innerHash = _sha1Hash.digest();
  33.  
  34.         //xor key with opad then concatonate with inner hash
  35.         var _opadKey = byteArrayXOR(_opad , _key,{:endianness => ENDIAN_BIG});
  36.         _opadKey.addAll(_innerHash);
  37.         _sha1Hash.update(_opadKey);
  38.         return _sha1Hash.digest();
  39.     }
  40.  
  41.     function byteArrayXOR(byteArray1 as ByteArray, byteArray2 as ByteArray, options as {:endianness as Endian}) as ByteArray{
  42.         //for little edianess
  43.         //find byte array sizes
  44.         var _result = new [0]b;
  45.         _result.addAll(byteArray1.size() > byteArray2.size() ? byteArray1 : byteArray2);
  46.  
  47.         if(options[:endianness] == ENDIAN_BIG){
  48.             if(byteArray1.size() > byteArray2.size()){
  49.                 for(var i = 0; i < byteArray2.size(); i++){
  50.                     _result[i] ^= byteArray2[i];
  51.                 }
  52.             }else{
  53.                 for(var i = 0;i < byteArray1.size();i++){
  54.                     _result[i] ^= byteArray1[i];
  55.                 }
  56.             }
  57.         }else{
  58.             if(byteArray1.size() > byteArray2.size()){
  59.                 var _offset = byteArray1.size() - byteArray2.size(); //cacluate the start of the array pos to xor
  60.                 for(var i = 0; i < byteArray2.size(); i++){
  61.                     _result[i+_offset] ^= byteArray2[i];
  62.                 }
  63.             }else{
  64.                 var _offset = byteArray2.size() - byteArray1.size(); //cacluate the start of the array pos to xor
  65.                 for(var i = 0;i < byteArray1.size();i++){
  66.                     _result[i+_offset] ^= byteArray1[i];
  67.                 }
  68.             }
  69.         }
  70.         return _result;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment