Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Give me plz a help I NEED THAT

 
Mohammad Ahmad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi plz i need to implement Sha1 in java and i tooked a good look on Security.MessageDigest in java packages...but i need to implement it from scratch ..i have a big problem in making the append operation using the shift bitwise so plz any one can help me on how to make the append operation for the 1 and the zeros bits??
i had this code from some place but also i couldnt understand it well so if any can help me on:

public class SHA1 {

/*
* Bitwise rotate a 32-bit number to the left
*/
private static int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}

/*
* Take a string and return the base64 representation of its SHA-1.
*/
public static String encode(String str) {

// Convert a string to a sequence of 16-word blocks, stored as an array.
// Append padding bits and the length, as described in the SHA1 standard

byte[] x = str.getBytes();
int[] blks = new int[(((x.length + 8) >> 6) + 1) * 16];
int i;

for(i = 0; i < x.length; i++) {
blks[i >> 2] |= x[i] << (24 - (i % 4) * 8);
}

blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
blks[blks.length - 1] = x.length * 8;

plz iam waiting the reply my message must be stored in a file then i have to calculate the Message Digest...
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me for being dense, but I read your previous post and I can't figure out why you are trying to reimplement SHA when it is available in the API. Perhaps if you would elaborate on your requirement we could give you better advice.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic