Guest User

Untitled

a guest
May 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import java.security.MessageDigest
  2. import java.io.*
  3.  
  4. /**
  5. * Determine the MD5 or SHA1Sum of a file:
  6. *
  7. * println Checksum.getMD5Sum(new File("file.bin"))
  8. * println Checksum.getSHA1Sum(new File("file.bin"))
  9. *
  10. * @author: Marcel Maatkamp (m.maatkamp avec gmail dot com)
  11. */
  12. public class Checksum {
  13.  
  14. static getSha1sum(file) {
  15. return getChecksum(file, "SHA1")
  16. }
  17.  
  18. static getMD5sum(file) {
  19. return getChecksum(file, "MD5")
  20. }
  21.  
  22. static getChecksum(file, type) {
  23. def digest = MessageDigest.getInstance(type)
  24. def inputstream = file.newInputStream()
  25. def buffer = new byte[16384]
  26. def len
  27.  
  28. while((len=inputstream.read(buffer)) > 0) {
  29. digest.update(buffer, 0, len)
  30. }
  31. def sha1sum = digest.digest()
  32.  
  33. def result = ""
  34. for(byte b : sha1sum) {
  35. result += toHex(b)
  36. }
  37. return result
  38. }
  39.  
  40. private static hexChr(int b) {
  41. return Integer.toHexString(b & 0xF)
  42. }
  43.  
  44. private static toHex(int b) {
  45. return hexChr((b & 0xF0) >> 4) + hexChr(b & 0x0F)
  46. }
  47.  
  48. static def main(args) {
  49. def sha1sum = Checksum.getSha1sum(new File(args[0]))
  50. println "file($args[0]): sha1sum($sha1sum)"
  51. }
  52.  
  53. }
Add Comment
Please, Sign In to add comment