CryptoTools.Mod 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. MODULE CryptoTools; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT
  3. Commands, Files, Streams,
  4. CryptoHashes, CryptoUtils;
  5. (** Hash hashname filenames... ~ *)
  6. PROCEDURE Hash*(context : Commands.Context);
  7. VAR
  8. buf, value: ARRAY 1024 OF CHAR;
  9. file: ARRAY 256 OF CHAR;
  10. hash: ARRAY 32 OF CHAR;
  11. len: LONGINT;
  12. h: CryptoHashes.Hash;
  13. f: Files.File;
  14. r: Files.Reader;
  15. BEGIN
  16. IF context.arg.GetString(hash) THEN
  17. h := CryptoHashes.NewHash(hash);
  18. IF h = NIL THEN
  19. context.error.String("Error: Unknown hash '");
  20. context.error.String(hash);
  21. context.error.String("'.");
  22. context.error.Ln;
  23. RETURN
  24. END;
  25. LOOP
  26. h.Initialize;
  27. IF ~context.arg.GetString(file) THEN EXIT END;
  28. f := Files.Old(file);
  29. IF f = NIL THEN
  30. context.error.String("Error: could not find file '");
  31. context.error.String(file);
  32. context.error.String("'.");
  33. context.error.Ln;
  34. RETURN
  35. END;
  36. Files.OpenReader(r, f, 0);
  37. IF r = NIL THEN
  38. context.error.String("Error: could not read file '");
  39. context.error.String(file);
  40. context.error.String("'.");
  41. context.error.Ln;
  42. RETURN
  43. END;
  44. WHILE r.res = Streams.Ok DO
  45. r.Bytes(buf, 0, LEN(buf), len);
  46. h.Update(buf, 0, len)
  47. END;
  48. h.GetHash(buf, 0);
  49. CryptoUtils.Bin2Hex(buf, 0, value, 0, h.size);
  50. context.out.String(file);
  51. context.out.String(": ");
  52. context.out.String(value);
  53. context.out.Ln;
  54. context.out.Update
  55. END;
  56. ELSE
  57. context.error.String("Error: expected hash name");
  58. context.error.Ln
  59. END
  60. END Hash;
  61. END CryptoTools.