Posts

Showing posts from April, 2012

F# - Common Hash Functions Explained

F# - Common Hash Functions Explained When working with data, hashing is a common technique used to generate a unique identifier (or "hash") for a given input. In this post, we’ll look at how to implement some common hash functions in F#—namely MD5, SHA-1, SHA-256, and SHA-512. These are widely used for tasks like checking data integrity or creating digital signatures. We'll focus on converting strings to their respective hash values, and converting the resulting hash into a readable hexadecimal format. Code Breakdown: module HashSum = open System open System.Security.Cryptography open System.Text // Convert string to byte array using UTF-8 encoding let encode (s: string) = UTF8Encoding().GetBytes(s) // Convert a byte to its hexadecimal representation let toHexDigit (n: byte) = if n < 10uy then char (n + 0x30uy) // For values 0-9 else char (n + 0x37uy) // For values 10-15 (A-F) // Convert byte array to ...