webcrypto test

Tutorials of (Webcrypto test) by Tomas langkaas

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>webcrypto test</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>
/* Downloaded from https://www.codeseek.co/ */
/* Downloaded from https://www.codeseek.co/ */
function ok(a,b,c){
  return function(){alert(['ok',a,b,c])}
}
function err(a,b,c){
  return function(){alert(['err',a,b,c])}
}


window.crypto = window.crypto || window.msCrypto; //for IE11
if (window.crypto.webkitSubtle) {
  window.crypto.subtle = window.crypto.webkitSubtle; //for Safari
}
//deriveBits
var password = window.crypto.getRandomValues(new Uint8Array(16));
window.crypto.subtle
  .importKey("raw", password, { name: "PBKDF2" }, false, [
    "deriveBits",
    "deriveKey"
  ])
  .then(function(key) {
    //sha-1
    window.crypto.subtle
      .deriveBits(
        {
          name: "PBKDF2",
          salt: window.crypto.getRandomValues(new Uint8Array(16)),
          iterations: 1000,
          hash: { name: "SHA-1" }
        },
        key,
        128
      )
      .then(ok("pbkdf2", "deriveBits", "SHA-1"))
      .catch(err("pbkdf2", "deriveBits", "SHA-1"));

    //sha-256
    window.crypto.subtle
      .deriveBits(
        {
          name: "PBKDF2",
          salt: window.crypto.getRandomValues(new Uint8Array(16)),
          iterations: 1000,
          hash: { name: "SHA-256" }
        },
        key,
        128
      )
      .then(ok("pbkdf2", "deriveBits", "SHA-256"))
      .catch(err("pbkdf2", "deriveBits", "SHA-256"));

    //sha-384
    window.crypto.subtle
      .deriveBits(
        {
          name: "PBKDF2",
          salt: window.crypto.getRandomValues(new Uint8Array(16)),
          iterations: 1000,
          hash: { name: "SHA-384" }
        },
        key,
        128
      )
      .then(ok("pbkdf2", "deriveBits", "SHA-384"))
      .catch(err("pbkdf2", "deriveBits", "SHA-384"));

    //sha-512
    window.crypto.subtle
      .deriveBits(
        {
          name: "PBKDF2",
          salt: window.crypto.getRandomValues(new Uint8Array(16)),
          iterations: 1000,
          hash: { name: "SHA-384" }
        },
        key,
        128
      )
      .then(ok("pbkdf2", "deriveBits", "SHA-512"))
      .catch(err("pbkdf2", "deriveBits", "SHA-512"));
  })
  .catch(err("pbkdf2", "deriveBits", "N/A"));

This awesome code ( webcrypto test ) is write by Tomas Langkaas, you can se more from this user in the personal repository

You can find the original code on Codepen.io

2018 © Tomas Langkaas