Advertisement
Guest User

Untitled

a guest
May 27th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. package oop.frame.structure;
  2.  
  3. public class CRC32 {
  4.  
  5. private int crc
  6. = 0xFFFFFFFF;
  7. // initial contents
  8. /**
  9. * calculates a checksum for given input {@code byte[]}
  10. * @param bytes {@code byte[]} to create checksum for
  11. */
  12. public void update(byte[] bytes) {
  13. int poly = 0xEDB88320;
  14. // reverse polynomial
  15. for (byte b : bytes) {
  16. int temp = (crc ^ b) & 0xff;
  17. // read 8 bits one at a time
  18. for (int i = 0; i < 8; i++) {
  19. if ((temp & 1) == 1) {
  20. temp = (temp >>> 1) ^ poly;
  21. } else {
  22. temp = (temp >>> 1);
  23. }
  24. }
  25. crc = (crc >>> 8) ^ temp;
  26. }
  27. // flip bits
  28. crc = crc ^ 0xffffffff;
  29. }
  30. /**
  31. * Gets the calculated checksum
  32. * @return {@code byte[]} representing the checksum
  33. */
  34. public byte[] getCRC() {
  35. return new byte[] {
  36. (byte) (crc >> 24),
  37. (byte) (crc >> 16),
  38. (byte) (crc >> 8),
  39. (byte) crc};
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement