website-theme/layouts/shortcodes/pgpform.html
2022-12-18 18:03:16 -05:00

39 lines
1.6 KiB
HTML

{{- $openPGP := resources.Get "js/openpgp.min.js" -}}
<script src="{{ $openPGP.Permalink }}"></script>
<textarea id="pgpcleartext" class="pgpform" style="width: 100%; min-height: 10rem;"></textarea>
<button class="pgpbutton" onclick="encrypt()">Encrypt</button>
<br/>
Encrypted Result:
<pre id="pgpresult" class="pgpform"></pre>
<script>
function encrypt() {
let textarea = document.querySelector("#pgpcleartext");
let resultarea = document.querySelector('#pgpresult');
let pubKeyURL = "{{ .Get 0 }}"
fetch(pubKeyURL).then(function(response) {
return response.text().then(function(text) {
const pubKey = openpgp.readKey({ armoredKey: text });
const message = openpgp.createMessage({ text: textarea.value })
return Promise.all([message, pubKey]).then(function(mp) {
const encryptionParameters = {
message: mp[0],
encryptionKeys: mp[1]
}
return openpgp.encrypt(encryptionParameters).then(function(encryptedMessage) {
pgpresult.textContent = encryptedMessage
});
});
});
});
}
function genEmail() {
let resultarea = document.querySelector('#pgpresult');
let contents = resultarea.textContent;
let encodedContents = encodeURIComponent(contents);
let encodedSubject = encodeURIComponent("Contact Form")
window.location.href = "mailto:{{ .Site.Params.email }}?subject=" +
encodedSubject + "&body=" + encodedContents;
}
</script>
<button class="pgpbutton" onclick="genEmail()">Generate Email</button>