Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

This is my first post in this forum. Can someone please help me to understand this question (little simple explanation about this question in easy understandable English) and find out the solution ? Thanks for all your help in advance.

Question:

Find out the input string that was passed to the following hash function
C#
Integer Hash (String inputString)
     Integer outputNumber = 7;
     String letters = "acdegilmnoprstuw";
     Integer index = 0;
     Integer lengthOfInputString = inputString.Length;
     while (index < lengthOfInputString)
     {
          outputNumber = (outputNumber* 37+ letters.IndexOf (inputString[index]));
     index = index + 1;
     }
     return outputNumber;
}

That produced the hashed output (integer value) as
945901726134069
Provided, the input string length was 9 and contains only characters
from the below string
"acdegilmnoprstuw"
Note: You need to reverse engineer the above hash logic and find out the
answer.



HELP:
String - a data type that holds collection of characters, an array of
characters with zero- based index. Hash - Modify an input value to a
different one using which one cannot easily find out the original value.
Dehash - Find the original input value that was modified by hashing.
Function - a code snippet that accepts zero or more number of input
parameters and return either no value or a value. Integer - a data type
that holds both positive and negative full numbers.
<string_variable>.IndexOf - an in-built function in any programming
language that finds out the starting index of any substring (one or more
characters together) inside the value stored in string_variable.
<string_variable>[<integer_variable>] - an expression that returns the
character at the-index specified by the value of integer_variable

What I have tried:

I have little knowledge on VBA and C# is completely new for me hence seeking advice from experts.
Posted
Updated 3-Mar-23 22:22pm
v2
Comments
Mike Hankey 3-Mar-23 12:21pm    
We don't answer homework questions without some effort.
Where are you stuck?
What have you tried?
Ranjith Kumar 2023 3-Mar-23 14:08pm    
Mr. Mike Hankey, Thanks for your reply. Please don't do any HOMEWORK behalf of me. I clearly told C# is completely new for me. I just wanted to understand the question and what is happening in this coding. I just need a clear explanation of the question. My effort is I break my head and I understand in order to get the solution we need to write the coding in reverse order.
[no name] 3-Mar-23 13:25pm    
you have to 'reverse' the code.
so start by the end ... until the first line..
Ranjith Kumar 2023 3-Mar-23 14:05pm    
Thanks for your kind reply. Actually, I am sorry to say this. I don't understand the question itself. What is happening in this coding and based on your reply in order to get the solution we need to reverse the code.
You don't need to write the coding and all just help me to understand what is happening in this code.
Mike Hankey 3-Mar-23 14:19pm    
If you don't understand the problem you need to ask your professor for help.

With these sorts of problems it is often helpful to start with a simple example. The basic equation is of the form


C#
47=(5*8)+7;
 //or
945901726134069=(n*37)+i;

You are given 47 and 8 and you need to find 5 and 7 when you are told that i has a maximum index value of 8 , the range of i being 0-8. Play around with different operations on the given values and see if you can come up with a solution that applies for all possible values of i. As you are new to C#, it may be helpful to know that 945901726134069 can be represented by the type ulong and that integer arithmetic applies. So the fractional part of an integer division is ignored. I would further suggest that you store the found char type letter values in a buffer of type char[9] and, when the array is full, convert it to a string.


C#
string result= new string[buffer].

Good luck!

 
Share this answer
 
Quote:
Note: You need to reverse engineer the above hash logic and find out the answer.

You are requested to study the sample code provided, to understand how it works.
Quote:
That produced the hashed output (integer value) as
945901726134069

Then as illustration of your new knowledge, find the input string that will give the requested output.
2 Answers:
- you failed the reverse engineering: you need to try every single input until you find the giving the requested output.
- You understand how the result is built by the hash function: apply your understanding to rebuild the input. "To get a particular output (or part of), I need this input ..."
 
Share this answer
 
Hash("aaaaaaaaa") tells you the min (for 9 letters); Hash("wwwwwwwww") is the max.

945901726134069 lies in between; and tells you it's using 9 letters.

Converging on 9459 .... gives Hash("ppppppppp").

From there, you converge, letter by letter, to (wait for it): "pramodena".
 
Share this answer
 
v2
This is a problem that is intended to get you thinking: you have a piece of code and it's output value.
You task is to work out what the input was.

That requires you to work the code backwards: start with the output value and think back through the code.
The important line is this one:
outputNumber = (outputNumber* 37+ letters.IndexOf (inputString[index]));
WHich has two halves separated by a + sign: the part on the left means that each digit in the input occupies 1/37th of the output.

So take your output and get the remainder when divided by 37. Then work out what input value gave you that number. That's the last letter of the input string.

Repeat the process and you'll get the original value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900