Encoding vs encryption vs hashing: the difference that matters

“We store the passwords in Base64, so they’re encrypted.” “The API key is hashed, so we can send it back to the user later.” Both sentences come from real projects, and both describe security holes. Encoding, encryption and hashing all turn data into something that looks unreadable, which is why they get mixed up — but they answer completely different questions. Choosing the wrong one is how databases full of “protected” passwords end up readable in plain text.

Encoding: changing the format, not hiding anything

Encoding transforms data so it can travel safely through a particular channel. The rules are public, there is no key, and anyone can reverse it. Its job is compatibility, not secrecy.

// Base64: binary-safe text
Buffer.from('p@ssw0rd').toString('base64');   // 'cEBzc3cwcmQ='
Buffer.from('cEBzc3cwcmQ=', 'base64').toString(); // 'p@ssw0rd'

// URL (percent) encoding: safe inside a query string
encodeURIComponent('a b&c=d');                 // 'a%20b%26c%3Dd'

Other examples: UTF-8 (text to bytes), hex, HTML entities such as <. If you can decode it without a secret, it protects nothing. See Base64 explained for how the most common one works.

Encryption: hiding data from everyone without the key

Encryption turns plaintext into ciphertext that can only be turned back with the right key. It is reversible by design — for whoever holds the key. Use it when you need the original data again later: messages, files, database fields like national ID numbers, backups.

import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';

