Skip to content

Commit

Permalink
Hash Function
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed May 15, 2021
1 parent 88aecfc commit 10bbd37
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions secret-hash.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,29 @@
<body>

<h2>Input:</h2>
<input type="text" autocomplete="off" class="encode_sha_param" id="encode_sha_form" style="width:80%">
<input required type="text" autocomplete="off" class="encode_sha_param" id="encode_sha_form" style="width:80%">
<br><br>
<strong>Function:</strong>
<select class="encode_function" id="encode_function" required>
<option selected value="SHA-512">SHA-512</option>
<option value="SHA-256">SHA-256</option>
</select>
<br>

<br>
<input type="button" value="Submit" id="valid_button">

<br><br><hr><br>

<h2>SHA-512:</h2>
<h2>Hash:</h2>
<div id="sha_result" style="font-weight:bold;color:blue;background-color:yellow;width:85%;word-break:break-all;margin:auto;">??????</div>


<script type="text/javascript">

async function digestMessage(message) {
async function digestMessage(message, hashFunction) {
var msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
var hashBuffer = await crypto.subtle.digest('SHA-512', msgUint8); // hash the message
var hashBuffer = await crypto.subtle.digest(hashFunction, msgUint8); // hash the message
var hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
var hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string

Expand All @@ -59,12 +65,13 @@ <h2>SHA-512:</h2>

$("#valid_button").on("click",(async function(){

var hashFunction = $("#encode_function").val();
var digestHex = $("#encode_sha_form").val();

var iteration = 1;

for (let i = 0; i < iteration; i++) {
digestHex = await digestMessage(digestHex);
digestHex = await digestMessage(digestHex, hashFunction);
}

$("#sha_result").html(digestHex.toUpperCase());
Expand Down

0 comments on commit 10bbd37

Please sign in to comment.