Mas Koding
/

hash SHA-256 string di browser

Mas Koding
Desember 30, 2024


 fungsi mengubah string teks menjadi hashing SHA-256

async function hashStringSHA256(input) {
    try {
        const encoder = new TextEncoder();
        const data = encoder.encode(input);
        const hashBuffer = await crypto.subtle.digest("SHA-256", data);
        const hashArray = Array.from(new Uint8Array(hashBuffer));
        const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');

        return hashHex;
    } catch (error) {
        console.error("Error hashing string:", error.message);
        return null;
    };
};

versi minify

async function hashStringSHA256(r){try{let t=new TextEncoder,n=t.encode(r),e=await crypto.subtle.digest("SHA-256",n),a=Array.from(new Uint8Array(e)),i=a.map(r=>r.toString(16).padStart(2,"0")).join("");return i}catch(s){return console.error("Error hashing string:",s.message),null}}


testing

hashStringSHA256("admin12345").then((hash) => {
    console.log(hash)
});
(async () => {
    let hash = await hashStringSHA256("admin12345");
    console.log(hash)
})();


Versi Nodejs

import {
    createHash
} from 'crypto';

// String yang akan di-hash
const data = "Ini adalah string yang ingin di-hash";

// Membuat hash menggunakan algoritma SHA-256
const hash = createHash('sha256').update(data).digest('hex');

console.log("Hasil hash:", hash);


Share Article

Spread the word

1 Komentar

  1. Hello sir how are you?
    Sir I want to need this script can you plz provide ma

    https://www.facebook.com/maskodingku/videos/1627581874759612/

    Here is my WhatsApp number Add me as a contact on WhatsApp. https://wa.me/qr/BTQ3OGAFMT5KD1

    BalasHapus

Mas Koding

Sharing knowledge about technology, programming tutorials, and the latest web development trends. Built for developers by developers.

Discover

© 2025 Mas Koding. Crafted with h.

System Operational