Douma37

gift_matcher

Oct 5th, 2020
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <script>
  5.       const names = ["Will", "Rob", "Joshua", "Joe", "Hungry", "Dance", "Agata"];
  6.       const passcodeHashes =
  7.         ["41b48b722295a63cd22c7c1dcd687e06a755e5716343d6b9775f8a26cd4b137b",
  8.          "b4a9972f5cefa151961e26c1afce1b7dd0e57b1fbc880678b3abebfb578d2c92",
  9.          "a7cdf5d0586b392473dd0cd08c9ba833240006a8a7310bf9bc8bf1aefdfaeadb",
  10.          "a62de7af4c7bcfc37c05dc54f11dbf0bcae54d26a77d73554a42f7fbe9d03cf9",
  11.          "dcc32a14366fb0e19441242cba35522a079ded437b6f0692c4f1094fcbe74453",
  12.          "35084a43186b4fedc5e25bd31e4fbc48fdea4e8576b0d5ca34b59782e1a2cc2f",
  13.          "5e737f891db1175442a39fde73e51d781a545506d71c95477a6deb5988bd7f9a"];
  14.       const matches =
  15.         ["4f82f7041ae65e995e36d24f6131e0ddc961691c08380d00a7fe77c0ed00550c",
  16.          "cb14bf5073ebaf6d9d04b63164b7017b2011d3558fb2f80f9450c9f5de6f6de8",
  17.          "23d2d73543f9d7d92ac704296e5ee744037f5f4c938e21abf2c17002c4e9ba71",
  18.          "6cef4ccc1019d6cee6b9cad39d49cabf808ba2e0665d5832b70c44c09c2dfae0",
  19.          "9b059f912cb56871704877e178f0e926f1ad7d4c27053f11525c2ba8b9a6a5d1",
  20.          "1eda7c08467f73ea38883ee78ec2179177736b9fbf1bf8a21e891a79247ced5d",
  21.          "6dd8b7d7d3c5c4689b33e51b9f10bc6a9be89fe8fa2a127c8c6c03cd05d68ace"];
  22.  
  23.       /*
  24.        * Converts a string into a hex hash of it
  25.        */
  26.       async function digestMsg(message) {
  27.         const msgUint8 = new TextEncoder().encode(message);
  28.         const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);
  29.         const hashArray = Array.from(new Uint8Array(hashBuffer));
  30.         const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  31.         return hashHex;
  32.       }
  33.  
  34.       /*
  35.        * Verifies the name.
  36.        */
  37.       function verifyName(nameInput) {
  38.         return names.indexOf(nameInput) !== -1;
  39.       }
  40.      
  41.       /*
  42.        * Verifies the passcode.
  43.        */
  44.       async function verifyPasscode(passcodeInput, name) {
  45.         if (passcodeInput.length < 3) {
  46.           return false;
  47.         }
  48.         const hash = await digestMsg(passcodeInput);
  49.         const hashIndex = passcodeHashes.indexOf(hash);
  50.         const nameIndex = names.indexOf(name);
  51.         return hashIndex !== -1 && hashIndex === nameIndex;
  52.       }
  53.      
  54.       /*
  55.        * Does everything.
  56.        */
  57.       async function findMatch() {
  58.         const nameInput = document.getElementById("name").value.trim();
  59.         if (!verifyName(nameInput)) {
  60.           alert("not one of us");
  61.           return;
  62.         }
  63.         const name = nameInput;
  64.         var passcodeInput = document.getElementById("passcode").value.trim();
  65.         const passcodeMatches = await verifyPasscode(passcodeInput, name);
  66.         if (!passcodeMatches) {
  67.           alert("wrong passcode");
  68.           return;
  69.         }
  70.         const nameHash = await digestMsg(name);
  71.         var match = "";
  72.         for (let i = 0; i < names.length; i++) {
  73.           if (matches[i] === nameHash) {
  74.             match = matches[(i + 1) % matches.length];
  75.           }
  76.         }
  77.         for (let i = 0; i < names.length; i++) {
  78.           var candidateHash = await digestMsg(names[i]);
  79.           if (match === candidateHash) {
  80.             //alert("Your match is " + names[i]);
  81.             const result = "Your match is " + names[i];
  82.             console.log(result);
  83.             document.getElementById("result").innerHTML = result;
  84.           }
  85.         }
  86.       }
  87.      
  88.       /*
  89.        * Prevents default refreshing the page after the match is found
  90.        */
  91.       function preventRefresh() {
  92.         document.getElementById("resultButton").addEventListener("click", function(event){
  93.           event.preventDefault();
  94.           console.log("prevented refresh on button click");
  95.         });
  96.       }
  97.     </script>
  98.   </head>
  99.   <body onload="preventRefresh()">
  100.     <div>
  101.       <form>
  102.         <label for="name">What... is your name?</label><br>
  103.         <input type="text" id="name" name="name" value=""><br>
  104.         <label for="passcode">What... is your passcode?</label><br>
  105.         <input type="text" id="passcode" name="passcode" value=""><br><br>
  106.         <button id="resultButton" onclick="findMatch()">Find your match</button>
  107.       </form>
  108.     </div>
  109.     <div>
  110.       <p id="result"></p>
  111.     </div>
  112.   </body>
  113. </html>
Advertisement
Add Comment
Please, Sign In to add comment