const key = randomBytes(32);          // 256-bit key: keep it secret
const iv = randomBytes(12);           // new random IV for every message
const cipher = createCipheriv('aes-256-gcm', key, iv);
const ciphertext = Buffer.concat([cipher.update('Meet at 5pm', 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();

const decipher = createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(tag);
Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8'); // 'Meet at 5pm'

AES-GCM also detects tampering: if one byte of the ciphertext changes, decipher.final() throws. The hard part of encryption is rarely the algorithm; it is key management — where the key lives, who can read it, and how it is rotated. A key hard-coded next to the data it protects is barely better than no encryption.

Hashing: a one-way fingerprint

A cryptographic hash function maps any input to a fixed-size output, and it is not reversible — there is no key and no “unhash”. The same input always gives the same hash, and a tiny change in the input changes the output completely:

sha256('hello') = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha256('Hello') = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

Hashes are used to check integrity (download checksums, Git commits), to deduplicate data, and as a building block for signatures. You check a hash by hashing the candidate again and comparing.

Side by side

EncodingEncryptionHashing
PurposeCompatibility / transportConfidentialityIntegrity / fingerprint
Reversible?Yes, by anyoneYes, with the keyNo
Needs a key?NoYesNo (HMAC adds one)
Output sizeDepends on inputAbout the input sizeFixed (e.g. 256 bits)
ExamplesBase64, URL encoding, UTF-8AES-GCM, RSA-OAEP, ChaCha20-Poly1305SHA-256, SHA-3, BLAKE2

Passwords: slow, salted hashes only

Passwords should be hashed, never encrypted — the server only ever needs to check a login, not read the password. But a fast hash is the wrong tool. SHA-256 is designed to be quick, so a modern GPU can test billions of guesses per second against a leaked hash. MD5 and SHA-1 are even faster and have known weaknesses; unsalted MD5 hashes of common passwords can simply be looked up — search the web for 5d41402abc4b2a76b9719d911017c592 and you will find it is hello.

Use a dedicated password-hashing function: Argon2id (first choice for new systems), scrypt or bcrypt. They are deliberately slow and memory-hard, have a tunable cost, and take a random salt per user so that two people with the same password get different hashes and precomputed tables are useless.

import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto';

const PARAMS = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 };

function hashPassword(password) {
  const salt = randomBytes(16);
  const hash = scryptSync(password, salt, 32, PARAMS);
  return `${salt.toString('hex')}:${hash.toString('hex')}`;
}

function checkPassword(password, stored) {
  const [saltHex, hashHex] = stored.split(':');
  const hash = scryptSync(password, Buffer.from(saltHex, 'hex'), 32, PARAMS);
  return timingSafeEqual(hash, Buffer.from(hashHex, 'hex'));
}

On a typical laptop each call takes a few hundred milliseconds — unnoticeable for one login, crippling for an attacker. In Python, the argon2-cffi and bcrypt packages do the same job and store the salt and cost inside the hash string for you. Note that bcrypt only uses the first 72 bytes of a password.

Never store passwords as Base64, with reversible encryption, or as plain MD5, SHA-1 or SHA-256 — salted or not.

HMAC: hashing with a key

An HMAC combines a hash with a secret key. Only someone with the key can produce the right value, so it proves a message came from a key holder and wasn’t changed. Payment gateways use it to sign webhooks; HS256 JWTs are HMAC-SHA256.

createHmac('sha256', 'my-secret-key').update('amount=500&to=rahim').digest('hex');
// '0c91fba74f9d3a7ec543c117d241883b51d6a9a3df886463b813c6cd94dccd3c'

Compare HMACs with a constant-time function such as timingSafeEqual or Python’s hmac.compare_digest, not ===.

Common misconceptions

See for yourself that encoding hides nothing. Paste any Base64 string into the Base64 tool and it decodes instantly — no key needed, all in your browser. Try it now

FAQ

Can a hash be decrypted?

No. But a weak or fast hash of a short or common input can be guessed: an attacker hashes candidates until one matches. That is why passwords need slow, salted algorithms.

Should I encrypt or hash API keys I issue?

Hash them (a fast hash such as SHA-256 is fine here, because random keys are too long to guess) and show the full key to the user only once. If you must be able to display a secret again, such as a third-party key your app uses, encrypt it and protect the encryption key.

Is MD5 still useful for anything?

As a non-security checksum to spot accidental corruption, yes. For passwords, signatures or anything an attacker could influence, no — use SHA-256 or better.

এনকোডিং, এনক্রিপশন ও হ্যাশিং: যে পার্থক্যটা জরুরি

“আমরা পাসওয়ার্ড Base64-এ রাখি, তাই এগুলো এনক্রিপ্টেড।” “API key হ্যাশ করা আছে, পরে ইউজারকে ফেরত দেখানো যাবে।” দুটো বাক্যই বাস্তব প্রজেক্ট থেকে নেওয়া, আর দুটোই নিরাপত্তার ফাঁক বোঝায়। এনকোডিং, এনক্রিপশন আর হ্যাশিং — তিনটিই ডেটাকে এমন কিছুতে বদলায় যা দেখতে অপাঠ্য, তাই এগুলো গুলিয়ে যায়। কিন্তু তিনটি একেবারে আলাদা প্রশ্নের উত্তর দেয়। ভুলটা বেছে নিলেই “সুরক্ষিত” পাসওয়ার্ডে ভরা ডেটাবেস সাধারণ লেখার মতো পড়া যায়।

এনকোডিং: ফরম্যাট বদলানো, কিছু লুকানো নয়

এনকোডিং ডেটাকে এমনভাবে রূপান্তর করে যাতে সেটা নির্দিষ্ট কোনো মাধ্যমে নিরাপদে যেতে পারে। নিয়মগুলো সবার জানা, কোনো কী নেই, আর যে কেউ উল্টো করতে পারে। এর কাজ সামঞ্জস্য, গোপনীয়তা নয়।

// Base64: বাইনারি-নিরাপদ লেখা
Buffer.from('p@ssw0rd').toString('base64');   // 'cEBzc3cwcmQ='
Buffer.from('cEBzc3cwcmQ=', 'base64').toString(); // 'p@ssw0rd'

// URL (percent) এনকোডিং: query string-এর ভেতরে নিরাপদ
encodeURIComponent('a b&c=d');                 // 'a%20b%26c%3Dd'

আরও উদাহরণ: UTF-8 (লেখা থেকে বাইট), hex, <-এর মতো HTML entity। সিক্রেট ছাড়াই ডিকোড করা গেলে সেটা কিছুই রক্ষা করে না। সবচেয়ে প্রচলিতটি কীভাবে কাজ করে জানতে দেখুন Base64 সহজ ভাষায়।

এনক্রিপশন: কী ছাড়া সবার কাছ থেকে ডেটা লুকানো

এনক্রিপশন plaintext-কে ciphertext-এ বদলায়, যা কেবল সঠিক কী দিয়েই আগের রূপে ফেরানো যায়। এটা নকশাগতভাবেই উল্টানো যায় — যার কাছে কী আছে তার জন্য। যখন পরে আসল ডেটা আবার লাগবে, তখন এটা ব্যবহার করুন: মেসেজ, ফাইল, NID নম্বরের মতো ডেটাবেস ফিল্ড, ব্যাকআপ।

import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';

const key = randomBytes(32);          // ২৫৬-বিট কী: গোপন রাখুন
const iv = randomBytes(12);           // প্রতিটি মেসেজের জন্য নতুন র‍্যান্ডম IV
const cipher = createCipheriv('aes-256-gcm', key, iv);
const ciphertext = Buffer.concat([cipher.update('Meet at 5pm', 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();

const decipher = createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(tag);
Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8'); // 'Meet at 5pm'

AES-GCM কারচুপিও ধরে ফেলে: ciphertext-এর একটা বাইট বদলালেই decipher.final() এরর দেয়। এনক্রিপশনের কঠিন অংশ সাধারণত অ্যালগরিদম নয়, বরং কী ব্যবস্থাপনা — কী কোথায় থাকে, কে পড়তে পারে, কীভাবে বদলানো হয়। যে ডেটা রক্ষা করছে তার পাশেই হার্ড-কোড করা কী, এনক্রিপশন না থাকার চেয়ে খুব একটা ভালো নয়।

হ্যাশিং: একমুখী আঙুলের ছাপ

ক্রিপ্টোগ্রাফিক হ্যাশ ফাংশন যেকোনো ইনপুটকে নির্দিষ্ট আকারের আউটপুটে রূপান্তর করে, আর এটা উল্টানো যায় না — কোনো কী নেই, কোনো “unhash” নেই। একই ইনপুট সবসময় একই হ্যাশ দেয়, আর ইনপুটে সামান্য পরিবর্তনেও আউটপুট পুরো বদলে যায়:

sha256('hello') = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha256('Hello') = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

হ্যাশ ব্যবহার হয় অখণ্ডতা যাচাইয়ে (ডাউনলোডের checksum, Git commit), ডুপ্লিকেট ডেটা ধরতে, আর সিগনেচারের ভিত্তি হিসেবে। হ্যাশ মেলাতে হলে সম্ভাব্য ইনপুটটা আবার হ্যাশ করে তুলনা করতে হয়।

পাশাপাশি তুলনা

এনকোডিংএনক্রিপশনহ্যাশিং
উদ্দেশ্যসামঞ্জস্য / পরিবহনগোপনীয়তাঅখণ্ডতা / আঙুলের ছাপ
উল্টানো যায়?হ্যাঁ, যে কেউ পারেহ্যাঁ, কী থাকলেনা
কী লাগে?নাহ্যাঁনা (HMAC-এ লাগে)
আউটপুটের আকারইনপুটের ওপর নির্ভর করেপ্রায় ইনপুটের সমাননির্দিষ্ট (যেমন ২৫৬ বিট)
উদাহরণBase64, URL এনকোডিং, UTF-8AES-GCM, RSA-OAEP, ChaCha20-Poly1305SHA-256, SHA-3, BLAKE2

পাসওয়ার্ড: শুধু ধীর, salt-যুক্ত হ্যাশ

পাসওয়ার্ড হ্যাশ করতে হয়, কখনো এনক্রিপ্ট নয় — সার্ভারের শুধু লগইন যাচাই করা দরকার, পাসওয়ার্ড পড়ার দরকার নেই। কিন্তু দ্রুত হ্যাশ এখানে ভুল হাতিয়ার। SHA-256 দ্রুত চলার জন্যই তৈরি, তাই একটা আধুনিক GPU ফাঁস হওয়া হ্যাশের বিপরীতে প্রতি সেকেন্ডে কোটি কোটি অনুমান পরীক্ষা করতে পারে। MD5 আর SHA-1 আরও দ্রুত, আর এদের জানা দুর্বলতাও আছে; সাধারণ পাসওয়ার্ডের salt ছাড়া MD5 হ্যাশ সরাসরি খুঁজেই পাওয়া যায় — ওয়েবে 5d41402abc4b2a76b9719d911017c592 সার্চ করলেই দেখবেন এটা hello।

পাসওয়ার্ডের জন্য বানানো ফাংশন ব্যবহার করুন: Argon2id (নতুন সিস্টেমে প্রথম পছন্দ), scrypt বা bcrypt। এগুলো ইচ্ছে করেই ধীর ও বেশি মেমরি নেয়, খরচ (cost) বাড়ানো-কমানো যায়, আর প্রতিটি ইউজারের জন্য র‍্যান্ডম salt নেয় — ফলে একই পাসওয়ার্ডের দুজন ইউজারের হ্যাশ আলাদা হয়, আর আগে থেকে হিসাব করা টেবিল কোনো কাজে আসে না।

import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto';

const PARAMS = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 };

function hashPassword(password) {
  const salt = randomBytes(16);
  const hash = scryptSync(password, salt, 32, PARAMS);
  return `${salt.toString('hex')}:${hash.toString('hex')}`;
}

function checkPassword(password, stored) {
  const [saltHex, hashHex] = stored.split(':');
  const hash = scryptSync(password, Buffer.from(saltHex, 'hex'), 32, PARAMS);
  return timingSafeEqual(hash, Buffer.from(hashHex, 'hex'));
}

সাধারণ ল্যাপটপে প্রতিটি কলে কয়েকশো মিলিসেকেন্ড লাগে — একটা লগইনে টের পাওয়া যায় না, কিন্তু অ্যাটাকারের জন্য বিরাট বাধা। Python-এ argon2-cffi আর bcrypt প্যাকেজ একই কাজ করে, আর salt ও cost হ্যাশ স্ট্রিংয়ের ভেতরেই রেখে দেয়। মনে রাখবেন, bcrypt পাসওয়ার্ডের প্রথম ৭২ বাইটই শুধু ব্যবহার করে।

পাসওয়ার্ড কখনোই Base64-এ, উল্টানো যায় এমন এনক্রিপশনে, বা সাধারণ MD5, SHA-1 বা SHA-256 হিসেবে রাখবেন না — salt থাকুক বা না থাকুক।

HMAC: কী-সহ হ্যাশিং

HMAC একটা হ্যাশের সাথে গোপন কী জুড়ে দেয়। কী যার কাছে আছে কেবল সে-ই সঠিক মান বানাতে পারে, তাই এটা প্রমাণ করে মেসেজটা কী-ধারীর কাছ থেকে এসেছে এবং বদলানো হয়নি। পেমেন্ট গেটওয়ে webhook সাইন করতে এটা ব্যবহার করে; HS256 JWT আসলে HMAC-SHA256।

createHmac('sha256', 'my-secret-key').update('amount=500&to=rahim').digest('hex');
// '0c91fba74f9d3a7ec543c117d241883b51d6a9a3df886463b813c6cd94dccd3c'

HMAC তুলনা করুন timingSafeEqual বা Python-এর hmac.compare_digest-এর মতো constant-time ফাংশন দিয়ে, === দিয়ে নয়।

প্রচলিত ভুল ধারণা

নিজেই দেখুন, এনকোডিং কিছু লুকায় না। যেকোনো Base64 স্ট্রিং Base64 টুলে পেস্ট করুন — কোনো কী ছাড়াই সাথে সাথে ডিকোড হয়ে যাবে, পুরোটা আপনার ব্রাউজারেই। এখনই চেষ্টা করুন

প্রশ্নোত্তর

হ্যাশ কি ডিক্রিপ্ট করা যায়?

না। তবে ছোট বা প্রচলিত ইনপুটের দুর্বল বা দ্রুত হ্যাশ অনুমান করা যায়: অ্যাটাকার একের পর এক সম্ভাব্য মান হ্যাশ করতে থাকে যতক্ষণ না মিলে যায়। এজন্যই পাসওয়ার্ডে ধীর, salt-যুক্ত অ্যালগরিদম লাগে।

আমার দেওয়া API key কি এনক্রিপ্ট করব, না হ্যাশ?

হ্যাশ করুন (এখানে SHA-256-এর মতো দ্রুত হ্যাশই চলে, কারণ র‍্যান্ডম কী অনুমান করার পক্ষে অনেক লম্বা) আর পুরো কী ইউজারকে শুধু একবার দেখান। কোনো সিক্রেট যদি পরে আবার দেখাতেই হয় — যেমন আপনার অ্যাপ যে থার্ড-পার্টি কী ব্যবহার করে — তাহলে এনক্রিপ্ট করুন এবং এনক্রিপশন কী সুরক্ষিত রাখুন।

MD5 কি এখনো কোনো কাজে লাগে?

নিরাপত্তার বাইরে, দুর্ঘটনাবশত ফাইল নষ্ট হয়েছে কি না ধরার checksum হিসেবে — হ্যাঁ। পাসওয়ার্ড, সিগনেচার বা অ্যাটাকার প্রভাবিত করতে পারে এমন কিছুর জন্য — না; SHA-256 বা তার চেয়ে ভালো কিছু ব্যবহার করুন।

Related guides

More free tools