Skip to main content

正在搜索…

工具
文章
查看全部结果

Developer Lab · Haskell

Generate UUID in Haskell

UUID generation in Haskell using the uuid package from Hackage - pure and type-safe.

Implementation Code

Production Ready
haskell
import Data.UUID.V4 (nextRandom)
import Data.UUID    (toString)

main :: IO ()
main = do
    -- Generate a single UUID v4
    uuid <- nextRandom
    putStrLn (toString uuid)

    -- Generate multiple UUIDs
    uuids <- mapM (\_ -> nextRandom) [1..5 :: Int]
    mapM_ (putStrLn . toString) uuids

Explanation

The uuid package on Hackage provides a strongly-typed UUID type. Data.UUID.V4.nextRandom generates a version 4 UUID in the IO monad using the system's CSPRNG. toString converts it to the standard hyphenated string format.

Output Example

b4e1f2a3-7c8d-4e9f-a0b1-c2d3e4f5a6b7

Best Practices, Performance, and Security

Best practices

✓

Use the UUID type directly in your data types rather than converting to String early. This keeps type safety throughout your application. The uuid-types package provides a lightweight alternative if you only need the type.

Performance

Very good. The uuid package is efficient and the GHC runtime handles IO operations well. Suitable for high-throughput Haskell services.

Security

Cryptographically secure. nextRandom uses the system's CSPRNG (/dev/urandom on Linux, CryptGenRandom on Windows).

Installation

uuid package (Hackage)

bash
# package.yaml or .cabal
dependencies:
  - uuid

Then run cabal install uuid or stack build to fetch the dependency.

Frequently Asked Questions

How do I generate a UUID in Haskell?

Use the uuid package from Hackage. Add uuid to your build-depends, then import Data.UUID.V4 and call nextRandom to get a random v4 UUID in the IO monad; Data.UUID.toString renders it. The package is pure and type-safe.

Is nextRandom cryptographically secure?

No. Data.UUID.V4.nextRandom draws on System.Random, which is not a CSPRNG. For security-sensitive identifiers such as session tokens or API keys, generate UUIDs from a cryptographic source (for example the crypton / Crypto.Random packages) instead.

What is the difference between UUID v4 and v7 in Haskell?

UUID v4 (nextRandom or equivalent) is fully random and not sortable. UUID v7 embeds a millisecond timestamp for chronological sorting (RFC 9562). Use v4 for general-purpose IDs; use v7 for database primary keys at scale.

Do I need to install a package for UUID generation in Haskell?

A package or library install may be required. See the Installation section for the recommended approach in Haskell.

How do I validate a UUID string in Haskell?

Use the platform's UUID parse/validation function, or test against the RFC 4122 regex: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i. Always validate external input at API boundaries.

Should I use UUIDs as database primary keys in Haskell applications?

UUIDs work well as primary keys for distributed systems. Prefer native UUID/BINARY(16) column types over VARCHAR(36). For very large tables, consider UUID v7 for better B-tree insert locality.

Can I generate UUIDs in Haskell without a network connection?

Yes. UUID generation uses local OS entropy sources and does not require network access. Each call is independent and thread-safe on modern platforms.

What output formats are available in Haskell?

The standard hyphenated lowercase string (36 chars) is the default. Most APIs also support 32-char hex (no hyphens) and 16-byte binary formats. Use string format for APIs and binary for database storage.

What RFC standards apply to Haskell UUID generation?

Version 4 UUIDs follow RFC 4122. UUID v7 follows RFC 9562 (May 2024). Ensure your chosen method produces compliant version and variant bits.

When should I avoid UUID v1?

Avoid UUID v1 in security-sensitive contexts - it embeds MAC address and timestamp information. Prefer v4 (nextRandom or equivalent) unless you need legacy Cassandra timeuuid compatibility.

Key definitions

UUID
128-bit universally unique identifier, usually shown as 36 hex characters with hyphens.
CSPRNG
Cryptographically secure pseudo-random number generator - the entropy source behind secure UUID generation.
RFC 4122
IETF standard defining UUID versions 1 through 5. Version 4 is random.
RFC 9562
IETF standard adding UUID versions 6, 7, and 8. Version 7 is time-ordered.