瀏覽代碼

Added command tools to do crypto operations.

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@6612 8c9fc860-2736-0410-a75d-ab315db34111
eth.tmartiel 9 年之前
父節點
當前提交
b564996493
共有 1 個文件被更改,包括 68 次插入0 次删除
  1. 68 0
      source/CryptoTools.Mod

+ 68 - 0
source/CryptoTools.Mod

@@ -0,0 +1,68 @@
+MODULE CryptoTools; (** AUTHOR ""; PURPOSE ""; *)
+
+IMPORT
+	Commands, Files, Streams,
+	CryptoHashes, CryptoUtils;
+
+	(** Hash hashname filenames... ~ *)
+	PROCEDURE Hash*(context : Commands.Context);
+	VAR
+		buf, value: ARRAY 1024 OF CHAR;
+		file: ARRAY 256 OF CHAR;
+		hash: ARRAY 32 OF CHAR;
+		len: LONGINT;
+		h: CryptoHashes.Hash;
+		f: Files.File;
+		r: Files.Reader;
+	BEGIN
+		IF context.arg.GetString(hash) THEN
+			h := CryptoHashes.NewHash(hash);
+			IF h = NIL THEN
+				context.error.String("Error: Unknown hash '");
+				context.error.String(hash);
+				context.error.String("'.");
+				context.error.Ln;
+				RETURN
+			END;
+
+			LOOP
+				h.Initialize;
+				IF ~context.arg.GetString(file) THEN EXIT END;
+				f := Files.Old(file);
+				IF f = NIL THEN
+					context.error.String("Error: could not find file '");
+					context.error.String(file);
+					context.error.String("'.");
+					context.error.Ln;
+					RETURN
+				END;
+				Files.OpenReader(r, f, 0);
+				IF r = NIL THEN
+					context.error.String("Error: could not read file '");
+					context.error.String(file);
+					context.error.String("'.");
+					context.error.Ln;
+					RETURN
+				END;
+
+				WHILE r.res = Streams.Ok DO
+					r.Bytes(buf, 0, LEN(buf), len);
+					h.Update(buf, 0, len)
+				END;
+
+				h.GetHash(buf, 0);
+				CryptoUtils.Bin2Hex(buf, 0, value, 0, h.size);
+				context.out.String(file);
+				context.out.String(": ");
+				context.out.String(value);
+				context.out.Ln;
+				context.out.Update
+			END;
+		ELSE
+			context.error.String("Error: expected hash name");
+			context.error.Ln
+		END
+	END Hash;
+	
+
+END CryptoTools.