Problem Statement
It is typically not a good idea to store customer passwords in a database without any encryption. While more secure algorithms, like RSA, can be used to encrypt passwords, often a simple hash function will suffice.
An additional concern when using any hash function is that someone might simply run the hash function on every word in a dictionary to figure out someone's password. The simplest way around this is to use a secret hash function that very few people have access to. In this problem, your task is to implement such a hash function to the following specification:
First, you will be given an unencrypted password as a String. For example, let's start with "password".
Next, you will add another copy of the first and last characters, so "password" becomes "ppasswordd".
Third, you will reverse the String, so "ppasswordd" becomes "ddrowssapp".
Finally, call the hashCode() method on the resulting String object, and return the result.
An additional concern when using any hash function is that someone might simply run the hash function on every word in a dictionary to figure out someone's password. The simplest way around this is to use a secret hash function that very few people have access to. In this problem, your task is to implement such a hash function to the following specification:
First, you will be given an unencrypted password as a String. For example, let's start with "password".
Next, you will add another copy of the first and last characters, so "password" becomes "ppasswordd".
Third, you will reverse the String, so "ppasswordd" becomes "ddrowssapp".
Finally, call the hashCode() method on the resulting String object, and return the result.
Definition
- Class:
- Hasher
- Method:
- hash
- Parameters:
- String
- Returns:
- int
- Method signature:
- int hash(String password)
- (be sure your method is public)
Constraints
- password will contain between 6 and 50 characters, inclusive.
- Each character in password will have ASCII value between 32 and 126, inclusive.
Examples
"password"
Returns: 1528838823
The example from above."abcdef"
Returns: 696032926
"abcdef" becomes "aabcdeff". Then, we reverse it and return the hash code."0_9**0)"
Returns: -1756902378