Monday, January 27, 2025

Crypto.JS Vanilla JS

 <!DOCTYPE html>

<html>

<head>

  <title>Contoh CryptoJS</title>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

</head>

<body>

  <input type="text" id="input-text" placeholder="Masukkan teks">

  <button onclick="encrypt()">Enkripsi</button>

  <button onclick="decrypt()">Dekripsi</button>

  <p id="result"></p>

  <script>

    function encrypt() {

      var message = document.getElementById('input-text').value;

      var key = "your_secret_key"; // Ganti dengan kunci rahasia Anda


      // Enkripsi menggunakan AES

      var ciphertext = CryptoJS.AES.encrypt(message, key).toString();


      document.getElementById('result').textContent = "Teks Terenkripsi: " + ciphertext;

    }


    function decrypt() {

      var ciphertext = document.getElementById('result').textContent.split(": ")[1];

      var key = "your_secret_key"; // Pastikan kunci sama dengan saat enkripsi


      // Dekripsi

      var bytes = CryptoJS.AES.decrypt(ciphertext, key);

      var originalText = bytes.toString(CryptoJS.enc.Utf8);


      document.getElementById('result').textContent = "Teks Asli: " + originalText;

    }

  </script>

</body>


</html>

Thursday, January 9, 2025

HMAC Untuk WEB TOKEN DENGAN Beda Server dengan PHP & Vanilla_JS

Server 1 = http://localhost:7000

hmac.php

<?php

header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=UTF-8");

header("Access-Control-Allow-Headers: X-ACCESS_TOKEN, Origin, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$getheaders = getallheaders();

$secretKey = '8uRhAeH89naXfFXKGOEj';

$value = 'username=rasmuslerdorf';

$signature = $getheaders['Authorization'];

if (hash_equals(hash_hmac('sha256', $value, $secretKey), $signature)) {

    echo json_encode("The value is correctly signed.");

} else {

    echo json_encode("The value was tampered with.");

}

?>


Server 1 = http://localhost:9000

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

</body>

<script>

    window.addEventListener("load", async () => {

        await callData();

    })

    function callData() {

        fetch('http://localhost:7000/hmac.php', {

            method: 'GET',

            headers: {

                'Authorization': '8c35009d3b50caf7f5d2c1e031842e6b7823a1bb781d33c5237cd27b57b5f327'

            }

        })

            .then(response => response.json())

            .then(data => {

                console.log(data)

            })

    }

</script>

</html>


Set Cookie dan Remove Cookie dengan php vanilla.js

< html lang = "en" > < head >     < meta charset = "UTF-8" >     < meta name = "viewport...