• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hashids, ported for Swift (http://www.hashids.org)

Hashids.swift


A Swift class to generate YouTube-like ids from numbers.

Ported from Hashids.php by ivanakimov

Read documentation at http://hashids.org/php

Example Usage

var hashids = Hashids(salt:"this is my salt");
var hash = hashids.encode(1, 2, 3); // hash:"laHquq"
var values = hashids.decode(hash!); // values:[1,2,3]

Example with custom alphabet and minimum hash length

var hashids = Hashids(salt:"this is my salt", minHashLength:8, alphabet:"abcdefghij1234567890");
var hash = hashids.encode(1, 2, 3); // hash:"514cdi42"
var values = hashids.decode(s!); // values:[1,2,3]

Example with UTF8 alphabet

var hashids = Hashids(salt:"this is my salt", minHashLength:0, alphabet:"β–β–‚β–ƒβ–„β–…β–†β–‡β–ˆ");
var hash = hashids.encode(1, 2, 3); // hash:"β–…β–…β–‚β–„β–ƒβ–†"
var values = hashids.decode(hash!); // values:[1,2,3]

Notes

Internally Hashids is using integer array rather than strings. By default Hashids class is just a type alias for Hashids_<UInt32> and is using unicode scalars for all string manipulations. This makes it possible to use unicode alphabets eq. 'πŸ₯🐷🐱🐢🐭'.

Generic version Hashids_<T> can be used for both ASCII and UTF alphabets. Based on how characters are interpreted in Swift there are possible 3 scenarios

  • Hashids_<UInt8> ASCII, uses String.UTF8View
  • Hashids_<UInt16> UTF16, uses String.UTF16View
  • Hashids_<UInt32> Unicode, uses String.UnicodeScalarView
Swift < 5

For Swift 4.2 version checkout branch swift-4.2 For Swift 4.1 version checkout branch swift-4.1 For Swift 3.0 version checkout branch swift-3.0 For Swift 2.3 (2) version checkout branch swift-2.3

License

MIT License. See the LICENSE file.