Git Product home page Git Product logo

xxhash-swift's Introduction


Platform Language Swift%205.0 CocoaPods Carthage compatible SwiftPM compatible Build Status

Introduction

xxHash framework in Swift.
A framework includes XXH32/XXH64/XXH3-64/XXH3-128 functions.

Original xxHash algorithm created by Yann Collet.

Requirements

  • Platforms
    • iOS 10.0+
    • macOS 10.12+
    • tvOS 12.0+
    • Linux
  • Swift 5.0

Installation

Carthage

github "daisuke-t-jp/xxHash-Swift"

CocoaPods

use_frameworks!

target 'target' do
pod 'xxHash-Swift'
end

Usage

Import framework

import xxHash_Swift

XXH32

Generate digest(One-shot)

let digest = XXH32.digest("123456789ABCDEF")
// digest -> 0x576e3cf9

// Using seed.
let digest = XXH32.digest("123456789ABCDEF", seed: 0x7fffffff)
// digest -> 0xa7f06f9d

Generate digest(Streaming)

// Create xxHash instance
let xxh = XXH32() // if using seed, e.g. "XXH32(0x7fffffff)"

// Get data from file
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: "alice29", ofType: "txt")!
let data = NSData(contentsOfFile: path)! as Data

let bufSize = 1024
var index = 0

repeat {
    var lastIndex = index + bufSize
    if lastIndex > data.count {
        lastIndex = index + data.count - index
    }

    let data2 = data[index..<lastIndex]
    xxh.update(data2) // xxHash update

    index += data2.count
    if index >= data.count {
        break
    }
} while(true)

let digest = xxh.digest()
// digest -> 0xafc8e0c2

XXH64

Generate digest(One-shot)

let digest = XXH64.digest("123456789ABCDEF")
// digest -> 0xa66df83f00e9202d

// Using seed.
let digest = XXH64.digest("123456789ABCDEF", seed: 0x000000007fffffff)
// digest -> 0xe8d84202a16e482f

Generate digest(Streaming)

// Create xxHash instance
let xxh = XXH64() // if using seed, e.g. "XXH64(0x000000007fffffff)"

// Get data from file
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: "alice29", ofType: "txt")!
let data = NSData(contentsOfFile: path)! as Data

let bufSize = 1024
var index = 0

repeat {
    var lastIndex = index + bufSize
    if lastIndex > data.count {
        lastIndex = index + data.count - index
    }

    let data2 = data[index..<lastIndex]
    xxh.update(data2) // xxHash update

    index += data2.count
    if index >= data.count {
        break
    }
} while(true)

let digest = xxh.digest()
// digest -> 0x843c2c4ccfbfb749

XXH3-64

Generate digest(One-shot)

let digest = XXH3.digest64("123456789ABCDEF")
// digest -> 0xfb28db77f56706e8

// Using seed.
let digest = XXH3.digest64("123456789ABCDEF", seed: 0x000000007fffffff)
// digest -> 0xced1ef1da8aa95ae

XXH3-128

Generate digest(One-shot)

let digest = XXH3.digest128("123456789ABCDEF")
// digest[0] -> 0x208cfe2ef00d2aaa
// digest[1] -> 0x9b72015eec4abbf3

// Using seed.
let digest = XXH3.digest128("123456789ABCDEF", seed: 0x000000007fffffff)
// digest[0] -> 0x50554db504518e64
// digest[1] -> 0xc8fb00b18f99658c

Demo

There are demos.

xxhash-swift's People

Contributors

daisuke-t-jp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xxhash-swift's Issues

XXH3 implementation out of date with xxHash 0.8.0

With xxHash v0.8.0, XXH3 is now stable (see https://github.com/Cyan4973/xxHash/releases/tag/v0.8.0).

Looking at the test vectors in this library, it looks as though it is out of sync with the latest tag. Is there a plan to upgrade?

For example, for xxHash3-64, for an empty input and zero-value seed we would expect a hash of 2D06800538D394C2.
https://github.com/Cyan4973/xxHash/blob/94e5f23e736f2bb67ebdf90727353e65344f9fc0/xxhsum.c#L1246

Instead the current implementation in this library returns 0000000000000000.

XCTAssertEqual(XXH3.digest64Hex("").lowercased(), "0000000000000000")

Classes vs structs

Why using classes instead of Swift structs? Why not marking classes as final (turns on some optimizations)?

Example code not working?

I am not sure if I am missing something, but I can't get the example code to work for the life of me. I guess I am supposed to use length instead of count, but then I also cannot access the subscript of the NSData type. On the off chance that its an issue with the example code I am posting it here, but assistance would be appreciated if it is just me not understanding the code.

screen shot 2019-03-06 at 4 53 24 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.