### Build and Install Demo Apps Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/CLAUDE.md Build and install the Android demo application, and start the development servers for the web demo applications. These commands are used to test the library's integration within sample applications. ```bash # Demo apps ./gradlew :androidApp:installDebug ./gradlew :webApp:jsBrowserDevelopmentWebpack ./gradlew :webApp:wasmJsBrowserDevelopmentWebpack ``` -------------------------------- ### Run Web App Development Build Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Starts the development build for the web application using Webpack. ```bash ./gradlew :webApp:jsBrowserDevelopmentWebpack ``` -------------------------------- ### JVM Basic Hash Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Calculate the XXH64 hash for a byte array in a main application on the JVM. This demonstrates basic usage with standard Kotlin extensions. ```kotlin import io.github.limuyang2.xxhash.lib.* fun main() { val data = "Hello, JVM!".encodeToByteArray() val hash = data.xxh64() println("Hash: ${hash.toULong().toString(16)}") } ``` -------------------------------- ### Hash Data in Browser with WASM (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Asynchronous example of hashing data in a browser using the WebAssembly variant of XXHash. This requires a WASM-enabled browser and the 'xxhash-lib' import. ```kotlin import io.github.limuyang2.xxhash.lib.* async fun hashData(data: String): String { val hash = data.xxh64() return hash.toULong().toString(16) } ``` -------------------------------- ### Import and Basic Usage of XXHash Kotlin Multiplatform Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Demonstrates how to import and use the XXHash library for hashing strings, byte arrays, and ByteBuffers. Includes examples for different XXHash algorithms and options like seeds and slices. ```kotlin import io.github.limuyang2.xxhash.lib.* // Simple string hash val hash = "Hello, World!".xxh64() println(hash.toULong().toString(16)) // Byte array val data = "data".encodeToByteArray() val hash32 = data.xxh32() val hash64 = data.xxh64() val hash3_64 = data.xxh3As64() val hash3_128 = data.xxh3As128() // With seed val seeded = "data".xxh64(seed = 42L) // Slice val slice = byteArrayOf(1, 2, 3, 4, 5).xxh64(offset = 1, length = 3) // ByteBuffer (JVM/Android) val buffer = ByteBuffer.wrap(myData) val bufferHash = buffer.xxh64() ``` -------------------------------- ### Hash User Data in Browser (Kotlin/JS) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Example of hashing user data in a browser environment using Kotlin compiled to JavaScript. Ensure the 'xxhash-lib' is imported. ```kotlin import io.github.limuyang2.xxhash.lib.* fun hashUserData(userData: String): String { val hash = userData.xxh64() return hash.toULong().toString(16).padStart(16, '0') } ``` -------------------------------- ### Install Android Debug App Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Installs the debug version of the Android application to a connected device or emulator. ```bash ./gradlew :androidApp:installDebug ``` -------------------------------- ### Run WebAssembly Web App Development Server Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/README.md Starts the development server for the web application using Webpack, targeting WebAssembly (WasmJs). This command is for WebAssembly-based web development. ```bash ./gradlew :webApp:wasmJsBrowserDevelopmentWebpack ``` -------------------------------- ### Cross-Platform Hash Consistency Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Verifies that the hash values generated for the same input data and seed are identical across different platforms, ensuring cross-platform validation capabilities. ```kotlin // Android val androidHash = "data".xxh64() // JVM val jvmHash = "data".xxh64() // iOS, JavaScript, WebAssembly val otherHash = "data".xxh64() // androidHash == jvmHash == otherHash ✓ ``` -------------------------------- ### Android Basic Hash Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Calculate the XXH64 hash for a byte array in Kotlin on Android. Ensure you import the necessary extension functions. ```kotlin import io.github.limuyang2.xxhash.lib.* val data = "Hello, Android!".encodeToByteArray() val hash = data.xxh64() Log.d("XXHash", "Hash: ${hash.toULong().toString(16)}") ``` -------------------------------- ### Verify npm Package Integrity (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Example of verifying the integrity of an npm package by comparing its computed hash with an expected hash in a Node.js environment. Assumes 'xxhash-lib' is available. ```kotlin fun verifyPackage(packageContent: String, expectedHash: String): Boolean { val computed = packageContent.xxh64() val computedHex = computed.toULong().toString(16).padStart(16, '0') return computedHex == expectedHash } ``` -------------------------------- ### Android Background Thread Hashing Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Demonstrates hashing a large file in a background thread on Android to avoid blocking the main UI thread. The result is posted back to the UI thread. ```kotlin Thread { val largeFile = File("/sdcard/large_video.mp4") val bytes = largeFile.readBytes() val hash = bytes.xxh64() // Blocks, but in background handler.post { updateUI(hash) } }.start() ``` -------------------------------- ### Android Hash From File Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Compute the XXH64 hash of data read from a file on Android. This requires reading the file's content into a byte array. ```kotlin val file = File(context.cacheDir, "data.bin") val bytes = file.readBytes() val hash = bytes.xxh64() ``` -------------------------------- ### Transferring Hashes Between Platforms (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Example of serializing a hash to a hexadecimal string for transfer between platforms and then deserializing it on the receiving end for verification. Ensures consistent handling of hash values in distributed systems. ```kotlin // Sender (any platform) val hash = data.xxh64() val hashHex = hash.toULong().toString(16).padStart(16, '0') sendToOtherPlatform(hashHex) // Receiver (any platform) val receivedHex = receiveFromOtherPlatform() val receivedHash = receivedHex.toLong(16) verify(computedHash == receivedHash) ``` -------------------------------- ### Async Hashing in iOS Background Thread Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Example of performing xxhash calculation asynchronously on a background thread in iOS and updating the UI on the main thread. ```swift DispatchQueue.global(qos: .background).async { let data = readLargeFile() let hash = data.xxh64() DispatchQueue.main.async { updateUI(hash) } } ``` -------------------------------- ### Integration Test for Cross-Platform Consistency Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md An integration test example to verify that XXHash produces consistent results across different platforms (Android, JVM, iOS, JS, WASM). Compares the computed hash against a known expected value. ```kotlin @Test fun testCrossPlatformConsistency() { // Same on all platforms: Android, JVM, iOS, JS, WASM val data = "test data".encodeToByteArray() val expectedHash = 0x1234567890ABCDEFL // Known value from reference impl val hash = data.xxh64(seed = 0) assertEquals(expectedHash, hash) } ``` -------------------------------- ### Signed Long Semantics Example Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Demonstrates how signed Kotlin Longs are used to represent unsigned 32-bit and 64-bit hash values. It shows the conversion process to obtain the unsigned representation. ```kotlin val hash = XXHash.xxh32(byteArrayOf(), 0) println(hash) // -772998267 (signed) println(hash.toLong().and(0xffffffffL)) // 3521969029 (unsigned as Int) println(hash.toLong().and(0xffffffffL).toLong()) // 3521969029 (unsigned as Long) ``` -------------------------------- ### Extracting and using low and high 64-bit components of a hash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Shows how to access the individual low and high 64-bit components from the LongArray returned by the XXH3-128bit hash function. These components can be used separately, for example, to construct a UUID. ```kotlin val hash = "data".xxh3As128() val lowPart = hash[0] // As Long val highPart = hash[1] // As Long // Example: use as UUID-like identifier val uuid = UUID(hash[1].toULong(), hash[0].toULong()) ``` -------------------------------- ### Web Worker for Hashing (Main Thread - Kotlin/JS) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Code for the main thread to offload hashing tasks to a Web Worker, preventing the UI from blocking. This example assumes 'hasher-worker.js' is the worker script. ```kotlin // Main thread val worker = Worker("hasher-worker.js") worker.postMessage(largeFileData) worker.onmessage = { event -> val hash = event.data as String updateUI(hash) } ``` -------------------------------- ### Importing XXHash Functions Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Demonstrates how to import specific XXHash functions or all extension functions. Includes imports for ByteBuffer on JVM/Android. ```kotlin // Specific imports import io.github.limuyang2.xxhash.lib.XXHash import io.github.limuyang2.xxhash.lib.xxh32 import io.github.limuyang2.xxhash.lib.xxh64 // Or import extension functions import io.github.limuyang2.xxhash.lib.* // For ByteBuffer (JVM/Android) import java.nio.ByteBuffer // ByteBuffer extensions are in the same package ``` -------------------------------- ### XXH32 Implementation Pattern for ByteBuffer Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/api-reference/XXHashByteBuffer.md Illustrates the internal implementation pattern for `ByteBuffer.xxh32`, showing how the buffer's position is saved, remaining bytes are read into a temporary array, and the position is restored before hashing. ```kotlin fun ByteBuffer.xxh32(seed: Int = 0): Long { val arr = ByteArray(remaining()) val pos = position() get(arr) // Read remaining bytes into arr, advances position position(pos) // Restore original position return arr.xxh32(seed) } ``` -------------------------------- ### Compare XXH3-64 Hashes with and without Explicit Zero Seed Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Demonstrates how to achieve optimized hashing by either omitting the seed parameter or explicitly setting it to 0 for XXH3-64. Compares this to a less optimized method that always passes the seed. ```kotlin // Optimized: Uses dedicated zero-seed function val hash1 = "hello".xxh3As64() val hash2 = "hello".xxh3As64(seed = 0) // Also optimized (conditional dispatch) // Not optimized: Passes seed explicitly even when 0 val hash3 = XXHash.xxh3_64bitsWithSeed("hello".encodeToByteArray(), 0) ``` -------------------------------- ### Run iOS Simulator Tests for Library Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Executes the unit tests for the library module on an iOS simulator with ARM64 architecture. ```bash ./gradlew :lib:iosSimulatorArm64Test ``` -------------------------------- ### Handle Form Submission with Hashing (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Demonstrates hashing form data before sending it to the server in a browser context. Requires the 'xxhash-lib' to be available. ```kotlin fun handleFormSubmit(formData: String) { val hash = formData.xxh64() sendToServer(mapOf( "data" to formData, "hash" to hash.toULong().toString(16) )) } ``` -------------------------------- ### Run Tests for XXHash KMP Library Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/CLAUDE.md Execute tests for different targets of the XXHash KMP library. Ensure all platform-specific tests pass before integration. ```bash # Run tests ./gradlew :lib:jvmTest ./gradlew :lib:iosSimulatorArm64Test ./gradlew :lib:jsNodeTest ./gradlew :lib:wasmJsNodeTest ``` -------------------------------- ### iOS Static Library Build Commands Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Provides the bash commands used by Gradle to build a static XXHash library for iOS. It demonstrates the use of xcrun for compiling C code and creating an archive. ```bash # From Gradle (automated in build.gradle.kts) xcrun --sdk iphoneos clang \ -arch arm64 \ -isysroot /path/to/ios-sdk \ -miphoneos-version-min=12.0 \ -O3 \ -c xxhash.c \ -o xxhash.o xcrun --sdk iphoneos ar rcs libxxhash.a xxhash.o ``` -------------------------------- ### Basic XXHash Usage in Kotlin Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/README.md Demonstrates how to compute XXH32, XXH64, XXH3 64-bit, and XXH3 128-bit hashes for strings and byte arrays. Supports custom seeds and byte array slicing. ```kotlin import io.github.limuyang2.xxhash.lib.xxh32 import io.github.limuyang2.xxhash.lib.xxh64 import io.github.limuyang2.xxhash.lib.xxh3As64 import io.github.limuyang2.xxhash.lib.xxh3As128 val text = "Hello, World!" val h32 = text.xxh32() val h64 = text.xxh64() val h3_64 = text.xxh3As64() val h3_128 = text.xxh3As128() // longArrayOf(low64, high64) val bytes = text.encodeToByteArray() val slice = bytes.xxh32(offset = 7, length = 6) val seeded = bytes.xxh64(seed = 42) ``` -------------------------------- ### Embed and Sign iOS Framework Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/README.md Prepares and signs the common application framework for Xcode integration. This command is specific to iOS development. ```bash ./gradlew :commonApp:embedAndSignAppleFrameworkForXcode ``` -------------------------------- ### Compiling xxhash for iOS Static Libraries Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Commands to compile the C source of xxhash into object files and static libraries for iOS arm64 devices and simulators. ```bash xcrun --sdk iphoneos clang -O3 -c xxhash.c -o xxhash.o xcrun --sdk iphoneos ar rcs libxxhash.a xxhash.o ``` -------------------------------- ### Unit Test for XXHash Consistency Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Demonstrates unit tests for verifying the consistency and behavior of XXHash functions. Includes tests for basic consistency, different inputs, seeding, and slicing. ```kotlin import kotlin.test.Test import kotlin.test.assertEquals class XXHashTest { @Test fun testConsistency() { val data = "Hello, World!".encodeToByteArray() val hash1 = data.xxh64() val hash2 = data.xxh64() assertEquals(hash1, hash2) } @Test fun testDifferentInputs() { val hash1 = "Hello".xxh64() val hash2 = "World".xxh64() assertNotEquals(hash1, hash2) } @Test fun testSeeding() { val data = "data".encodeToByteArray() val hash0 = data.xxh64(seed = 0) val hash42 = data.xxh64(seed = 42) assertNotEquals(hash0, hash42) } @Test fun testSlicing() { val data = byteArrayOf(1, 2, 3, 4, 5) val sliceHash = data.xxh64(offset = 1, length = 2) assertNotEquals(0, sliceHash) } } ``` -------------------------------- ### Run Cross-Platform Tests Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Provides the Gradle commands to execute tests across various platforms including JVM, Android, iOS, JavaScript, and WebAssembly, ensuring consistent behavior. ```bash ./gradlew :lib:jvmTest ./gradlew :lib:androidDeviceTest ./gradlew :lib:iosSimulatorArm64Test ./gradlew :lib:jsNodeTest ./gradlew :lib:wasmJsNodeTest ``` -------------------------------- ### JVM XXH32 Hashing Data Flow with Seed Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Illustrates the data flow for hashing a String using XXH32 with a seed on the JVM. It shows the progression from String to ByteArray, then through various XXHash functions, finally returning a signed Long. ```text String.xxh32(seed=12345) → encodeToByteArray() → ByteArray.xxh32(seed=12345) → XXHash.xxh32(this, 12345) → XXHashFactory.fastestJavaInstance().hash32().hash(byteArray, 0, size, 12345) → Returns: signed Long (0xffffffff masked) ``` -------------------------------- ### Direct vs. Non-Direct ByteBuffers with XXHash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Illustrates that both heap (non-direct) and direct ByteBuffers work with XXHash. The hashing process involves copying data to a temporary array, making performance similar for both buffer types. ```kotlin // ✅ Both work equally well val heapBuffer = ByteBuffer.wrap(myByteArray) val directBuffer = ByteBuffer.allocateDirect(1024) val hash1 = heapBuffer.xxh64() // Copies to temp array anyway val hash2 = directBuffer.xxh64() // Copies to temp array anyway // Performance similar; no advantage to direct buffers ``` -------------------------------- ### Displaying Hash Values as Hexadecimal Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Demonstrates how to convert XXH32, XXH64, and XXH3-128 hash results into their hexadecimal string representations to match canonical xxHash output. This is useful for debugging and comparison with other implementations. ```kotlin val hash32 = "hello".xxh32() println(hash32.toLong().and(0xffffffffL).toString(16)) // 32-bit as hex val hash64 = "hello".xxh64() println(hash64.toULong().toString(16).padStart(16, '0')) // 64-bit as hex val hash128 = "hello".xxh3As128() val hexLow = hash128[0].toULong().toString(16).padStart(16, '0') val hexHigh = hash128[1].toULong().toString(16).padStart(16, '0') println(hexHigh + hexLow) // 128-bit as hex ``` -------------------------------- ### String Hashing with UTF-8 Encoding Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Demonstrates equivalent ways to hash a string using UTF-8 encoding. The first method uses a direct string extension function, while the second explicitly converts the string to a byte array before hashing. ```kotlin // These are equivalent: val hash1 = "data".xxh64() val hash2 = "data".encodeToByteArray().xxh64() ``` -------------------------------- ### Compile Kotlin Metadata for Library Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Compiles the Kotlin metadata for the library module, essential for multiplatform compilation. ```bash ./gradlew :lib:compileKotlinMetadata ``` -------------------------------- ### Hash ByteBuffer with XXH32 (JVM) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Demonstrates hashing a ByteBuffer from a specific position to its limit using the XXH32 algorithm. This is a JVM/Android specific extension. ```kotlin val buffer = ByteBuffer.wrap(myData) buffer.position(100) val hash = buffer.xxh32() // Hashes from position 100 to limit ``` -------------------------------- ### Run Android Connected Device Tests Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Executes tests for the library module on an Android device or emulator connected to the host. ```bash ./gradlew :lib:connectedAndroidTest ``` -------------------------------- ### Run JVM Tests for Library Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Executes the unit tests for the library module targeting the Java Virtual Machine. ```bash ./gradlew :lib:jvmTest ``` -------------------------------- ### Run Node.js Tests for Library (JS) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Executes the unit tests for the library module targeting Node.js using the JavaScript runtime. ```bash ./gradlew :lib:jsNodeTest ``` -------------------------------- ### Publish to Local Maven Repository Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/CLAUDE.md Publish the Kotlin Multiplatform library to a local Maven repository. This is useful for testing the publishing process or for local development workflows. ```bash # Publish to local RepoDir ./gradlew :lib:publishKotlinMultiplatformPublicationToMavenRepository ``` -------------------------------- ### Leverage Optimized Zero-Seed Paths Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md When using a seed of 0, the library often employs optimized internal paths for XXH3. Explicitly calling the zero-seed variant or relying on the default zero seed ensures these optimizations are utilized. ```kotlin // ✅ Good: uses optimized zero-seed path val hash1 = data.xxh3As64() // Optimized // ✅ Also optimized: smart dispatch with default seed val hash2 = data.xxh3As64(seed = 0) // Same optimization // ❌ Less optimal: explicit seeded call val hash3 = XXHash.xxh3_64bitsWithSeed(data, 0) // Extra parameter check ``` -------------------------------- ### Preserve ByteBuffer Position with XXHash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Demonstrates the correct way to use XXHash with ByteBuffers, ensuring the buffer's position is preserved after hashing. Incorrect usage may lead to unexpected position changes if not handled explicitly. ```kotlin // ✅ Good: position is preserved val buffer = ByteBuffer.wrap(data) buffer.position(100) val hash = buffer.xxh64() // Hashes from position 100 println(buffer.position()) // Still 100 // ❌ Wrong: assuming position advances val result = buffer.xxh64() // Position not changed buffer.position(200) // Must explicitly advance ``` -------------------------------- ### Generate and format 128-bit hash from a String Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Demonstrates how to generate a 128-bit hash from a String using the xxh3As128 extension function and then format the resulting LongArray into a canonical hexadecimal string representation. ```kotlin val hash = "data".xxh3As128() val low64 = hash[0].toULong().toString(16).padStart(16, '0') val high64 = hash[1].toULong().toString(16).padStart(16, '0') println("Hash: $high64$low64") ``` -------------------------------- ### Hash a String with XXH32 Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Demonstrates how to compute the XXH32 hash of a string. The output is formatted as an unsigned hexadecimal string. ```kotlin val hash = "Hello, World!".xxh32() println(hash.toLong().and(0xffffffffL).toString(16)) // unsigned hex ``` -------------------------------- ### Handle 32-bit Hash Output as Unsigned Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md For 32-bit hashes, convert the signed integer output to an unsigned representation before formatting it as a hexadecimal string. This ensures the full 32-bit range is correctly displayed, padded to 8 characters. ```kotlin // ✅ Also correct: for 32-bit val hash32 = "data".xxh32() val hex32 = hash32.toLong().and(0xffffffffL).toString(16).padStart(8, '0') ``` -------------------------------- ### Hash Salting in Bloom Filter with XXH32 Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Illustrates using XXH32 with different seeds to generate multiple hash functions, commonly used in data structures like Bloom filters or hash chains. ```kotlin val userId = "user123" for (i in 0 until numHashFunctions) { val hash = userId.xxh32(seed = i) bloomFilter.set(hash % filterSize) } ``` -------------------------------- ### Use XXH3 for Maximum Speed Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md For performance-critical applications, utilize the XXH3 algorithm, which generally offers superior speed compared to XXH32 and XXH64 on modern hardware. This is recommended when raw hashing speed is the primary concern. ```kotlin // ✅ Fast: XXH3 is faster than XXH32/64 val hash = data.xxh3As64() // Faster than xxh64() ``` -------------------------------- ### Run Node.js Tests for Library (Wasm JS) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Executes the unit tests for the library module targeting Node.js using the WebAssembly JavaScript runtime. ```bash ./gradlew :lib:wasmJsNodeTest ``` -------------------------------- ### Handle 64-bit Hash Output as Unsigned Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Always convert 64-bit hash outputs to unsigned for correct display, especially when representing them as hexadecimal strings. This ensures consistent and accurate representation across different platforms and avoids issues with signed integer interpretation. ```kotlin // ❌ Wrong: displays as signed integer val hash = "data".xxh64() println(hash) // Example: -123456789 // ✅ Correct: displays as unsigned hex val hexValue = hash.toULong().toString(16).padStart(16, '0') println(hexValue) // Example: "ff864b1c" ``` -------------------------------- ### Content-Addressed Storage using XXHash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Implements a content store where data is keyed by its XXHash. This pattern is useful for content-addressed storage systems, ensuring data integrity and efficient retrieval. ```kotlin // Hash-based key for content storage class ContentStore { private val store = mutableMapOf fun store(content: ByteArray): String { val hash = content.xxh64() val key = hash.toULong().toString(16).padStart(16, '0') store[key] = content return key } fun retrieve(key: String): ByteArray? = store[key] fun verify(content: ByteArray, expectedKey: String): Boolean { val hash = content.xxh64() val computedKey = hash.toULong().toString(16).padStart(16, '0') return computedKey == expectedKey } } // Usage val storage = ContentStore() val key = storage.store("important data".encodeToByteArray()) println(storage.verify("important data".encodeToByteArray(), key)) // true ``` -------------------------------- ### Web XXH32 Hashing Data Flow with Slice Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Describes the data flow for hashing a slice of a ByteArray using XXH32 in a Web environment. It includes slice validation and the pure Kotlin algorithm execution, returning a signed Long. ```text ByteArray.xxh32(offset=5, length=15, seed=999) → XXHash.xxh32Bytes(this, 5, 15, 999) → validateSlice(this, 5, 15) ✓ → XXHashWebPure.xxh32(this, 5, 15, 999) → Pure Kotlin algorithm → Returns: signed Long (masked to 0xffffffffL) ``` -------------------------------- ### Batch Hash Operations in Loops Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md For hashing multiple items, perform the operations within a tight loop or use parallel streams for large collections. This approach is cache-friendly and can significantly improve throughput compared to individual, scattered calls. ```kotlin // ✅ Good: hash in tight loop fun hashMultiple(items: List): List { return items.map { it.xxh64() } // Compact, cache-friendly } // ✅ Also good: parallel for large lists fun hashMultipleParallel(items: List): List { return items.parallelStream() .map { it.xxh64() } .collect(Collectors.toList()) } ``` -------------------------------- ### Hashing with Different Encodings Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Illustrates how different character encodings (UTF-8 vs. UTF-16) result in different hash values for the same string data. This highlights the importance of consistent encoding when hashing. ```kotlin val utf8Hash = "data".encodeToByteArray(Charsets.UTF_8).xxh64() val utf16Hash = "data".encodeToByteArray(Charsets.UTF_16).xxh64() // utf8Hash != utf16Hash ``` -------------------------------- ### Hash Parameters with XXH3-64 using Seed or Zero Seed Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Hashes a variable number of string parameters by joining them with a delimiter. It uses a specific seed if enabled, otherwise falls back to the optimized zero-seed variant of XXH3-64. ```kotlin fun hashParameters(vararg params: String): Long { val combined = params.joinToString("|") return if (shouldUseSeed) { combined.xxh3As64(seed = PARAM_SEED) } else { combined.xxh3As64() // Uses optimized zero-seed } } const val PARAM_SEED = 777L ``` -------------------------------- ### Hash Subranges of Byte Arrays Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Shows how to compute hashes for specific subranges (slices) of a byte array without the need for creating new byte arrays, improving efficiency. ```kotlin val data = byteArrayOf(1, 2, 3, 4, 5) val sliceHash = data.xxh64(offset = 1, length = 3) // Hash [2, 3, 4] ``` -------------------------------- ### Generate Hashes with Different Seeds Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Illustrates how different seed values produce distinct hash outputs for the same input data. Using a seed of 0 provides the canonical xxHash output. ```kotlin val hash0 = "data".xxh64(seed = 0) val hash42 = "data".xxh64(seed = 42) // hash0 != hash42 ``` -------------------------------- ### Unit Test for Hash Consistency (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md A basic JUnit test case to verify the XXHash computation for a given input against an expected hash value. This test should be run on all target platforms to ensure consistency. ```kotlin @Test fun testConsistency() { val data = "test".encodeToByteArray() val expectedHash = 0x123456789ABCDEFL val hash = XXHash.xxh64(data, 0) assertEquals(expectedHash, hash) } ``` -------------------------------- ### Return Type Conversion for XXHash Hashes Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md Shows how to convert the Long and LongArray return types of XXHash functions into unsigned representations for display or further processing. Covers 32-bit, 64-bit, and 128-bit hashes. ```kotlin // 32-bit: mask to unsigned val hash32 = "data".xxh32() val hex = hash32.toLong().and(0xffffffffL).toString(16).padStart(8, '0') // 64-bit: convert to unsigned val hash64 = "data".xxh64() val hex = hash64.toULong().toString(16).padStart(16, '0') // 128-bit: two components val hash128 = "data".xxh3As128() val low = hash128[0].toULong().toString(16).padStart(16, '0') val high = hash128[1].toULong().toString(16).padStart(16, '0') ``` -------------------------------- ### iOS App Hashing with Kotlin Multiplatform Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Demonstrates calling the xxhash function from Swift in an iOS app using a Kotlin Multiplatform framework. Requires importing the shared framework. ```swift // iOS app uses Kotlin Multiplatform framework import shared // Generated framework from :commonApp or :lib let hash = XXHash.shared.xxh64(data: myData, seed: 0) ``` -------------------------------- ### ByteArray Extension Functions for Hashing Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Provides convenient extension functions for hashing entire ByteArrays or specific slices using various XXHash algorithms and optional seeds. ```kotlin fun ByteArray.xxh32(seed: Int = 0): Long fun ByteArray.xxh64(seed: Long = 0): Long fun ByteArray.xxh3As64(seed: Long = 0): Long fun ByteArray.xxh3As128(seed: Long = 0): LongArray fun ByteArray.xxh32(offset: Int, length: Int, seed: Int = 0): Long fun ByteArray.xxh64(offset: Int, length: Int, seed: Long = 0): Long ``` -------------------------------- ### Hashing a String Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Calculates the 64-bit XXHash for a given string and prints its hexadecimal representation. ```kotlin val hash = "Hello, World!".xxh64() println(hash.toULong().toString(16)) ``` -------------------------------- ### Consistent Hash Value Across Platforms (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Illustrates that the XXHash algorithm produces identical hash values for the same input across different platforms like Android/JVM/iOS and JavaScript/WebAssembly. This is crucial for cross-platform data verification. ```kotlin // Platform 1 (Android/JVM/iOS) val hash = "data".xxh64() // Example: 0x1234567890ABCDEF // Platform 2 (JavaScript/WebAssembly) val hash = "data".xxh64() // Same result: 0x1234567890ABCDEF ``` -------------------------------- ### Consistent Hashing for Cache Nodes Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Implements a simplified consistent hashing ring to distribute keys across a fixed set of cache nodes. Ensures that a given key always maps to the same node. ```kotlin class CacheRouter(val nodeNames: List) { fun getNode(key: String): String { val hash = key.xxh64() val nodeIndex = (hash.toULong() % nodeNames.toULong()).toInt() return nodeNames[nodeIndex] } } // Usage val router = CacheRouter(listOf("cache-01", "cache-02", "cache-03")) val node = router.getNode("user:12345") // Always same node for same key ``` -------------------------------- ### Hash a String with XXH32 and Seed Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Shows how to compute the XXH32 hash of a byte array using a specific seed. Using different seeds will result in different hash values. ```kotlin val data = "my data".encodeToByteArray() val hash0 = data.xxh32(seed = 0) val hash1 = data.xxh32(seed = 1) // hash0 != hash1, both are valid xxh32 hashes ``` -------------------------------- ### Hash File Content with XXH64 Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Calculates the XXH64 hash of a file's content by reading its bytes. The hash is then printed in hexadecimal format. ```kotlin val fileBytes = File("myfile.bin").readBytes() val hash = fileBytes.xxh64() println("File hash: ${hash.toULong().toString(16)}") ``` -------------------------------- ### Common API Contract (expect) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Defines the expected interface for XXHash operations that all platform implementations must adhere to. This contract specifies methods for XXH32, XXH64, XXH3 (64-bit and 128-bit) with and without seeds. ```kotlin expect object XXHash { fun xxh32(input: ByteArray, seed: Int): Long fun xxh32Bytes(input: ByteArray, offset: Int, length: Int, seed: Int): Long fun xxh64(input: ByteArray, seed: Long): Long fun xxh64Bytes(input: ByteArray, offset: Int, length: Int, seed: Long): Long fun xxh3_64bits(input: ByteArray): Long fun xxh3_64bitsWithSeed(input: ByteArray, seed: Long): Long fun xxh3_128bits(input: ByteArray): LongArray fun xxh3_128bitsWithSeed(input: ByteArray, seed: Long): LongArray } ``` -------------------------------- ### XXHash Object Methods Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/README.md This section details the core methods available in the XXHash object for hashing various inputs with different algorithms, seeds, and byte range specifications. ```APIDOC ## XXHash Object Methods **Package**: `io.github.limuyang2.xxhash.lib` | Method | Algorithm | Seed | Return | |---|---|---|---| | `xxh32(input, seed)` | XXH32 | Required | `Long` | | `xxh32Bytes(input, offset, length, seed)` | XXH32 | Required | `Long` | | `xxh64(input, seed)` | XXH64 | Required | `Long` | | `xxh64Bytes(input, offset, length, seed)` | XXH64 | Required | `Long` | | `xxh3_64bits(input)` | XXH3 | 0 (fixed) | `Long` | | `xxh3_64bitsWithSeed(input, seed)` | XXH3 | Required | `Long` | | `xxh3_128bits(input)` | XXH3 | 0 (fixed) | `LongArray[2]` | | `xxh3_128bitsWithSeed(input, seed)` | XXH3 | Required | `LongArray[2]` | ``` -------------------------------- ### Benchmark Kotlin Hashing Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Measures the average hashing time per call after a warmup period. Ensures JIT compilation benefits are included. ```kotlin fun benchmark() { val data = "test data".encodeToByteArray() val iterations = 1_000_000 // Warmup repeat(10_000) { data.xxh64() } // Measurement val start = System.nanoTime() repeat(iterations) { data.xxh64() } val elapsed = System.nanoTime() - start println("Avg per call: ${elapsed / iterations} ns") } ``` -------------------------------- ### Extension function for 128-bit XXH3 hash (String) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Provides a convenient extension function to compute the 128-bit XXH3 hash for a String. It supports an optional seed, defaulting to 0 for a zero-seed hash. ```kotlin fun String.xxh3As128(seed: Long = 0): LongArray ``` -------------------------------- ### Fallback to JS Hash Function (Kotlin) Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Demonstrates a fallback mechanism where the WebAssembly hash function is attempted first, and if it fails (e.g., in older browsers), it falls back to the pure JavaScript hash function. Requires both `wasmHash` and `jsHash` functions to be defined. ```kotlin val hash = try { wasmHash(data) } catch (e: Exception) { jsHash(data) // Fallback to JS variant } ``` -------------------------------- ### JVM Hash File or InputStream Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Platform-Guide.md Utility functions to hash the contents of a file or an InputStream on the JVM. These methods read all bytes into memory before hashing. ```kotlin fun hashFile(file: File): Long { return file.readBytes().xxh64() } fun hashInputStream(input: InputStream): Long { val bytes = input.readAllBytes() return bytes.xxh64() } ``` -------------------------------- ### String Extension Functions for Hashing Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Offers convenience extension functions for hashing String values. Strings are automatically UTF-8 encoded before hashing. ```kotlin fun String.xxh32(seed: Int = 0): Long fun String.xxh64(seed: Long = 0): Long fun String.xxh3As64(seed: Long = 0): Long fun String.xxh3As128(seed: Long = 0): LongArray ``` -------------------------------- ### Add XXHash Dependency Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/README.md Include the XXHash KMP library in your project's dependencies. Use the root 'xxhash' artifact for general KMP projects. ```kotlin dependencies { implementation("io.github.limuyang2:xxhash:") } ``` -------------------------------- ### Batch and Parallel Hashing Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Provides methods for efficiently hashing lists of strings, both sequentially and in parallel using Java Streams. Useful for processing large collections of data. ```kotlin // Efficient batch hashing class BatchHasher { fun hashBatch(items: List): List { return items.map { it.xxh64() } } fun hashBatchParallel(items: List): List { return items.parallelStream() .map { it.xxh64() } .collect(Collectors.toList()) } } ``` -------------------------------- ### Safe Byte Pointer Usage in Kotlin/Native Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Architecture-and-Internals.md Demonstrates how to safely pass byte pointers from Kotlin ByteArray to C functions using `useSlicePointer`. Ensures the ByteArray is pinned during C function execution. ```kotlin private inline fun ByteArray.useSlicePointer( offset: Int, length: Int, block: (CPointer?, ULong) -> R, ): R { require(offset >= 0) { "offset must be >= 0" } require(length >= 0) { "length must be >= 0" } require(offset <= size) { "offset must be <= input size" } require(offset + length <= size) { "offset + length must be <= input size" } if (length == 0) { return block(null, 0u) // Pass null pointer for empty slices } return usePinned { pinned -> block(pinned.addressOf(offset), length.convert()) } } ``` -------------------------------- ### Public API Methods Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/README.md This section details the public API methods available for hashing data using different XXHash algorithms and options. ```APIDOC ## Public API This section details the public API methods available for hashing data using different XXHash algorithms and options. ### `xxh32(input, seed)` - **Description**: Computes a 32-bit XXHash. - **Returns**: `Long` (32-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). - `seed`: The seed value for the hash function. ### `xxh32Bytes(input, offset, length, seed)` - **Description**: Computes a 32-bit XXHash for a slice of byte array. - **Returns**: `Long` (32-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The byte array to hash. - `offset`: The starting offset in the byte array. - `length`: The number of bytes to hash. - `seed`: The seed value for the hash function. ### `xxh64(input, seed)` - **Description**: Computes a 64-bit XXHash. - **Returns**: `Long` (64-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). - `seed`: The seed value for the hash function. ### `xxh64Bytes(input, offset, length, seed)` - **Description**: Computes a 64-bit XXHash for a slice of byte array. - **Returns**: `Long` (64-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The byte array to hash. - `offset`: The starting offset in the byte array. - `length`: The number of bytes to hash. - `seed`: The seed value for the hash function. ### `xxh3_64bits(input)` - **Description**: Computes a 64-bit XXH3 hash. - **Returns**: `Long` (64-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). ### `xxh3_64bitsWithSeed(input, seed)` - **Description**: Computes a 64-bit XXH3 hash with a seed. - **Returns**: `Long` (64-bit hash represented as a Kotlin `Long`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). - `seed`: The seed value for the hash function. ### `xxh3_128bits(input)` - **Description**: Computes a 128-bit XXH3 hash. - **Returns**: `LongArray` (containing `[low64, high64]`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). ### `xxh3_128bitsWithSeed(input, seed)` - **Description**: Computes a 128-bit XXH3 hash with a seed. - **Returns**: `LongArray` (containing `[low64, high64]`) - **Parameters**: - `input`: The data to hash (e.g., String or ByteArray). - `seed`: The seed value for the hash function. **Note**: All returned hash values are signed Kotlin `Long`. Treat them as unsigned when formatting or comparing against canonical xxHash hex output. ``` -------------------------------- ### Format XXH32 Hash as Unsigned Hex Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Algorithm-Reference.md Provides the correct method to format a signed Long XXH32 hash into an 8-character unsigned hexadecimal string. ```kotlin val hash = "data".xxh32() val hexValue = hash.toLong().and(0xffffffffL).toString(16).padStart(8, '0') // Example: "60415d5f" ``` -------------------------------- ### Robust Hashing with Algorithm Fallback Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Implements a resilient hashing function that attempts XXH64 and falls back to XXH32 if an error occurs. Provides a final fallback to 0L if both algorithms fail. ```kotlin // Try multiple algorithms, fallback gracefully fun robustHash(data: ByteArray): Long { return try { data.xxh64() // Try XXH64 } catch (e: Exception) { try { data.xxh32() // Fallback to XXH32 } catch (e2: Exception) { 0L // Last resort } } } ``` -------------------------------- ### Compile Kotlin JS for Library Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/AGENTS.md Compiles the Kotlin code for the JavaScript target within the library module. ```bash ./gradlew :lib:compileKotlinJs ``` -------------------------------- ### Multi-Phase Hashing of ByteBuffer Regions Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/api-reference/XXHashByteBuffer.md Perform multi-phase hashing on different regions of the same ByteBuffer without creating intermediate arrays. Adjust position and limit for each region. ```kotlin val buffer = ByteBuffer.wrap(data) buffer.position(0) buffer.limit(100) val hash1 = buffer.xxh64() // Hash first 100 bytes buffer.position(100) buffer.limit(data.size) val hash2 = buffer.xxh64() // Hash remaining bytes ``` -------------------------------- ### Computing 128-bit Hash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/API-Overview.md Calculates the 128-bit XXHash for a string and extracts the low and high 64-bit parts. ```kotlin val hash = "data".xxh3As128() val low = hash[0].toULong().toString(16) val high = hash[1].toULong().toString(16) ``` -------------------------------- ### Data Fingerprinting with XXHash Source: https://github.com/limuyang2/fast-xxhash-kmp/blob/main/_autodocs/Best-Practices-and-Examples.md Computes both 64-bit and 32-bit XXHash fingerprints for byte arrays, useful for data comparison and integrity checks. Provides quick 32-bit and full 128-bit XXHash3 variants as well. ```kotlin class DataFingerprint { fun computeFingerprint(data: ByteArray): String { val hash64 = data.xxh64() val hash32 = data.xxh32() return "${hash64.toULong().toString(16)}:" + "${hash32.and(0xffffffffL).toString(16)}" } fun computeQuick(data: ByteArray): Long = data.xxh32() fun computeFull(data: ByteArray): LongArray = data.xxh3As128() } // Usage val fingerprint = DataFingerprint() val fp = fingerprint.computeFingerprint("important file content".encodeToByteArray()) println(fp) // "abc123:def456" ```