Problems in second blocks message digest in self-learning SHA-1 algorithm - hash

Output
I am new in learning C programming. Now, I am trying to do SHA-1 for university project. I think this coding by myself. I am trying to do the message digest from the file above 55characters, which means 2 blocks is needed. The first block message digest is correct, but the second block is wrong. I have checked it very many times, but I still not able to find the mistake. Can anyone with experiences able to help me find it out? Thank you.
patients information.txt
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length,number_of_block,str_length;
unsigned int i = 0,j = 0,l = 0,e = 0,n = 0, t=0, k=0,f=0;
float x=0;
char c;
int H[5]={0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0};
unsigned int temp = 0;
FILE *file;
file = fopen("patients information.txt", "r");//Choose the file that want to access
if (file == NULL)//detect the file is empty or not
{
printf("The file is empty");
}
fseek(file, 0, SEEK_END);// move the file pointer to the end of the file
length = ftell(file);//calculate the length of sting in file
fseek(file, 0, SEEK_SET);// move file pointer back to start of file so we can read each character
printf("The length of string is %d\n",length);//check the number of character in string
char *string = malloc(sizeof(char) * (length+1));
while ( (c = fgetc(file)) != EOF)//pass the every character in the file to the string array
{
string[i] = c;
i++;
}
string[i] = '\0';//terminate the string storing
unsigned char long_msg[length+1];
for (i=0;i<length;i++)//pass the pointer array to the unsigned character array
{
long_msg[i]=string[i];
}
printf("The character store in th array is ");
long_msg[length]=128;
for (i=0;i<=length;i++)//check the message in msg array
{
printf("%X ",long_msg[i]);
}
if (length<=55)
{
number_of_block = 1;
}
else if (length>55 && length<120)
{
number_of_block = 2;
}
else
{
x = ((length - 55)/64);//calculate the number of block needed
number_of_block = x+2;
}
printf("\nNumber of block needed is %d\n",number_of_block);
unsigned char blocks[number_of_block][64];
for (i=0;i<number_of_block;i++)//Split the long string into n number of blocks
{
for(j=0;j<64;j++)
{
blocks[i][j]=long_msg[l];
if(l>length)//padding 0
{
blocks[i][j]=0;
}
l++;
}
}
for (i=0;i<number_of_block;i++)//check the blocks content
{
for(j=0;j<64;j++)
{
printf("%X ",blocks[i][j]);
}
}
printf("\nCheck length padding:\n");
str_length = 8*length;//sting length in bits
if (length<32)//if length of string is 1 bytes in hexadecimal
{
blocks[number_of_block-1][63]=str_length;
}
else
{
blocks[number_of_block-1][62]=(str_length>>8);//second last block
blocks[number_of_block-1][63]=((str_length<<8)>>8);//last block
}
for (i=0;i<number_of_block;i++)//check length padding
{
for(j=0;j<64;j++)
{
printf("%02X ",blocks[i][j]);
}
}
unsigned int w[number_of_block][16][4];
unsigned int W[number_of_block][80];
unsigned int A[number_of_block],B[number_of_block],C[number_of_block],D[number_of_block],E[number_of_block];
for (e=0;e<number_of_block;e++)
{
/*The problem is here*/
n=0;
for (i=0;i<16;i++)//split the padding message into w0 to w15 ,exp. w0 = (w[0][1]),....,(w[0][3])
{
for(j=0;j<4;j++)
{
w[e][i][j] = blocks[e][n];
n++;
}
}
for (i=0;i<16;i++)//combine the hex --> 16 block of hexadecimal(W0 to W15)
{
W[e][i] = ((w[e][i][0])<<24 | (w[e][i][1])<<16 | (w[e][i][2])<<8 | (w[e][i][3]));
}
/*Compute message digest*/
A[e] = 0x67452301;
B[e] = 0xEFCDAB89;
C[e] = 0x98BADCFE;
D[e] = 0x10325476;
E[e] = 0xC3D2E1F0;
for (t=0;t<=79;t++)
{
//for t = 16 -> 79
if (t>=16 && t<=79)//prepare W16 to W79
{
W[e][t]= ( (W[e][t-3]) ^ (W[e][t-8]) ^ (W[e][t-14]) ^ (W[e][t-16]) );
W[e][t]= ( ((W[e][t])<<1) | ((W[e][t]) >> (32-1)));//perform circular left shift
}
if (t>=0 && t<=19)
{
f = (B[e]&C[e]) | ((~B[e])&D[e]);
k = 0x5A827999;
}
else if (t>=20 && t<=39)
{
f = (B[e]^C[e]^D[e]);
k = 0x6ED9EBA1;
}
else if (t>=40 && t<=59)
{
f = (B[e]&C[e]) | (B[e]&D[e]) | (C[e]&D[e]);
k = 0x8F1BBCDC;
}
else if(t>=60 && t<=79)
{
f = (B[e]^C[e]^D[e]);
k = 0xCA62C1D6;
}
temp = ((A[e]<<5) | (A[e] >> (32-5))) + f + E[e] + W[e][t] + k;
E[e] = D[e];
D[e] = C[e];
C[e] = ( (B[e]<<30) | (B[e]>> (32-30)));
B[e] = A[e];
A[e] = temp;
}
printf("\n\n");
printf("%08X %08X %08X %08X %08X",A[e],B[e],C[e],D[e],E[e]);//check the value before adding up
H[0] = ( H[0] + A[e]);
H[1] = ( H[1] + B[e]);
H[2] = ( H[2] + C[e]);
H[3] = ( H[3] + D[e]);
H[4] = ( H[4] + E[e]);
}
printf("\n\n");
printf("Message digest:");
for (i=0;i<5;i++)
{
printf("%X ",H[i]);
}
printf("\n\n");
return 0;
}
The wrong output of second block : CE3A1FD0 01464A63 F6766B50 AF97AC62 8D5DBBDD
The output of second block should be: 906FD62C 58C0AAC0 B6A55520 74E9B89D 9AF00B7F

Related

In Flutter and if the number after decimal point is equal 0 convert the number to int

This is a function if the endValueFixed is equal for example 12.0 I want to print the number without zero so I want it to be 12.
void calculateIncrease() {
setState(() {
primerResult = (startingValue * percentage) / 100;
endValue = startingValue + primerResult;
endValueFixe`enter code here`d = roundDouble(endValue, 2);
});
}
This may be an overkill but it works exactly as you wish:
void main() {
// This is your double value
final end = 98.04;
String intPart = "";
String doublePart = "";
int j = 0;
for (int i = 0; i < end.toString().length; i++) {
if (end.toString()[i] != '.') {
intPart += end.toString()[i];
} else {
j = i + 1;
break;
}
}
for (int l = j; l < end.toString().length; l++) {
doublePart += end.toString()[l];
}
if (doublePart[0] == "0" && doublePart[1] != "0") {
print(end);
} else {
print(end.toString());
}
}
You may use this code as a function and send whatever value to end.
if (endValueFixed==12) {
print('${endValueFixed.toInt()}');
}
conditionally cast it to an int and print it then :)

Convert std::string from UTF8, UTF16, ISO88591 to Hexadecimal

I try to Encoding the std::string from UTF8,... to Hexadecimal. What I can't do right now is that I can't get the decimal value of each character of the input string to convert it if the input string contains special character which is from the Code Page Identifiers(windows-1258) include Vietnamese character.
At first, I will get the decimal value and then convert it to Binary and then to Hexadecimal. s is my input string. s = "Ồ".
void StringUtils::strToBinary(wstring s, string* result)
{
int n = s.length();
for (int i = 0; i < n; i++)
{
wchar_t c = s[i];
long val = long(c);
std::string bin = "";
while (val > 0)
{
(val % 2) ? bin.push_back('1') :
bin.push_back('0');
val /= 2;
}
reverse(bin.begin(), bin.end());
result->append(convertBinToHex(bin));
}
}
std::string StringUtils::convertBinToHex(std::string temp) {
long long binaryValue = atoll(temp.c_str());
long long dec_value = 0;
int base = 1;
int i = 0;
while (binaryValue) {
long long last_digit = binaryValue % 10;
binaryValue = binaryValue / 10;
dec_value += last_digit * base;
base = base * 2;
}
char hexaDeciNum[10];
while (dec_value != 0)
{
int temp = 0;
temp = dec_value % 16;
if (temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
dec_value = dec_value / 16;
}
std::string str;
for (int j = i - 1; j >= 0; j--) {
str = str + hexaDeciNum[j];
}
return str;
}
If my input only contain "Ồ" this is what my expected output
UTF8 : E1BB92
UTF16 : FEFF 1ED2
UTF16BE : 1ED2
UTF16LE : D21E
This how I do it in Java
Charset charset = Charset.forName(Enum.valueOf(Encoding.class, encodingType).toString());
ByteBuffer buffer = charset.newEncoder().encode(CharBuffer.wrap(inputString.toCharArray()));
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes, 0, buffer.limit());
result = new ByteField(bytes);
return result;
}

Sending messages through LoRaWAN using STM32

