     }
 
     // Ensure the counter value is big-endian
-    // TODO: use CFSwapInt64HostToBig instead
-    var bigCounter = _OSSwapInt64(counter)
+    var bigCounter = counter.bigEndian
 
     // Generate an HMAC value from the key and counter
     let (hashAlgorithm, hashLength) = hashInfoForAlgorithm(algorithm)

    var truncatedHash = UnsafePointer<UInt32>(truncatedHashPtr).memory

    // Ensure the four bytes taken from the hash match the current endian format
    // TODO: use CFSwapInt32BigToHost instead
    truncatedHash = _OSSwapInt32(truncatedHash)
    // Discard the most significant bit
    truncatedHash &= 0x7fffffff

    truncatedHash = truncatedHash % UInt32(pow(10, Float(digits)))
    var string = String(truncatedHash)
    while countElements(string) < digits {
        string = "0" + string
    }

    let ptr = UnsafePointer<UInt8>(hash.bytes)
    let offset = ptr[hash.length-1] & 0x0f

    // Take 4 bytes from the hash, starting at the given byte offset
    var truncatedHashPtr = ptr
    for var i: UInt8 = 0; i < offset; i++ {
        truncatedHashPtr = truncatedHashPtr.successor()
