Pure Lua md5 using a byte array as input instead of string - hash

I've been working in an algorithm that I found in this link: Pure Lua implementation of md5 to use in an embedded system, but it uses strings as input.
For my specific application, the hash need to receive an hex array like that: 85202599ee7e0165ee32be43336755595955544554554747.
How can I change the function bellow to calculate the hash using the array above?
function md5.Calc(s)
local msgLen=string.len(s)
local padLen=56- msgLen % 64
if msgLen % 64 > 56 then padLen=padLen+64 end
if padLen==0 then padLen=64 end
s=s..string.char(128)..string.rep(string.char(0),padLen-1)
s=s..leIstr(8*msgLen)..leIstr(0)
assert(string.len(s) % 64 ==0)
local t=md5.consts
local a,b,c,d=t[65],t[66],t[67],t[68]
for i=1,string.len(s),64 do
local X=leStrCuts(string.sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
assert(#X==16)
X[0]=table.remove(X,1) -- zero based!
a,b,c,d=md5.transform(a,b,c,d,X)
end
local swap=function (w) return beInt(leIstr(w)) end
return string.format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d))
end
return md5.Calc("85202599ee7e0165ee32be43336755595955544554554747"); -- returns: cc238dfd3cf48d588714774efeaf9a1f
-- but I need a return like that calculated in https://cryptii.com/pipes/md5-hash: c7e9f863554dc9a410c414f8758b307d

Lua strings can hold binary data: each "character" then is one byte. This is what is being used in above code. You can see that characters 128 (0x80 or 0b10000000) and zero are used for the padding.
So you should not change the above code at all; instead you should decode the hexadecimal string to a binary string and use that as input to above function.
You can find an interesting, concise method of hex decoding here.

Related

Work with binary numbers as scalars in Matlab

I am working with a MATLAB function that uses numbers in the binary base. To do so it uses the function dec2bin to transform an integer into a char array containing the binary information. The issue is that I plan to use HDL Coder to generate a HDL version of the function. One step of the process is to convert the variables to fixed point. This can be done automatically when the data is a scalar, so is there any way to manage binary numbers without using vectors?
dec2bin is just for display purposes. Numbers are always stored in the computer using binary representation. You can use the functions
bitand,
bitor,
bitxor,
bitcmp,
bitshift,
bitget, and
bitset
to do bit-wise manipulation of integer numbers:
>> a = uint32(7);
>> b = uint32(12);
>> bitand(a, b)
ans =
uint32
4
(Click on the function names above for the documentation. You can also do help bitand in MATLAB to read a shorter version of the documentation or doc bitand to read the full documentation.)

Why can't MATLAB save uint8 type matrix?

Here is the code:
x = rand(5)*100;
save('pqfile.txt','x','-ascii','-tabs')
The above works, but:
x = rand(5)*100;
x = uint8(x);
save('pqfile.txt','x','-ascii','-tabs')
says:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'x' not written to file.
Does anyone know why this happens? How come I can't save the data when it is uint8. I have to read data into a VHDL testbench so was experimenting. I guess the only option is to save my 8 bit unsigned integer values in 2d array using printf then read into the test bench.
ASCII option
The save method is somewhat restrictive in what it can support, and then it uses floating point notation to represent your numbers which bloats your file when dealing with a limited range of numbers like you are (i.e. uint8, 0 to 255).
Check out dlmwrite as an alternative (documentation here).
It takes the filename to write/save to, the variable to store, and some additional parameters, like the delimiter you want to separate your values with.
For your example, it looks like this
x = rand(5)*100;
x = uint8(x);
dlmwrite('pqfile.txt',x,'\t');
Binary option
If you are looking to stored your uint8 data as single bytes then you probably want go with a custom binary file instead instead of ASCII. (Yes, you can convert uint8 to single ASCII characters but you run into issues with these values being interpreted with your delimiters; newlines or tabs.)
fid=fopen('pqfile.dat','wb');
if(fid>2)
fwrite(fid,size(x),'*uint8'); % Note: change data type here you are dealing with more than 255 rows or columns
fwrite(fid,x','*uint8'); % Transpose x (with x') so it is stored in row order.
fclose(fid);
else
fprintf(1,'Could not open the file for writing.\n');
end
I'm not sure what type of parser you are using for your VHDL, but this will pack your data into a file with a short header of the expected dimensions followed by one long row of your serialized data.
To read it back in with MATLAB, you can do this:
fid = fopen('pqfile.dat','rb');
szX = fread(fid,2,'uint8');
x = fread(fid,szX,'*uint8')'; % transpose back if you are dealing with matlab.
fclose(fid);
The transpose operations are necessary for MATLAB because it reads data column-wise, whereas most other languages (in my experience) read row-wise.

marshalling arbitrary-length binary data from udp() object

