patternjavaMinor
New MD5 algorithm
Viewed 0 times
algorithmmd5new
Problem
Can you please try my algorithm and give comments or feedback about it?
```
package md52;
import java.io.*; //input outputs
import static java.lang.Math.*; //for math purposes
/**
*
* JesseePogi
*/
public class Md52 {
public static String input;
public static void main(String[] args) {
//start timer (for algo speed testing)
long start = System.currentTimeMillis();
//input
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter the text here: ");
input = bf.readLine();
}
catch(IOException err)
{
System.out.println("Read Error!");
}
//initialize digest
int h0 = 0x67452301;
int h1 = 0xefcdab89;
int h2 = 0x98badcfe;
int h3 = 0x10325476;
//Initialize hash for chunk
int a = h0;
int b = h1;
int c = h2;
int d = h3;
int f = 0, g = 0;
//initialize shift rotation
int r[] = {7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
5, 9,14,20,5, 9,14,20,5, 9,14,20,5, 9,14,20,
4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21};
//initialize constant k
int k[] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9,
```
package md52;
import java.io.*; //input outputs
import static java.lang.Math.*; //for math purposes
/**
*
* JesseePogi
*/
public class Md52 {
public static String input;
public static void main(String[] args) {
//start timer (for algo speed testing)
long start = System.currentTimeMillis();
//input
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter the text here: ");
input = bf.readLine();
}
catch(IOException err)
{
System.out.println("Read Error!");
}
//initialize digest
int h0 = 0x67452301;
int h1 = 0xefcdab89;
int h2 = 0x98badcfe;
int h3 = 0x10325476;
//Initialize hash for chunk
int a = h0;
int b = h1;
int c = h2;
int d = h3;
int f = 0, g = 0;
//initialize shift rotation
int r[] = {7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
5, 9,14,20,5, 9,14,20,5, 9,14,20,5, 9,14,20,
4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21};
//initialize constant k
int k[] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9,
Solution
A few notes:
There is no need to comment
Commented-out code is dead code. Dead code should be removed. If you are afraid of deleting code, you probably don't know about source control management. Look into learning how to work with SCM tools such as git.
Variable names should be meaningful and reveal the semantics in order to make the code read like a story or at least a manual. Names like
Your code contains many magic numbers, i.e. numbers that appear in the code without obviously revealing their meaning. Convert them into constants to improve both readability and maintainability.
Avoid enumerating variable names such as
Ask yourself: If you only saw the name
The common Java standard is camelCase, not snake_case. So variables should be named
Your method is (way, way) too long. Break every individual piece into its own method. Maybe even break it down into separate classes if necessary. A good method does one thing, yours does dozens of things and makes it pretty much impossible to keep track of what is going on as there is no abstraction supporting this.
Instead of printing the result, your method (which won't be a
There is no need to comment
import statements. Although I want to say thank you, I've never actually seen anyone do that before. But really, don't do it.Commented-out code is dead code. Dead code should be removed. If you are afraid of deleting code, you probably don't know about source control management. Look into learning how to work with SCM tools such as git.
Variable names should be meaningful and reveal the semantics in order to make the code read like a story or at least a manual. Names like
a, b, c, … are meaningless and only make understanding (and therefore maintaining) the code harder.Your code contains many magic numbers, i.e. numbers that appear in the code without obviously revealing their meaning. Convert them into constants to improve both readability and maintainability.
Avoid enumerating variable names such as
byteTemp and then byteTemp1. Again, variable names should represent semantics. They should not represent their type either (we will know that by reading their declaration).Ask yourself: If you only saw the name
byteTemp, what would you really know about it? Maybe that it (probably) has to do with bytes, but you would know nothing about what this variable represents in the context.The common Java standard is camelCase, not snake_case. So variables should be named
someVariable, not some_variable.Your method is (way, way) too long. Break every individual piece into its own method. Maybe even break it down into separate classes if necessary. A good method does one thing, yours does dozens of things and makes it pretty much impossible to keep track of what is going on as there is no abstraction supporting this.
Instead of printing the result, your method (which won't be a
main method anyway) should return the result. Leave it up to the caller to decide what they want to do with the result.Context
StackExchange Code Review Q#24300, answer score: 7
Revisions (0)
No revisions yet.