### RAW_blake2: BLAKE2 Hashing Variants Source: https://context7.com/tannerdsilva/rawdog/llms.txt Supports BLAKE2 variants (B, S, BP, SP) with generic Hasher. Output can be static buffer, [UInt8], or raw pointer. Requires importing RAW_blake2. ```swift import RAW_blake2 // Unkeyed blake2b with static Hash output (64 bytes) var b2b = try RAW_blake2.Hasher() try b2b.update(Array("hello".utf8) as [UInt8]) let digest = try b2b.finish() // returns B.Hash (64 bytes) digest.RAW_access { buf in print("BLAKE2b:", buf.map { String(format:"%02x",$0) }.joined()) } // Keyed blake2b with [UInt8] output at custom length let key: [UInt8] = Array(repeating: 0x42, count: 32) var keyed = try RAW_blake2.Hasher(key: key, outputCount: 32) try keyed.update(Array("message".utf8) as [UInt8]) let keyedDigest: [UInt8] = try keyed.finish() print("keyed BLAKE2b (\(keyedDigest.count) bytes):", keyedDigest.map { String(format:"%02x",$0) }.joined()) // HMAC-BLAKE2b let mac = try RAW_blake2.Hasher.hmac( key: key as [UInt8], message: Array("authenticate".utf8) as [UInt8] ) ``` -------------------------------- ### SHA-256 Hashing with RAW_sha256 Source: https://context7.com/tannerdsilva/rawdog/llms.txt Demonstrates both streaming and one-shot SHA-256 hashing using the RAW_sha256 module. Produces a 32-byte hash output. ```swift import RAW_sha256 // Streaming var hasher = RAW_sha256.Hasher() hasher.update(Array("part1".utf8)) hasher.update(Array("part2".utf8)) var hash: RAW_sha256.Hash? = nil try hasher.finish(into: &hash) hash!.RAW_access { print("SHA-256:", $0.map { String(format:"%02x",$0) }.joined()) } // One-shot let oneShot: RAW_sha256.Hash = try RAW_sha256.Hasher.hash(Array("hello".utf8)) ``` -------------------------------- ### Ed25519 Signatures with Blinding and Verification Contexts Source: https://context7.com/tannerdsilva/rawdog/llms.txt Demonstrates Ed25519 signing and verification, including support for blinded signing contexts and reusable verification contexts for batch verification. ```swift import RAW_ed25519 import RAW_dh25519 // Generate a Curve25519 seed key for the signing pair let seed = try MemoryGuarded.new() // Unblinded key generation and signing let (publicKey, privateKey) = try RAW_ed25519.generateKeys(secretKey: seed) let message: [UInt8] = Array("sign this message".utf8) var sigBytes = [UInt8](repeating: 0, count: 64) sigBytes.withUnsafeMutableBytes { RAW_ed25519.sign( to: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), privateKey: privateKey, message: UnsafeBufferPointer(start: message, count: message.count) ) } // Verify let isValid = sigBytes.withUnsafeBytes { sigPtr in message.withUnsafeBytes { RAW_ed25519.verify( signature: sigPtr.baseAddress!.assumingMemoryBound(to: UInt8.self), publicKey: publicKey, message: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: message.count) ) } } print("signature valid:", isValid) // true // Blinded signing for improved key hardening let blindingSeed = try generateSecureRandomBytes(count: 64) let ctx = blindingSeed.withUnsafeBytes { RAW_ed25519.BlindingContext(randomSource: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: 64)) } let (bPub, bPriv) = try ctx.generateKeys(secretKey: seed) // Reusable verification context for batch verification let verifyCtx = RAW_ed25519.VerificationContext(publicKey: bPub) let valid = sigBytes.withUnsafeBytes { sigPtr in message.withUnsafeBytes { verifyCtx.verify( signature: sigPtr.baseAddress!.assumingMemoryBound(to: UInt8.self), message: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: message.count) ) } } ``` -------------------------------- ### RAW_hasher Protocol Usage for Hashing Source: https://context7.com/tannerdsilva/rawdog/llms.txt Demonstrates one-shot and streaming hashing using the RAW_hasher protocol, including HMAC generation. Requires specific hashing algorithm imports. ```swift import RAW import RAW_sha256 // One-shot hash via static convenience let data: [UInt8] = Array("hello world".utf8) let digest: RAW_sha256.Hash = try RAW_sha256.Hasher.hash(data) digest.RAW_access { buf in print("SHA-256 length:", buf.count) // 32 } // Streaming hash var hasher = RAW_sha256.Hasher() hasher.update(data) let moreData: [UInt8] = Array(" and more".utf8) hasher.update(moreData) var result: RAW_sha256.Hash? = nil try hasher.finish(into: &result) print(result != nil) // true // HMAC via protocol extension let key: [UInt8] = Array("secret".utf8) let message: [UInt8] = Array("authenticate this".utf8) let mac: RAW_sha256.Hash = try RAW_sha256.Hasher.hmac(key: key, message: message) mac.RAW_access { buf in print("HMAC-SHA256 length:", buf.count) // 32 } ``` -------------------------------- ### RAW_hmac: Generic HMAC Implementation Source: https://context7.com/tannerdsilva/rawdog/llms.txt Builds HMAC over any RAW_hasher with streaming updates. Supports one-shot via protocol extension. Requires importing RAW_hmac and a specific hasher like RAW_sha256. ```swift import RAW_hmac import RAW_sha256 // Build HMAC-SHA256 with streaming updates let key: [UInt8] = Array("secret-key".utf8) var hmac = try HMAC>(key: key) hmac.update(message: Array("part 1 ".utf8) as [UInt8]) hmac.update(message: Array("part 2".utf8) as [UInt8]) let result: RAW_sha256.Hash = try hmac.finish() result.RAW_access { buf in print("HMAC-SHA256:", buf.map { String(format:"%02x",$0) }.joined()) } // One-shot via protocol extension (any RAW_hasher) let oneShot: RAW_sha256.Hash = try RAW_sha256.Hasher.hmac( key: Array("key".utf8) as [UInt8], message: Array("message".utf8) as [UInt8] ) ``` -------------------------------- ### RAW_md5: Compute MD5 Hash Source: https://context7.com/tannerdsilva/rawdog/llms.txt Computes a 16-byte MD5 hash for legacy compatibility. Requires importing RAW_md5. ```swift import RAW_md5 var hasher = RAW_md5.Hasher() hasher.update(Array("hello".utf8)) var hash: RAW_md5.Hash? = nil try hasher.finish(into: &hash) hash!.RAW_access { buf in print("MD5:", buf.map { String(format:"%02x",$0) }.joined()) // MD5: 5d41402abc4b2a76b9719d911017c592 } ``` -------------------------------- ### Define RawDouble with BinaryFloatingPoint Type Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use `@RAW_staticbuff_binaryfloatingpoint_type` for types like `Double` to use their native memory layout. Initialize with `RAW_native:` and recover with `.RAW_native()`. ```swift import RAW @RAW_staticbuff(bytes:8) @RAW_staticbuff_binaryfloatingpoint_type() public struct RawDouble: Sendable, Comparable, Equatable {} let d = RawDouble(RAW_native: 3.14159) let recovered: Double = d.RAW_native() print(recovered) // 3.14159 // Raw bytes reflect the IEEE 754 double layout d.RAW_access { buf in print("bytes:", Array(buf)) } ``` -------------------------------- ### RAW_bcrypt_blowfish: BCrypt Password Hashing Source: https://context7.com/tannerdsilva/rawdog/llms.txt Implements BCrypt blowfish password hashing. Generates salts with a specified cost factor (default 12). Requires importing RAW_bcrypt_blowfish. ```swift import RAW_bcrypt_blowfish // Generate a bcrypt salt with cost factor 12 let salt = try RAW_bcrypt_blowfish.Salt.generate(passes: 12) // Hash a password let hashBytes = try RAW_bcrypt_blowfish.hash(phrase: "my-secret-password", salt: salt) let hashString = String(bytes: hashBytes.prefix(while: { $0 != 0 }), encoding: .utf8)! print("bcrypt hash:", hashString) // e.g. "$2b$12$... // Verify: re-hash with the same salt and compare let verifySalt = try RAW_bcrypt_blowfish.Salt.generate(passes: 12) let verifyHash = try RAW_bcrypt_blowfish.hash(phrase: "my-secret-password", salt: verifySalt) // In practice, extract the salt from the stored hash string before re-hashing ``` -------------------------------- ### SHA-1 Hashing with RAW_sha1 Source: https://context7.com/tannerdsilva/rawdog/llms.txt Calculates a SHA-1 hash using the RAW_sha1 module. This is provided for legacy compatibility and produces a 20-byte output. ```swift import RAW_sha1 var hasher = RAW_sha1.Hasher() let data: [UInt8] = Array("rawdog".utf8) hasher.update(data) var result: RAW_sha1.Hash? = nil try hasher.finish(into: &result) result!.RAW_access { let hex = $0.map { String(format: "%02x", $0) }.joined() print("SHA-1:", hex) // SHA-1: e14ef56e13f74f11fe7f94f10d33fe0d23f2c4f7 (example) } ``` -------------------------------- ### Create MyString with Convertible String Type Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use `@RAW_convertible_string_type` for Unicode-string-backed types. Specify a `RAW_encoded_fixedwidthinteger` for length prefixing. Synthesizes initializers, encoding, decoding, and comparison. ```swift import RAW // A big-endian UInt32 for length prefix @RAW_staticbuff(bytes:4) @RAW_staticbuff_fixedwidthinteger_type(bigEndian: true) public struct UInt32BE: Sendable, Comparable, Equatable {} // A length-prefixed UTF-8 string type @RAW_convertible_string_type(backing: UInt32BE.self) public struct MyString: Sendable, Comparable, Equatable {} // Construct from Swift String let s = MyString("hello, rawdog") // Encode to bytes and decode back var encoded = [UInt8](repeating: 0, count: 4 + 13) encoded.withUnsafeMutableBytes { ptr in _ = s.RAW_encode(dest: ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)) } let decoded = MyString(RAW_decode: encoded, count: encoded.count) print(String(decoded!)) // "hello, rawdog" // Compare two strings lexicographically let a = MyString("apple") let b = MyString("banana") print(a < b) // true ``` -------------------------------- ### Base64 Encoding and Decoding Source: https://context7.com/tannerdsilva/rawdog/llms.txt Enables encoding byte arrays to Base64 strings and decoding Base64 strings back to byte arrays. Supports padding and encoding from buffer pointers. ```swift import RAW_base64 // Encode bytes to base64 let bytes: [UInt8] = Array("hello, rawdog".utf8) let encoded = RAW_base64.encode(bytes) let b64String = String(encoded) print(b64String) // "aGVsbG8sIHJhd2RvZw==" // Decode a base64 string back to bytes let decoded = try RAW_base64.decode("aGVsbG8sIHJhd2RvZw==") print(String(bytes: decoded, encoding: .utf8)!) // "hello, rawdog" // Encode from an UnsafeBufferPointer let moreBytes: [UInt8] = [0xFF, 0x00, 0xAB] let ptrEncoded = moreBytes.withUnsafeBufferPointer { RAW_base64.encode($0) } print(String(ptrEncoded)) // "/wCr" // Decode from Encoded struct directly (no-throw, lossless) let decodedDirect = RAW_base64.decode(encoded) print(decodedDirect == bytes) // true ``` -------------------------------- ### SHA-512 Hashing with RAW_sha512 Source: https://context7.com/tannerdsilva/rawdog/llms.txt Performs SHA-512 hashing using the RAW_sha512 module, producing a 64-byte hash output. Supports streaming updates. ```swift import RAW_sha512 var hasher = RAW_sha512.Hasher() hasher.update(Array("data".utf8)) var hash: RAW_sha512.Hash? = nil try hasher.finish(into: &hash) hash!.RAW_access { print("SHA-512 bytes:", $0.count) // 64 } ``` -------------------------------- ### Generate Random and Secure Bytes Source: https://context7.com/tannerdsilva/rawdog/llms.txt Generates random bytes from /dev/urandom or cryptographically secure bytes using OS entropy. Includes secure zeroing utilities for mutable buffers. ```swift import RAW // Generate 32 non-cryptographic random bytes let random32 = try generateRandomBytes(count: 32) print(random32.count) // 32 // Generate up to 256 cryptographically secure bytes let secure16 = try generateSecureRandomBytes(count: 16) print(secure16.count) // 16 // Generate secure random bytes directly into a RAW_staticbuff type @RAW_staticbuff(bytes:32) struct Nonce: Sendable {} let nonce = try generateSecureRandomBytes(as: Nonce.self) // Securely zero a mutable buffer var secret = [UInt8](repeating: 0xFF, count: 64) try secret.withUnsafeMutableBytes { try secureZeroBytes($0.baseAddress!, count: $0.count) } print(secret.allSatisfy { $0 == 0 }) // true // Securely zero via UnsafeMutableRawBufferPointer overload var buf = [UInt8](repeating: 0xAA, count: 32) try buf.withUnsafeMutableBytes { try secureZeroBytes(UnsafeMutableRawBufferPointer($0)) } ``` -------------------------------- ### Hexadecimal Encoding and Decoding Source: https://context7.com/tannerdsilva/rawdog/llms.txt Provides functions for encoding byte arrays into hexadecimal strings and decoding hexadecimal strings back into byte arrays. Supports various input types and random hex value generation. ```swift import RAW_hex // Encode bytes to hex let bytes: [UInt8] = [0xDE, 0xAD, 0xBE, 0xEF] let encoded = RAW_hex.encode(bytes) let hexString = String(encoded) print(hexString) // "deadbeef" // Decode hex string back to bytes let decoded = try RAW_hex.decode("deadbeef") print(decoded) // [222, 173, 190, 239] // Generate random hex values let randomHex = [RAW_hex.Value].random(count: 8) let randomStr = String(RAW_hex.Encoded.from(encoded: randomHex) ?? RAW_hex.encode([])) print("random hex:", randomStr) // Encode a RAW_staticbuff type directly @RAW_staticbuff(bytes:4) struct FourBytes: Sendable {} let fb = FourBytes(RAW_staticbuff: [0x01, 0x02, 0x03, 0x04]) let fbEncoded = RAW_hex.encode(fb) print(String(fbEncoded)) // "01020304" ``` -------------------------------- ### Define 32-byte static buffer with RAW_staticbuff Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use the `@RAW_staticbuff` macro to define a fixed-size byte buffer type. This macro generates boilerplate for initialization, access, comparison, and zeroing. The struct must be Sendable and declare no instance variables. ```swift import RAW // Define a 32-byte static buffer type (e.g. a hash output or key) @RAW_staticbuff(bytes:32) public struct MyHash: Sendable, Hashable, Comparable, Equatable {} // Zero-initialize let zero = MyHash(RAW_staticbuff: MyHash.RAW_staticbuff_zeroed()) // Initialize from raw bytes let bytes: [UInt8] = Array(repeating: 0xAB, count: 32) let fromBytes = MyHash(RAW_staticbuff: bytes) // Access underlying bytes as UnsafeBufferPointer fromBytes.RAW_access { buffer in print("byte count:", buffer.count) // 32 print("first byte:", buffer[0]) // 171 } // Bitwise invert (produces a new value with all bits flipped) let inverted = ~fromBytes // Compare two values (lexicographic on raw bytes) print(zero < fromBytes) // true print(zero == fromBytes) // false // Iterate over bytes for byte in fromBytes { // byte: UInt8 } // Encode to a destination pointer var dest = [UInt8](repeating: 0, count: 32) dest.withUnsafeMutableBytes { ptr in fromBytes.RAW_encode(dest: ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)) } ``` -------------------------------- ### RAW_argon2: Argon2id Key Derivation Source: https://context7.com/tannerdsilva/rawdog/llms.txt Performs memory-hard password hashing/KDF using Argon2id. Output can be any fixed-length RAW_staticbuff. Requires importing RAW_argon2. ```swift import RAW_argon2 // Define a 32-byte output type for the derived key @RAW_staticbuff(bytes:32) struct DerivedKey: Sendable {} // Hash a password with Argon2id let password: [UInt8] = Array("correct horse battery staple".utf8) let salt: [UInt8] = Array("unique-per-user-salt".utf8) let derivedKey = try RAW_argon2.ID.hash( password: password, salt: salt, timeCost: 3, // iterations memoryCost: 65536, // 64 MiB parallelism: 1, as: DerivedKey.self ) derivedKey.RAW_access { buf in print("Argon2id key (\(buf.count) bytes):", buf.map { String(format:"%02x",$0) }.joined()) } // Verify by re-hashing and comparing let candidateKey = try RAW_argon2.ID.hash( password: Array("correct horse battery staple".utf8), salt: salt, timeCost: 3, memoryCost: 65536, parallelism: 1, as: DerivedKey.self ) print("keys match:", derivedKey == candidateKey) // true ``` -------------------------------- ### ChaCha20-Poly1305 AEAD Encryption and Decryption Source: https://context7.com/tannerdsilva/rawdog/llms.txt Demonstrates authenticated encryption with associated data using ChaCha20-Poly1305. Requires a 16- or 32-byte key and a 12-byte nonce. The encrypt function returns a 16-byte tag, and decrypt verifies this tag, throwing an error on mismatch. ```swift import RAW_chachapoly // 32-byte key let keyBytes: [UInt8] = try generateSecureRandomBytes(count: 32) var ctx = keyBytes.withUnsafeBytes { RAW_chachapoly.Context(key: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: 32)) }! // 12-byte nonce let nonceBytes: [UInt8] = try generateSecureRandomBytes(count: 12) let nonce = RAW_chachapoly.Nonce(RAW_staticbuff: nonceBytes) let plaintext: [UInt8] = Array("secret message".utf8) let aad: [UInt8] = Array("additional data".utf8) var ciphertext = [UInt8](repeating: 0, count: plaintext.count) // Encrypt let tag = try plaintext.withUnsafeBytes { aad.withUnsafeBytes { ciphertext.withUnsafeMutableBytes { try ctx.encrypt( nonce: nonce, associatedData: UnsafeBufferPointer(start: $1.baseAddress!.assumingMemoryBound(to: UInt8.self), count: aad.count), inputData: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: plaintext.count), output: $2.baseAddress!.assumingMemoryBound(to: UInt8.self) ) } } } // Decrypt var recovered = [UInt8](repeating: 0, count: plaintext.count) ciphertext.withUnsafeBytes { aad.withUnsafeBytes { recovered.withUnsafeMutableBytes { try ctx.decrypt( tag: tag, nonce: nonce, associatedData: UnsafeBufferPointer(start: $1.baseAddress!.assumingMemoryBound(to: UInt8.self), count: aad.count), inputData: UnsafeBufferPointer(start: $0.baseAddress!.assumingMemoryBound(to: UInt8.self), count: ciphertext.count), output: $2.baseAddress!.assumingMemoryBound(to: UInt8.self) ) } } } print("decrypted:", String(bytes: recovered, encoding: .utf8)!) ``` -------------------------------- ### Encode and Decode Entropy to Mnemonic in Swift Source: https://context7.com/tannerdsilva/rawdog/llms.txt Encodes 16 bytes of entropy into a 12-word mnemonic and decodes it back, verifying the checksum. Handles potential checksum mismatch or unknown word errors. ```swift import RAW_mnemonic // Encode 16 bytes of entropy to a 12-word mnemonic let entropy: [UInt8] = try generateSecureRandomBytes(count: 16) let words = try entropy.withUnsafeBufferPointer { try Mnemonic.encode($0) } print("mnemonic (" + "\(words.count) words):", words.joined(separator: " ")) // e.g. "abandon ability able about above absent absorb abstract absurd abuse access accident" // Decode back to bytes (verifies checksum) var recovered = [UInt8](repeating: 0, count: 16) try recovered.withUnsafeMutableBytes { try Mnemonic.decode(words, into: $0.baseAddress!.assumingMemoryBound(to: UInt8.self)) } print("entropy matches:", entropy == recovered) // true // Checksum mismatch error example do { var corruptWords = words corruptWords[0] = "abandon" corruptWords[1] = "zoo" var out = [UInt8](repeating: 0, count: 16) try out.withUnsafeMutableBytes { try Mnemonic.decode(corruptWords, into: $0.baseAddress!.assumingMemoryBound(to: UInt8.self)) } } catch { print("error:", error) // checksumMismatch or unknownWord } // 24-word mnemonic from 32 bytes let entropy32: [UInt8] = try generateSecureRandomBytes(count: 32) let words24 = try entropy32.withUnsafeBufferPointer { try Mnemonic.encode($0) } print("24-word mnemonic word count:", words24.count) // 24 ``` -------------------------------- ### Create fixed-width integer type with @RAW_staticbuff_fixedwidthinteger_type Source: https://context7.com/tannerdsilva/rawdog/llms.txt Extend a `@RAW_staticbuff`-decorated struct to represent a fixed-width integer. This macro adds initializers and accessors for native integer types and enables numeric comparison based on value. ```swift import RAW @RAW_staticbuff(bytes:8) @RAW_staticbuff_fixedwidthinteger_type(bigEndian: true) public struct BigEndianUInt64: Sendable, Comparable, Equatable {} // Construct from native integer let val = BigEndianUInt64(RAW_native: 0x0102030405060708) // Recover the native integer let recovered: UInt64 = val.RAW_native() print(recovered) // 72623859790382856 (= 0x0102030405060708) // The raw bytes are in big-endian order val.RAW_access { buf in print(Array(buf)) // [1, 2, 3, 4, 5, 6, 7, 8] } // Numeric comparison is by value let small = BigEndianUInt64(RAW_native: 1) let large = BigEndianUInt64(RAW_native: 1000) print(small < large) // true ``` -------------------------------- ### Decode Fixed-Size LE32 Type Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use `RAW_convertible_fixed` for types with compile-time size checks. `LE32` decodes from a raw pointer and byte count, returning `nil` if the count does not match `MemoryLayout.size`. ```swift import RAW @RAW_staticbuff(bytes:4) @RAW_staticbuff_fixedwidthinteger_type(bigEndian: false) public struct LE32: Sendable, Comparable, Equatable {} // Encode let value = LE32(RAW_native: 0xDEADBEEF) var rawBytes = [UInt8](repeating: 0, count: 4) rawBytes.withUnsafeMutableBytes { ptr in _ = value.RAW_encode(dest: ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)) } // Little-endian layout: [EF, BE, AD, DE] print(rawBytes.map { String(format: "%02X", $0) }) // Decode (fixed-size: returns nil if count != 4) let decoded = rawBytes.withUnsafeBytes { ptr -> LE32? in LE32(RAW_decode: ptr.baseAddress!, count: ptr.count) } print(decoded?.RAW_native() as Any) // Optional(3735928559) // Wrong size returns nil let tooShort: [UInt8] = [0xEF, 0xBE] let failed = tooShort.withUnsafeBytes { ptr -> LE32? in LE32(RAW_decode: ptr.baseAddress!, count: ptr.count) } print(failed as Any) // nil ``` -------------------------------- ### Encode RAW_accessible to Byte Array Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use `RAW_encode` to convert any `RAW_accessible` type to a byte array. `[UInt8]` conforms by default. Access raw bytes via `RAW_access` closure without copying. ```swift import RAW // Any RAW_accessible can be encoded into a new byte array func toByteArray(_ value: borrowing A) -> [UInt8] { var count: size_t = 0 value.RAW_encode(count: &count) var buffer = [UInt8](repeating: 0, count: count) buffer.withUnsafeMutableBytes { ptr in _ = value.RAW_encode(dest: ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)) } return buffer } // [UInt8] conforms to RAW_accessible out of the box let bytes: [UInt8] = [10, 20, 30] let encoded = toByteArray(bytes) print(encoded) // [10, 20, 30] // Access raw bytes via closure without copying bytes.RAW_access { buffer in print("count:", buffer.count) // 3 print("first:", buffer[0]) // 10 } ``` -------------------------------- ### Concatenate RAW_staticbuff types with @RAW_staticbuff(concat:) Source: https://context7.com/tannerdsilva/rawdog/llms.txt Use the `@RAW_staticbuff(concat:)` macro to combine multiple static buffer types into a single compound type. This generates a tuple-based store type and field-by-field comparison semantics. ```swift import RAW @RAW_staticbuff(bytes:4) public struct PartA: Sendable {} @RAW_staticbuff(bytes:8) public struct PartB: Sendable {} // Concatenated 12-byte type with field-by-field comparison @RAW_staticbuff(concat: PartA.self, PartB.self) public struct Combined: Sendable, Comparable, Equatable { public var a: PartA public var b: PartB } let aBytes: [UInt8] = [1, 2, 3, 4] let bBytes: [UInt8] = Array(repeating: 0xFF, count: 8) let combined = Combined( a: PartA(RAW_staticbuff: aBytes), b: PartB(RAW_staticbuff: bBytes) ) // The comparison first compares `a`, then `b` if `a` is equal let other = Combined( a: PartA(RAW_staticbuff: aBytes), b: PartB(RAW_staticbuff: Array(repeating: 0x00, count: 8)) ) print(other < combined) // true (b fields differ) // Access the raw 12-byte concatenated representation combined.RAW_access { buffer in print("total bytes:", buffer.count) // 12 } ``` -------------------------------- ### Generate and Access MemoryGuarded Private Key Source: https://context7.com/tannerdsilva/rawdog/llms.txt Generates a new curve25519 private key in locked memory. Provides read-only and mutating access to the secure buffer. ```swift import RAW import RAW_dh25519 // Generate a new curve25519 private key in locked memory let privateKey = try MemoryGuarded.new() // Read-only access privateKey.RAW_access { buffer in print("key bytes:", buffer.count) // 32 } // Mutating access (e.g. fill with secure random data) let blank = try MemoryGuarded.blank() try generateSecureRandomBytes(into: blank) // Decode from existing raw bytes let existingKeyBytes: [UInt8] = Array(repeating: 0x42, count: 32) let loaded = existingKeyBytes.withUnsafeBytes { MemoryGuarded(RAW_decode: $0.baseAddress!, count: $0.count) } // Memory is automatically zeroed and unlocked when `loaded` goes out of scope ``` -------------------------------- ### Generate Curve25519 Key Pairs and Shared Secrets Source: https://context7.com/tannerdsilva/rawdog/llms.txt Generates Curve25519 key pairs and computes Diffie-Hellman shared secrets. Private keys are stored in MemoryGuarded for security. ```swift import RAW_dh25519 // Generate a new private key in guarded memory let alicePriv = try MemoryGuarded.new() // Derive the public key let alicePub = RAW_dh25519.PublicKey(privateKey: alicePriv) // Bob does the same let bobPriv = try MemoryGuarded.new() let bobPub = RAW_dh25519.PublicKey(privateKey: bobPriv) // Each side computes the shared key — both should match let aliceShared = try MemoryGuarded.compute( privateKey: alicePriv, publicKey: bobPub ) let bobShared = try MemoryGuarded.compute( privateKey: bobPriv, publicKey: alicePub ) // Both sides have the same 32-byte shared secret let match = aliceShared.RAW_access { aliceBuf in bobShared.RAW_access { bobBuf in zip(aliceBuf, bobBuf).allSatisfy { $0 == $1 } } } print("shared keys match:", match) // true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.