?? Irete: Cryptography

Real-world security and cryptographic operations.

Use Case 1: Secure Password Storage

// password_auth.ifa - Secure password hashing and verification

ise hash_password(password) {
    // Generate 16-byte salt
    ayanmo salt = Irete.random_bytes(16);
    
    // Hash password with salt using SHA-256
    ayanmo salted = salt + password;
    ayanmo hash = Irete.sha256(salted);
    
    // Store as salt$hash format
    padap? salt + "$" + hash;
}

ise verify_password(password, stored) {
    // Extract salt and hash
    ayanmo parts = Ika.split(stored, "$");
    ayanmo salt = Ogunda.first(parts);
    ayanmo expected_hash = Ogunda.last(parts);
    
    // Recompute hash with same salt
    ayanmo salted = salt + password;
    ayanmo actual_hash = Irete.sha256(salted);
    
    // Constant-time comparison
    padap? actual_hash == expected_hash;
}

// Usage
ayanmo password = "my_secret_password";
ayanmo stored = hash_password(password);
Irosu.fo("Stored: " + stored);

ti (verify_password("my_secret_password", stored)) {
    Irosu.fo("? Password correct!");
} bib?k? {
    Irosu.fo("? Wrong password");
}

Use Case 2: File Integrity Checker

// integrity.ifa - Detect file tampering

ise compute_checksums(directory) {
    ayanmo checksums = {};
    ayanmo files = Odi.list(directory);
    
    fun file ninu files {
        ayanmo path = directory + "/" + file;
        ti (Odi.is_file(path)) {
            ayanmo content = Odi.read(path);
            ayanmo hash = Irete.sha256(content);
            checksums[file] = hash;
        }
    }
    
    padap? checksums;
}

ise save_manifest(checksums, path) {
    ayanmo json = Ogbe.format_json(checksums);
    Odi.write(path, json);
    Irosu.fo("? Manifest saved with " + Ogunda.len(Ogbe.keys(checksums)) + " files");
}

ise verify_integrity(directory, manifest_path) {
    ayanmo manifest = Ogbe.parse_json(Odi.read(manifest_path));
    ayanmo current = compute_checksums(directory);
    ayanmo issues = [];
    
    // Check for modified files
    fun file key(manifest) {
        ti (current[file] == nil) {
            ayanmo issues = Ogunda.push(issues, {"type": "DELETED", "file": file});
        } bib?k? ti (current[file] != manifest[file]) {
            ayanmo issues = Ogunda.push(issues, {"type": "MODIFIED", "file": file});
        }
    }
    
    // Check for new files
    fun file key(current) {
        ti (manifest[file] == nil) {
            ayanmo issues = Ogunda.push(issues, {"type": "NEW", "file": file});
        }
    }
    
    padap? issues;
}

// Usage
save_manifest(compute_checksums("./src"), "manifest.json");

// Later...
ayanmo issues = verify_integrity("./src", "manifest.json");
ti (Ogunda.len(issues) == 0) {
    Irosu.fo("? All files intact");
} bib?k? {
    Irosu.fo("? Issues found:");
    fun issue ninu issues {
        Irosu.fo("  " + issue["type"] + ": " + issue["file"]);
    }
}

Use Case 3: API Token Generator

// tokens.ifa - Generate secure API tokens

ise generate_api_token(user_id, scope) {
    // Create token payload
    ayanmo payload = {
        "user_id": user_id,
        "scope": scope,
        "created": Iwori.now(),
        "nonce": Irete.uuid()
    };
    
    // Serialize and hash
    ayanmo data = Ogbe.format_json(payload);
    ayanmo signature = Irete.sha256(data + SECRET_KEY);
    
    // Encode as base64
    ayanmo token = Irete.base64_encode(data) + "." + signature;
    
    padap? token;
}

ise validate_token(token) {
    ayanmo parts = Ika.split(token, ".");
    ti (Ogunda.len(parts) != 2) {
        padap? nil;
    }
    
    ayanmo data = Irete.base64_decode(Ogunda.first(parts));
    ayanmo signature = Ogunda.last(parts);
    
    // Verify signature
    ayanmo expected = Irete.sha256(data + SECRET_KEY);
    ti (signature != expected) {
        padap? nil;
    }
    
    // Parse and return payload
    padap? Ogbe.parse_json(data);
}

// Usage
ayanmo SECRET_KEY = "super_secret_key";
ayanmo token = generate_api_token("user123", ["read", "write"]);
Irosu.fo("Token: " + token);

ayanmo payload = validate_token(token);
ti (payload != nil) {
    Irosu.fo("Valid token for user: " + payload["user_id"]);
}

Use Case 4: Data Encryption

// encrypt_data.ifa - Encrypt/decrypt sensitive data

// Simple XOR encryption (use AES in production)
ise xor_encrypt(data, key) {
    ayanmo key_hash = Irete.sha256(key);
    ayanmo result = "";
    ayanmo i = 0;
    
    nigba (i < Ika.len(data)) {
        ayanmo d = Ika.char_at(data, i);
        ayanmo k = Ika.char_at(key_hash, i % 64);
        ayanmo encrypted = Ogbe.xor(d, k);
        ayanmo result = result + Ika.from_char(encrypted);
        ayanmo i = i + 1;
    }
    
    padap? Irete.base64_encode(result);
}

ise xor_decrypt(encrypted, key) {
    ayanmo data = Irete.base64_decode(encrypted);
    ayanmo key_hash = Irete.sha256(key);
    ayanmo result = "";
    ayanmo i = 0;
    
    nigba (i < Ika.len(data)) {
        ayanmo d = Ika.char_at(data, i);
        ayanmo k = Ika.char_at(key_hash, i % 64);
        ayanmo decrypted = Ogbe.xor(d, k);
        ayanmo result = result + Ika.from_char(decrypted);
        ayanmo i = i + 1;
    }
    
    padap? result;
}

// Usage
ayanmo secret = "Credit card: 4111-1111-1111-1111";
ayanmo key = "my_encryption_key";

ayanmo encrypted = xor_encrypt(secret, key);
Irosu.fo("Encrypted: " + encrypted);

ayanmo decrypted = xor_decrypt(encrypted, key);
Irosu.fo("Decrypted: " + decrypted);