How do I send the messages through LoRaWAN?
static void PrepareTxFrame( uint8_t port )
{
switch( port ) {
case 10: {
int pos = 0;
pc.printf("Prepare message\n");
#if 0
uint32_t tempValue = ( uint32_t )( LightValue * 1000000.0 );
AppData[0] = LightMode;
AppData[1] = ( ( tempValue & 0xFF000000 ) >> 24 ) & 0xFF;
AppData[2] = ( ( tempValue & 0x00FF0000 ) >> 16 ) & 0xFF;
AppData[3] = ( ( tempValue & 0x0000FF00 ) >> 8 ) & 0xFF;
AppData[4] = ( tempValue & 0x000000FF );
#else
AppData[pos] = count;
pc.printf("\n\r");
pc.printf("The value of the counter is : %d", count);
count++;
pc.printf("\n\r");
time_t seconds = time(NULL);
printf("The time is %s", ctime(&seconds));
AppData[++pos] = seconds;
pc.printf("%d \n %d", AppData[0], AppData[1]);
pc.printf("\n\r");
#endif
pc.printf("Message Ready\n");
}
break;
case 15: {
int pos = 0;
AppData[pos++] = AppLedStateOn;
#if 0
if( IsTxConfirmed == true )
{
AppData[pos++] = LoRaMacDownlinkStatus.DownlinkCounter >> 8;
AppData[pos++] = LoRaMacDownlinkStatus.DownlinkCounter;
AppData[pos++] = LoRaMacDownlinkStatus.Rssi >> 8;
AppData[pos++] = LoRaMacDownlinkStatus.Rssi;
AppData[pos++] = LoRaMacDownlinkStatus.Snr;
}
#endif
AppDataSize = pos;
}
break;
case 224:
if( ComplianceTest.LinkCheck == true ) {
ComplianceTest.LinkCheck = false;
AppDataSize = 3;
AppData[0] = 5;
AppData[1] = ComplianceTest.DemodMargin;
AppData[2] = ComplianceTest.NbGateways;
ComplianceTest.State = 1;
} else {
switch( ComplianceTest.State ) {
case 4:
ComplianceTest.State = 1;
break;
case 1:
AppDataSize = 2;
AppData[0] = ComplianceTest.DownLinkCounter >> 8;
AppData[1] = ComplianceTest.DownLinkCounter;
break;
}
}
break;
default:
break;
}
}
/*!
* \brief
*
* Prepares the pay-load of the frame
*
* \retval [0: frame could be send, 1: error]
*/
static bool SendFrame( void )
{
McpsReq_t mcpsReq;
LoRaMacTxInfo_t txInfo;
if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
{
// Send empty frame in order to flush MAC commands
mcpsReq.Type = MCPS_UNCONFIRMED;
mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
LoRaMacUplinkStatus.Acked = false;
LoRaMacUplinkStatus.Port = 0;
LoRaMacUplinkStatus.Buffer = NULL;
LoRaMacUplinkStatus.BufferSize = 0;
SerialDisplayUpdateFrameType( false );
} else {
LoRaMacUplinkStatus.Acked = false;
LoRaMacUplinkStatus.Port = AppPort;
LoRaMacUplinkStatus.Buffer = AppData;
LoRaMacUplinkStatus.BufferSize = AppDataSize;
SerialDisplayUpdateFrameType( IsTxConfirmed );
if( IsTxConfirmed == false ) {
mcpsReq.Type = MCPS_UNCONFIRMED;
mcpsReq.Req.Unconfirmed.fPort = AppPort;
mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
} else {
mcpsReq.Type = MCPS_CONFIRMED;
mcpsReq.Req.Confirmed.fPort = AppPort;
mcpsReq.Req.Confirmed.fBuffer = AppData;
mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mcpsReq.Req.Confirmed.NbTrials = 8;
mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
}
}
if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) {
return false;
}
return true;
}
Will the counter data and the time be sent? Also is the data in AppData the ones to be transmitted? I want the count and the timestamp to be sent every time the LoRa device transmits.
The payload to send (AppData) is given to the LoRaMAC layer through the MCPS (MAC Common Part Sublayer) request, e.g. for an unconfirmed frame:
mcpsReq.Req.Unconfirmed.fBuffer = AppData;
So physically (i.e. by RF), AppData is sent but it's encrypted and encapsulated before.
PrepareFrame() function builds the frame to send according to the PHYPayload scheme (See the "MAC Message Formats" part in the LoRaWAN™ Specification 1.0.2 document), following these fields:
MHDR (1 byte) Mac header
DevAddr (4 bytes) Address of the end-device
FCtrl (1 byte) Frame control
FCnt (2 bytes) Frame counter
FOpts (0 - 15 bytes) Frame options
FPort (0 - 1 byte) Port field
FRMPayload (0 - N bytes) MAC Frame Payload Encryption, your AppData encrypted
MIC (4 bytes) Message Integrity Code
FRMPayload is encrypted according to FPort. The encryption algorithm is based on AES 128.
If FPort = [1..255], the AppSKey key will be used to encrypt your payload.
Else (FPort = 0), it's encrypted by using the NwkSKey key.
See the LoRaMacPayloadEncrypt() function for more details.
PHYPayload will be encapsulated by the Radio PHY Layer and sent through RF.

least recently used algorithm in C for operating system

I made this code, however this shows a different value for page faults. Please help me.
time is an array which will store the number of times a page is referenced. And "foo" is a function designed to return the minimum time one page in the frame list.
#include <stdio.h>
int p,i,b,a[50],f[30],counter,fault;
int time[10] = {0};
int foo (void);
int main (void)
{
int min,flag,j;
printf("Enter the number of frames\n");
scanf("%d",&b);
for ( i = 0; i < b; i++ )
f[i] = -1;
printf("Enter the number of pages\n");
scanf("%d",&p);
printf("Enter the pages\n");
for ( i = 0; i < p; i++ )
scanf("%d",&a[i]);
fault = 0, counter = 0;
for (i = 0; i < p; i++)
{
flag = 0;
if ( i < b )
{
f[i] = a[i];
fault++;
time[i]++;
}
else
{
for ( j = 0; j < b; j++ )
{
if ( a[i] == f[j] )
{
flag = 1;
time[j]++;
}
}
if ( flag == 0 )
{
min = foo();
printf("The page replaced for %d page is at pos %d\n",i,min);
f[min] = a[i];
fault++;
time[min]++;
}
}
}
printf("The number of page faults are %d\n",fault);
return 0;
}
int foo (void)
{
int z,bar1, bar2;
bar1 = time[0];
bar2 = 0;
for ( z = 1; z < b; z++ )
{
if ( time[z] < bar1 )
{
bar1 = time[z];
bar2 = z;
}
}
return bar2;
}

How to determine if a string can be manipulated to be rewritten as a palindrome?

I believe this can be achieved by counting the instances for each character in that string. Even if a single character in that string is repeated at least twice, we can declare that string as a palindrome.
For example: bbcccc can be rewritten as bccccb or ccbbcc.
edified can be rewritten as deified.
Some book mentioned we should be using hash table. I think we can just use a list and check for the character count.
Do you think the logic is correct?
Yes, the main idea is to count the times of each char existing in the string. And it will be true if the string has at most one char occurs odd times and all others even times.
For example:
aabbcc => acbbca
aabcc => acbca
aabbb => abbba
No. You don't have to use a hash map (as some of the other answers suggest). But the efficiency of the solution will be determined by the algorithm you use.
Here is a solution that only tracks odd characters. If we get 2 odds, we know it can't be a scrambled palindrome. I use an array to track the odd count. I reuse the array index 0 over and over until I find an odd. Then I use array index 1. If I find 2 odds, return false!
Solution without a hash map in javascript:
function isScrambledPalindrome(input) {
// TODO: Add error handling code.
var a = input.split("").sort();
var char, nextChar = "";
var charCount = [ 0 ];
var charIdx = 0;
for ( var i = 0; i < a.length; ++i) {
char = a[i];
nextChar = a[i + 1] || "";
charCount[charIdx]++;
if (char !== nextChar) {
if (charCount[charIdx] % 2 === 1) {
if (charCount.length > 1) {
// A scrambled palindrome can only have 1 odd char count.
return false;
}
charIdx = 1;
charCount.push(0);
} else if (charCount[charIdx] % 2 === 0) {
charCount[charIdx] = 0;
}
}
}
return true;
}
console.log("abc: " + isScrambledPalindrome("abc")); // false
console.log("aabbcd: " + isScrambledPalindrome("aabbcd")); // false
console.log("aabbb: " + isScrambledPalindrome("aabbb")); // true
console.log("a: " + isScrambledPalindrome("a")); // true
Using a hash map, I found a cool way to only track the odd character counts and still determine the answer.
Fun javascript hash map solution:
function isScrambledPalindrome( input ) {
var chars = {};
input.split("").forEach(function(char) {
if (chars[char]) {
delete chars[char]
} else {
chars[char] = "odd" }
});
return (Object.keys(chars).length <= 1);
}
isScrambledPalindrome("aba"); // true
isScrambledPalindrome("abba"); // true
isScrambledPalindrome("abca"); // false
Any string can be palindrome only if at most one character occur odd no. of times and all other characters must occur even number of times.
The following program can be used to check whether a palindrome can be string or not.
vector<int> vec(256,0); //Vector for all ASCII characters present.
for(int i=0;i<s.length();++i)
{
vec[s[i]-'a']++;
}
int odd_count=0,flag=0;
for(int i=0;i<vec.size();++i)
{
if(vec[i]%2!=0)
odd_count++;
if(odd_count>1)
{
flag=1;
cout<<"Can't be palindrome"<<endl;
break;
}
}
if(flag==0)
cout<<"Yes can be palindrome"<<endl;
My code check if can it is palindrome or can be manipulated to Palindrome
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
//Tested on windows 64 bit arhc by using cygwin64 and GCC
bool isPalindrome (char *text);
int main()
{
char text[100]; // it could be N with defining N
bool isPal,isPosPal = false;
printf("Give me a string to test if it is Anagram of Palindrome\n");
gets(text);
isPal = isPalindrome(text);
isPosPal = isAnagramOfPalindrome(text);
if(isPal == false)
{
printf("Not a palindrome.\n");
}
else
{
printf("Palindrome.\n");
}
if(isPosPal == false)
{
printf("Not Anagram of Palindrome\n");
}
else
{
printf("Anagram of Palindrome\n");
}
return 0;
}
bool isPalindrome (char *text) {
int begin, middle, end, length = 0;
length = getLength(text);
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
return false;
}
end--;
}
if (begin == middle)
return true;
}
int getLength (char *text) {
int length = 0;
while (text[length] != '\0')
length++;
printf("length: %d\n",length);
return length;
}
int isAnagramOfPalindrome (char *text) {
int length = getLength(text);
int i = 0,j=0;
bool arr[26] = {false};
int counter = 0;
//char string[100]="neveroddoreven";
int a;
for (i = 0; i < length; i++)
{
a = text[i];
a = a-97;
if(arr[a])
{
arr[a] = false;
}
else
{
arr[a] = true;
}
}
for(j = 0; j < 27 ; j++)
{
if (arr[a] == true)
{
counter++;
}
}
printf("counter: %d\n",counter);
if(counter > 1)
{
return false;
}
else if(counter == 1)
{
if(length % 2 == 0)
return false;
else
return true;
}
else if(counter == 0)
{
return true;
}
}
as others have posted, the idea is to have each character occur an even number of times for an even length string, and one character an odd number of times for an odd length string.
The reason the books suggest using a hash table is due to execution time. It is an O(1) operation to insert into / retrieve from a hash map. Yes a list can be used but the execution time will be slightly slower as the sorting of the list will be O(N log N) time.
Pseudo code for a list implementation would be:
sortedList = unsortedList.sort;
bool oddCharFound = false;
//if language does not permit nullable char then initialise
//current char to first element, initialise count to 1 and loop from i=1
currentChar = null;
currentCharCount = 0;
for (int i=0; i <= sortedList.Length; i++) //start from first element go one past end of list
{
if(i == sortedList.Length
|| sortedList[i] != currentChar)
{
if(currentCharCount % 2 = 1)
{
//check if breaks rule
if((sortedList.Length % 2 = 1 && oddCharFound)
|| oddCharFound)
{
return false;
}
else
{
oddCharFound = true;
}
}
if(i!= sortedList.Length)
{
currentCharCount = 1;
currentChar = sortedList[i];
}
}
else
{
currentCharCount++;
}
}
return true;
Here is a simple solution using an array; no sort needed
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[256] = { 0 };
unsigned char i[] = {"aaBcBccc"};
unsigned char *p = &i[0];
int c = 0;
int j;
int flag = 0;
while (*p != 0)
{
a[*p]++;
p++;
}
for(j=0; j<256; j++)
{
if(a[j] & 1)
{
c++;
if(c > 1)
{
flag = 1;
break;
}
}
}
if(flag)
printf("Nope\n");
else
printf("yup\n");
return 0;
}
C#:
bool ok = s.GroupBy(c => c).Select(g => g.Count()).Where(c => c == 1).Count() < 2;
This solution, however, does use hashing.
Assuming all input characters are lower case letters.
#include<stdio.h>
int main(){
char *str;
char arr[27];
int j;
int a;
j = 0;
printf("Enter the string : ");
scanf("%s", str);
while (*str != '\0'){
a = *str;
a = a%27;
if(arr[a] == *str){
arr[a]=0;
j--;
}else{
arr[a] = *str;
j++;
}
*str++;
}
if(j==0 || j== -1 || j==1){
printf ("\nThe string can be a palindrome\n");
}
}