mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
Every page now loads /webmcp.js, scripts/webmcp.ts with its types stripped (npm run webmcp). Where the browser has document.modelContext it registers two read-only tools: search_skills over the published skills index, and get_skill, which returns a skill's markdown checked against the index digest. The markdown alternate link moves from build.py to site.py, so CI holds every page to it. The API catalog gains the site itself (llms.txt and the skills index) and hanzo.ai/agents; the live discovery check now fetches every catalog link. tests/site.mjs drives the tools through a recording modelContext, and checks a browser without one loads clean.
657 lines
26 KiB
HTML
657 lines
26 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>cryptography-cryptography-basics</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/cryptography-cryptography-basics">
|
|
<link rel="alternate" type="text/markdown" href="/cryptography-cryptography-basics.md">
|
|
<script type="module" src="/webmcp.js"></script>
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/cryptography-cryptography-basics.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>cryptography-cryptography-basics</h1><p class="lede">Cryptography fundamentals including symmetric/asymmetric encryption, hashing, signing, key exchange, and common algorithms</p><h1>Cryptography Basics</h1>
|
|
<p><strong>Scope</strong>: Symmetric/asymmetric encryption, hashing, digital signatures, key exchange, common algorithms <strong>Lines</strong>: ~380 <strong>Last Updated</strong>: 2025-10-27 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Understanding encryption fundamentals</li><li>Implementing data encryption</li><li>Selecting cryptographic algorithms</li><li>Working with keys and signatures</li><li>Building secure authentication</li><li>Hashing passwords or data</li><li>Understanding TLS/PKI foundations</li><li>Making security architecture decisions</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Cryptographic Primitives</h3>
|
|
<pre><code>1. Encryption/Decryption → Confidentiality
|
|
2. Hashing → Integrity
|
|
3. Digital Signatures → Authentication + Integrity
|
|
4. Key Exchange → Secure channel establishment</code></pre>
|
|
<hr>
|
|
<h2>Symmetric Encryption</h2>
|
|
<h3>Overview</h3>
|
|
<p><strong>Same key for encryption and decryption</strong>:</p>
|
|
<pre><code>Plaintext → [Encrypt with Key] → Ciphertext
|
|
Ciphertext → [Decrypt with Key] → Plaintext</code></pre>
|
|
<p><strong>Characteristics</strong>:</p>
|
|
<ul><li>Fast (hardware-accelerated)</li><li>Key must be shared securely</li><li>Used for bulk data encryption</li></ul>
|
|
<h3>Common Algorithms</h3>
|
|
<p><strong>AES (Advanced Encryption Standard)</strong>:</p>
|
|
<pre><code>Key sizes: 128, 192, 256 bits
|
|
Block size: 128 bits
|
|
Modes: GCM (recommended), CBC, CTR
|
|
Status: Industry standard, secure</code></pre>
|
|
<p><strong>ChaCha20</strong>:</p>
|
|
<pre><code>Key size: 256 bits
|
|
Stream cipher
|
|
Often paired with Poly1305 (MAC)
|
|
Status: Modern, secure, fast on mobile</code></pre>
|
|
<h3>Python Example (AES-GCM)</h3>
|
|
<pre><code class="lang-python">from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
import os
|
|
|
|
# Generate key
|
|
key = AESGCM.generate_key(bit_length=256)
|
|
aesgcm = AESGCM(key)
|
|
|
|
# Encrypt
|
|
nonce = os.urandom(12) # 96 bits for GCM
|
|
plaintext = b"Secret message"
|
|
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
|
|
|
# Decrypt
|
|
recovered = aesgcm.decrypt(nonce, ciphertext, None)
|
|
assert recovered == plaintext</code></pre>
|
|
<h3>Go Example (AES-GCM)</h3>
|
|
<pre><code class="lang-go">package main
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"io"
|
|
)
|
|
|
|
func encrypt(key, plaintext []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Prepend nonce to ciphertext
|
|
ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
|
|
return ciphertext, nil
|
|
}
|
|
|
|
func decrypt(key, ciphertext []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
|
|
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
return plaintext, err
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Asymmetric Encryption</h2>
|
|
<h3>Overview</h3>
|
|
<p><strong>Different keys for encryption and decryption</strong>:</p>
|
|
<pre><code>Public Key (share freely) → Encrypt
|
|
Private Key (keep secret) → Decrypt
|
|
|
|
Anyone can encrypt with public key
|
|
Only holder of private key can decrypt</code></pre>
|
|
<p><strong>Characteristics</strong>:</p>
|
|
<ul><li>Slow (1000x slower than symmetric)</li><li>No need to share secret</li><li>Used for key exchange, digital signatures</li></ul>
|
|
<h3>Common Algorithms</h3>
|
|
<p><strong>RSA</strong>:</p>
|
|
<pre><code>Key sizes: 2048, 3072, 4096 bits
|
|
Use cases: Key exchange, signatures
|
|
Status: Widely supported, slower</code></pre>
|
|
<p><strong>ECDSA (Elliptic Curve)</strong>:</p>
|
|
<pre><code>Key sizes: 256, 384, 521 bits
|
|
Use cases: Signatures (TLS, Bitcoin)
|
|
Status: Modern, faster, smaller keys</code></pre>
|
|
<p><strong>Ed25519</strong>:</p>
|
|
<pre><code>Key size: 256 bits
|
|
Use cases: Signatures (SSH, crypto)
|
|
Status: Modern, fast, secure</code></pre>
|
|
<h3>Python Example (RSA)</h3>
|
|
<pre><code class="lang-python">from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
# Generate key pair
|
|
private_key = rsa.generate_private_key(
|
|
public_exponent=65537,
|
|
key_size=2048
|
|
)
|
|
public_key = private_key.public_key()
|
|
|
|
# Encrypt with public key
|
|
plaintext = b"Secret message"
|
|
ciphertext = public_key.encrypt(
|
|
plaintext,
|
|
padding.OAEP(
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
algorithm=hashes.SHA256(),
|
|
label=None
|
|
)
|
|
)
|
|
|
|
# Decrypt with private key
|
|
recovered = private_key.decrypt(
|
|
ciphertext,
|
|
padding.OAEP(
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
algorithm=hashes.SHA256(),
|
|
label=None
|
|
)
|
|
)
|
|
assert recovered == plaintext</code></pre>
|
|
<h3>Rust Example (Ed25519 signatures)</h3>
|
|
<pre><code class="lang-rust">use ed25519_dalek::{Keypair, Signature, Signer, Verifier};
|
|
use rand::rngs::OsRng;
|
|
|
|
fn main() {
|
|
let mut csprng = OsRng{};
|
|
let keypair: Keypair = Keypair::generate(&mut csprng);
|
|
|
|
// Sign
|
|
let message = b"Important message";
|
|
let signature: Signature = keypair.sign(message);
|
|
|
|
// Verify
|
|
assert!(keypair.public.verify(message, &signature).is_ok());
|
|
}</code></pre>
|
|
<hr>
|
|
<h2>Hashing</h2>
|
|
<h3>Overview</h3>
|
|
<p><strong>One-way function</strong>:</p>
|
|
<pre><code>Input (any size) → [Hash Function] → Fixed-size output
|
|
|
|
Properties:
|
|
- Deterministic (same input → same output)
|
|
- Fast to compute
|
|
- Infeasible to reverse
|
|
- Collision-resistant</code></pre>
|
|
<h3>Common Algorithms</h3>
|
|
<p><strong>SHA-256</strong> (Secure Hash Algorithm):</p>
|
|
<pre><code>Output: 256 bits (32 bytes)
|
|
Use cases: Integrity, signatures, Bitcoin
|
|
Status: Industry standard</code></pre>
|
|
<p><strong>SHA-3</strong>:</p>
|
|
<pre><code>Output: 224, 256, 384, 512 bits
|
|
Use cases: Same as SHA-256
|
|
Status: Modern alternative</code></pre>
|
|
<p><strong>BLAKE2</strong>:</p>
|
|
<pre><code>Output: Configurable
|
|
Use cases: General hashing
|
|
Status: Faster than SHA-2, secure</code></pre>
|
|
<h3>Python Examples</h3>
|
|
<pre><code class="lang-python">import hashlib
|
|
|
|
# SHA-256
|
|
data = b"Data to hash"
|
|
hash_digest = hashlib.sha256(data).hexdigest()
|
|
print(f"SHA-256: {hash_digest}")
|
|
|
|
# BLAKE2b (faster)
|
|
hash_digest = hashlib.blake2b(data).hexdigest()
|
|
print(f"BLAKE2: {hash_digest}")
|
|
|
|
# File hashing
|
|
def hash_file(filename):
|
|
sha256 = hashlib.sha256()
|
|
with open(filename, 'rb') as f:
|
|
while chunk := f.read(8192):
|
|
sha256.update(chunk)
|
|
return sha256.hexdigest()</code></pre>
|
|
<hr>
|
|
<h2>Password Hashing</h2>
|
|
<h3>Overview</h3>
|
|
<p><strong>Different from general hashing</strong>:</p>
|
|
<pre><code>Requirements:
|
|
- Slow (prevent brute force)
|
|
- Salt (prevent rainbow tables)
|
|
- Memory-hard (resist GPU attacks)</code></pre>
|
|
<h3>Algorithms</h3>
|
|
<p><strong>bcrypt</strong>:</p>
|
|
<pre><code class="lang-python">import bcrypt
|
|
|
|
# Hash password
|
|
password = b"user_password"
|
|
salt = bcrypt.gensalt(rounds=12) # Cost factor
|
|
hashed = bcrypt.hashpw(password, salt)
|
|
|
|
# Verify password
|
|
if bcrypt.checkpw(password, hashed):
|
|
print("Password correct")</code></pre>
|
|
<p><strong>Argon2</strong> (modern, recommended):</p>
|
|
<pre><code class="lang-python">from argon2 import PasswordHasher
|
|
|
|
ph = PasswordHasher()
|
|
|
|
# Hash password
|
|
hashed = ph.hash("user_password")
|
|
|
|
# Verify password
|
|
try:
|
|
ph.verify(hashed, "user_password")
|
|
print("Password correct")
|
|
except:
|
|
print("Password incorrect")</code></pre>
|
|
<p><strong>scrypt</strong>:</p>
|
|
<pre><code class="lang-python">import hashlib
|
|
|
|
password = b"user_password"
|
|
salt = os.urandom(16)
|
|
|
|
# Hash with scrypt (memory-hard)
|
|
key = hashlib.scrypt(
|
|
password,
|
|
salt=salt,
|
|
n=2**14, # CPU/memory cost
|
|
r=8, # Block size
|
|
p=1, # Parallelization
|
|
dklen=32 # Key length
|
|
)</code></pre>
|
|
<hr>
|
|
<h2>Digital Signatures</h2>
|
|
<h3>Overview</h3>
|
|
<p><strong>Authenticate + Integrity</strong>:</p>
|
|
<pre><code>1. Hash the message
|
|
2. Encrypt hash with private key → Signature
|
|
3. Anyone can verify with public key
|
|
|
|
Proves:
|
|
- Message came from private key holder
|
|
- Message wasn't modified</code></pre>
|
|
<h3>Python Example</h3>
|
|
<pre><code class="lang-python">from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
# Generate key pair
|
|
private_key = rsa.generate_private_key(
|
|
public_exponent=65537,
|
|
key_size=2048
|
|
)
|
|
public_key = private_key.public_key()
|
|
|
|
# Sign message
|
|
message = b"Important document"
|
|
signature = private_key.sign(
|
|
message,
|
|
padding.PSS(
|
|
mgf=padding.MGF1(hashes.SHA256()),
|
|
salt_length=padding.PSS.MAX_LENGTH
|
|
),
|
|
hashes.SHA256()
|
|
)
|
|
|
|
# Verify signature
|
|
try:
|
|
public_key.verify(
|
|
signature,
|
|
message,
|
|
padding.PSS(
|
|
mgf=padding.MGF1(hashes.SHA256()),
|
|
salt_length=padding.PSS.MAX_LENGTH
|
|
),
|
|
hashes.SHA256()
|
|
)
|
|
print("Signature valid")
|
|
except:
|
|
print("Signature invalid")</code></pre>
|
|
<hr>
|
|
<h2>Key Exchange</h2>
|
|
<h3>Diffie-Hellman</h3>
|
|
<p><strong>Agree on shared secret over insecure channel</strong>:</p>
|
|
<pre><code>Alice Bob
|
|
private_a private_b
|
|
↓ ↓
|
|
public_a → -------→ public_b ← public_b
|
|
+ +
|
|
public_b public_a
|
|
↓ ↓
|
|
shared_secret shared_secret</code></pre>
|
|
<p><strong>Python Example</strong>:</p>
|
|
<pre><code class="lang-python">from cryptography.hazmat.primitives.asymmetric import dh
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
# Generate parameters (can be reused)
|
|
parameters = dh.generate_parameters(generator=2, key_size=2048)
|
|
|
|
# Alice generates keypair
|
|
alice_private = parameters.generate_private_key()
|
|
alice_public = alice_private.public_key()
|
|
|
|
# Bob generates keypair
|
|
bob_private = parameters.generate_private_key()
|
|
bob_public = bob_private.public_key()
|
|
|
|
# Both derive same shared secret
|
|
alice_shared = alice_private.exchange(bob_public)
|
|
bob_shared = bob_private.exchange(alice_public)
|
|
|
|
assert alice_shared == bob_shared</code></pre>
|
|
<hr>
|
|
<h2>Message Authentication Codes (MAC)</h2>
|
|
<h3>HMAC</h3>
|
|
<p><strong>Hash-based MAC</strong>:</p>
|
|
<pre><code class="lang-python">import hmac
|
|
import hashlib
|
|
|
|
key = b"shared_secret"
|
|
message = b"Message to authenticate"
|
|
|
|
# Create MAC
|
|
mac = hmac.new(key, message, hashlib.sha256).digest()
|
|
|
|
# Verify MAC
|
|
def verify_mac(message, mac, key):
|
|
expected_mac = hmac.new(key, message, hashlib.sha256).digest()
|
|
return hmac.compare_digest(mac, expected_mac) # Constant-time comparison</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Pattern 1: Hybrid Encryption</h3>
|
|
<p><strong>Use asymmetric for key exchange, symmetric for data</strong>:</p>
|
|
<pre><code class="lang-python">from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
|
from cryptography.hazmat.primitives import hashes
|
|
import os
|
|
|
|
def hybrid_encrypt(public_key, plaintext):
|
|
# Generate random symmetric key
|
|
symmetric_key = AESGCM.generate_key(bit_length=256)
|
|
aesgcm = AESGCM(symmetric_key)
|
|
|
|
# Encrypt data with symmetric key
|
|
nonce = os.urandom(12)
|
|
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
|
|
|
# Encrypt symmetric key with public key
|
|
encrypted_key = public_key.encrypt(
|
|
symmetric_key,
|
|
padding.OAEP(
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
algorithm=hashes.SHA256(),
|
|
label=None
|
|
)
|
|
)
|
|
|
|
return {
|
|
'encrypted_key': encrypted_key,
|
|
'nonce': nonce,
|
|
'ciphertext': ciphertext
|
|
}
|
|
|
|
def hybrid_decrypt(private_key, encrypted_data):
|
|
# Decrypt symmetric key
|
|
symmetric_key = private_key.decrypt(
|
|
encrypted_data['encrypted_key'],
|
|
padding.OAEP(
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
algorithm=hashes.SHA256(),
|
|
label=None
|
|
)
|
|
)
|
|
|
|
# Decrypt data
|
|
aesgcm = AESGCM(symmetric_key)
|
|
plaintext = aesgcm.decrypt(
|
|
encrypted_data['nonce'],
|
|
encrypted_data['ciphertext'],
|
|
None
|
|
)
|
|
|
|
return plaintext</code></pre>
|
|
<hr>
|
|
<h2>Best Practices</h2>
|
|
<h3>1. Use Authenticated Encryption</h3>
|
|
<pre><code class="lang-python"># ✅ Good: AES-GCM (includes authentication)
|
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
|
|
# ❌ Bad: AES-CBC without MAC (vulnerable)
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes</code></pre>
|
|
<h3>2. Generate Random Keys Properly</h3>
|
|
<pre><code class="lang-python"># ✅ Good: Cryptographically secure
|
|
import secrets
|
|
key = secrets.token_bytes(32)
|
|
|
|
# ❌ Bad: Predictable
|
|
import random
|
|
key = random.randbytes(32) # NOT cryptographically secure</code></pre>
|
|
<h3>3. Use High-Level APIs</h3>
|
|
<pre><code class="lang-python"># ✅ Good: Use established libraries
|
|
from cryptography.fernet import Fernet
|
|
|
|
# ❌ Bad: Roll your own crypto
|
|
def my_custom_encryption(data):
|
|
return bytes([b ^ 42 for b in data]) # Insecure!</code></pre>
|
|
<hr>
|
|
<h2>Algorithm Selection Guide</h2>
|
|
<p>| Use Case | Algorithm | |----------|-----------| | Bulk encryption | AES-256-GCM | | Key exchange | ECDH (X25519) | | Digital signatures | Ed25519 or ECDSA P-256 | | Password hashing | Argon2id | | General hashing | SHA-256 or BLAKE2 | | Message auth | HMAC-SHA256 |</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>cryptography-pki-fundamentals</code> - Certificates and PKI</li><li><code>cryptography-tls-configuration</code> - TLS implementation</li><li><code>cryptography-crypto-best-practices</code> - Security guidelines</li><li><code>security-authentication</code> - Auth patterns</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-27</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/cryptography-cryptography-basics.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |