Util.vala 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Invercargill;
  2. namespace Pprf.Util {
  3. public static uint8[] string_checksum(string str) {
  4. return data_checksum(str.data);
  5. }
  6. public static uint8[] data_checksum(uint8[] data) {
  7. var checksum_calculator = new Checksum(ChecksumType.SHA512);
  8. checksum_calculator.update(data, data.length);
  9. var checksum = new uint8[ChecksumType.SHA512.get_length()];
  10. size_t chk_len = checksum.length;
  11. checksum_calculator.get_digest(checksum, ref chk_len);
  12. checksum.length = (int)chk_len;
  13. return checksum;
  14. }
  15. public static uint8[] file_checksum(File file) throws Error {
  16. var buffer = new uint8[10240];
  17. var checksum = new Checksum(ChecksumType.SHA512);
  18. var stream = file.read();
  19. while(true) {
  20. var read = stream.read(buffer);
  21. if(read == 0) {
  22. break;
  23. }
  24. checksum.update(buffer, read);
  25. }
  26. size_t dig_len = 64;
  27. var digest = new uint8[64];
  28. checksum.get_digest(digest, ref dig_len);
  29. digest.length = (int)dig_len;
  30. return digest;
  31. }
  32. }