webcrypto test

In this example below you will see how to do a webcrypto test with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by tomaslangkaas, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright tomaslangkaas ©
  • HTML
  • JavaScript
<!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/tomaslangkaas/webcrypto-test-WOrKXX */
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"));

Comments