Selaa lähdekoodia

Only output sha1 at the end

Dennis Rodewyk 5 vuotta sitten
vanhempi
commit
c40284a1df
2 muutettua tiedostoa jossa 22 lisäystä ja 20 poistoa
  1. 1 1
      hugo-encrypt.go
  2. 21 19
      shortcodes/hugo-encrypt.html

+ 1 - 1
hugo-encrypt.go

@@ -60,7 +60,7 @@ func encryptPage(path string) {
 		fmt.Printf("\nSHA1: % x\n", sha1.Sum(data))
 		sha1_sum := sha1.Sum(data)
 		sha1_string := hex.EncodeToString(sha1_sum[:])
-		finalcontent := ("<div id='beginProtectedContent'>" + sha1_string + "</div>\n" + blockhtml + "\n<div id='endProtectedContent'>" + sha1_string + "</div>")
+		finalcontent := (blockhtml + "\n<div id='verifyText'>" + sha1_string + "</div>")
 		enchtml := encrypt(password, finalcontent)
 		block.RemoveAttr("data-password")
 		block.SetHtml(enchtml)

+ 21 - 19
shortcodes/hugo-encrypt.html

@@ -16,13 +16,13 @@
 
 <hugo-encrypt>
 	{{ if .Get 0 }}
-		{{- $password := $.Scratch.Set "password" (.Get 0) -}}
-	{{ else if .Site.Params.Passwoird }}
-		{{- $password := $.Scratch.Set "password" .Site.Params.Password -}}
+		{{- $passphrase := $.Scratch.Set "passphrase" (.Get 0) -}}
+	{{ else if .Site.Params.Password }}
+		{{- $passphrase := $.Scratch.Set "passphrase" .Site.Params.Password -}}
 	{{ else }}
-		{{- $password -}}
+		{{- $passphrase -}}
 	{{ end }}
-	<p>{{ i18n "protectedbypwd" }}</p>
+	<p>{{ i18n "protectedbypwd" }} The password is <code>{{ $.Scratch.Get "passphrase" }}</code>.</p>
 
 	<div class='hugo-encrypt-form'>
 		<input
@@ -37,11 +37,13 @@
 			id="button" onclick="hugoDecrypt(document.getElementById('hugo-encrypt-password').value,'input')"
 		/>
 	</div>
-	<cipher-text data-password="{{ $password }}" style="display:none;">
+	<cipher-text data-password="{{ $.Scratch.Get "passphrase" }}" style="display:none;">
 <!-- Do not indent the following two lines -->
-<p id="verifyText" style="display:none;">The quick brown fox jumps over the lazy dog</p>
 {{ .Inner }}
 	</cipher-text>
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/core.js"></script>
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/hmac.js"></script>
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/sha1.js"></script>
 	<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.0/showdown.min.js"></script>
 	<script>
 		let cipher = document.getElementsByTagName("cipher-text")[0];
@@ -108,14 +110,6 @@
 				.then(([key]) => crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, data))
 				.then(v => buf2str(new Uint8Array(v)));
 		}
-		/**
-		 * The html entities in the decrypted text need to be decoded
-		 * before converting any markdown to html
-		 */
-		function htmlDecode(input){
-			var doc = new DOMParser().parseFromString(input, "text/html");
-			return doc.documentElement.textContent;
-		}
 		/**
 		 * Needed to convert markdown within the decrypted text to html
 		 */
@@ -132,10 +126,19 @@
 		const hugoDecrypt = function(password, type) {
 			try {
 				decrypt(password, cipher.innerText).then(function(res) {
-					if ( res.includes("The quick brown fox jumps over the lazy dog") ) {
-						cipher.parentElement.outerHTML = interpreteMarkdown(htmlDecode(res));
+					/**
+					* calculate sha1 of decrypted text and check if it
+					* matches the sha1 at the bottom of the decrypted text
+					* to get the hash that was added during encryption we
+					* need to remove the last line
+					*/
+					var hash = CryptoJS.SHA1(res.replace(/[\w\W]+?\n+?/,"").replace(/\r?\n?[^\r\n]*$/, ""));
+					var result = CryptoJS.enc.Hex.stringify(hash);
+					if ( res.includes(result) ) {
+						cipher.parentElement.outerHTML = interpreteMarkdown(res);
 						userStorage.setItem(storageKey, password);
-						document.getElementById("verifyText").outerHTML = "";
+						document.getElementById("beginProtectedContent").innerHTML = "Beginning of decrypted content";
+						document.getElementById("endProtectedContent").innerHTML = "End of decrypted content";
 					} else {
 						if (type === "input") {
 							alert('{{ i18n "wrongpwd" }}');
@@ -161,4 +164,3 @@
 		};
 	</script>
 </hugo-encrypt>
-