I am reading binary data from instrumentation using the Matlab udp() object.
I am surprised by the apparent lack of support for reading arbitrary length data types. How does one read a 24-bit integer? Or a 24-bit float? These are not that strange in instrumentation, and I have found only 8/16/32/64 data types in the documentation.
Have you looked tried help fread? The documentation shows it supports reading up to 64 bits at a time using bitN where N is a value between 1 and 64.
fid = udp(<your parameters here>); % use fopen to open the stream.
...
A = fread(fid,1,'bit24=>int32'); % stream 24 bits to a 32 bit integer.
B = fread(fid,1,'ubit24=>uint32'); % stream 24 bits to a 32 bit unsigned integer.
Since floating point specs vary, so this may or may not work for your situation:
C = fread(fid,1,'bit24=>float32'); % transcode 24bits to 32 bit float (MATLAB spec)
UPDATE
Seeing that the udp/fread implementation does not support this casting there are a couple, not-so-pretty, workarounds you can try.
Read in uchar data in multiples of three and then multiply it by their byte offsets directly. For example:
% First determine number of bytes on the stream and make sure you
% have at 3 or more bytes to read so you can calculate thirdOfBytesExpected.
[anMx3result, packetCount] = fread(fid,[thirdOfBytesExpected,3]);
unsigned20bitInt = anMx3result*(2.^(0:8:16))';
To be precise, the unsigned20bitInt is actually stored as a MATLAB double here. So if you need to write it elsewhere, you will need to bring it back to the individual uchar types it came from.
The not so pretty option is to eat the overhead of streaming the data back to a binary file format as an interim step so that you can then use the base fread method mentioned above. Not an ideal solution, but perhaps worth considering if you just need something to work.
% your original code for opening the udp handle
....
tmpFid = fopen('tmp.bin','rw');
[ucharVec, bytesRead] = fread(udpFid,bytesExpected,'uchar=>uchar');
bytesWritten = fwrite(tmpFid,ucharVec,'uchar');
% Do some quality control on bytes read vs written ...
fseek(tmpFid,-bytesWritten,'cof');
% in theory you should be able to just go to the beginning
% of the file each time like this fseek(tmpFid, 0, 'bof');
% provided you also reset to the beginning prior writing or after reading
% Read in the data as described originally
num24ByteIntsToRead = bytesWritten/3;
A = fread(tmpFid,num24BytsIntsToRead,'bit24=>int32');

how to correctly multiply 2 matrix? [duplicate]

I have this problem that has been bothering me for quite a while.. I want to change the format of the number.. Don`t know how? Ive tried the help files but can't find the answer.. If you can help me please do..
There are three different things you could potentially be referring to when you talk about the "format" of a number: the display format, the variable storage format in memory (i.e. the data type/class), and the storage format in a data file. I'll address each...
Display format: As already mentioned by Amro, the display format can be adjusted with the FORMAT command. However, this only affects how the numbers are displayed, not how they are stored in memory.
Variable types/classes: There are a number of numeric classes, both floating point and signed/unsigned integer types, that variables can use. By default, MATLAB variables are stored as double-precision floating point numbers. To convert to other types, you can use the variable type as a function to recast a value. For example, a = uint8(0); converts 0 to an 8-bit unsigned integer type and stores it in a, while b = single(pi); converts the value pi into single-precision and stores it in b.
File storage format: When reading/writing numeric values to files, the type of file affects how it will be stored. A binary file (written to and read from using FWRITE and FREAD) stores the complete binary representation of a number, and can thus represent it as exactly the same type as it is stored in memory as a variable (double-precision, single-precision, 8-bit integer, etc.).
Alternatively, a text file (written to and read from using FPRINTF and FSCANF, among other functions) will store a value in a string format that you must specify when outputting the values to the file. You can specify the digits of precision, as well as the format (such as exponential notation or hexadecimal). The documentation for FPRINTF specifies these output formats.
Use the format command
Example:
format long; pi
3.141592653589793
format short e; pi
3.1416e+000
format short g; pi
3.1416
sprintf('%.2f', 1.4795e4);
(in particular: if you want it displayed/saved/printed a certain way, be explicit about it!)

Retrieve Text From ASCII In Matlab

I have the following lines of code :
%for RSA Algorithm
clc;
disp('Implementation of RSA Algorithm');
clear all;
close all;
p = input('\nEnter value of p: ');
q = input('\nEnter value of q: ');
[Pk,Phi,d,e] = init(p,q);
M = input('\nEnter message: ','s');
x=length(M);
c=0;
for j= 1:x
for i=0:122
if strcmp(M(j),char(i))
c(j)=i;
end
end
end
disp('ASCII Code of the entered Message:');
disp(c);
% For Encryption
for j= 1:x
cipher(j)= crypto(c(j),Pk,e);
end
disp('Cipher Text of the entered Message:');
disp(cipher);
How can I know the text of encrypted message ? I mean what are the characters of the encryption ? I want the ASCII code of cipher to be converted back into characters. Can someone tell me the matlab code for this ?
Modular exponentiation - the main mathematical primitive used for RSA encryption/decryption uses integer values as input and output. The RSA algorithms themselves (in PKCS#1 v2.1 onwards) first convert messages to octets, then pads these objects according to one of two padding schemes, then perform modular exponentiation on the result converted to integer. The resulting integer in turn is converted to octets again.
Now depending on the actually performed calculation described above, you should have either an integer or octets. As it is not hard to convert an integer to octets - or to print an integer - lets focus only on octets. You need to create a string (text) from something that is inherently not text. There are specific encoding schemes that do this, but the most common one is base 64.
Note that usually you only perform one RSA encryption per plaintext. If the plaintext does not fit (or may not fit in the future, or if you just want to adhere to standards) then you should use hybrid encryption instead of RSA "in ECB mode".