Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Jan 3, 2018
0 parents commit 41c6b4f
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/doc/
/lib/
/bin/
/.shards/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Anton Maminov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# nanoid.cr

Crystal implementation of original NanoID https://github.com/ai/nanoid

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
nanoid:
github: mamantoha/nanoid.cr
```
## Usage
```crystal
require "nanoid"

Nanoid.generate
# => 3gFI8yZxcfXsXGhB0036l

Nanoid.generate(size: 8, alphabet: "1234567890abcdef")
# => 86984b57
```

## Development

Clone this repository and install dependencies:

```
shards install
```

Run tests:

```
crystal spec
```

## Contributing

1. Fork it ( https://github.com/mamantoha/nanoid.cr/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [mamantoha](https://github.com/mamantoha) Anton Maminov - creator, maintainer

## License

Copyright: 2018 Anton Maminov (anton.maminov@gmail.com)

This library is distributed under the MIT license. Please see the LICENSE file.
15 changes: 15 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: nanoid
version: 0.1.0

authors:
- Anton Maminov <anton.maminov@gmail.com>
description: |
Crystal implementation of Nanoid, secure URL-friendly unique ID generator
targets:
nanoid:
main: src/nanoid.cr

crystal: 0.24.1

license: MIT
55 changes: 55 additions & 0 deletions spec/nanoid_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "./spec_helper"

describe Nanoid do
it "has correct size" do
count = 100

count.times do
id = Nanoid.generate

(id.size).should eq(21)
end
end

it "generates URL-friendly IDs" do
count = 100

count.times do
id = Nanoid.generate

(0...id.size).each do |j|
(Nanoid::SAFE_ALPHABET.index(id[j])).should_not be_nil
end
end
end

it "has no collisions for million entries as 21 characters length" do
count = 100_000
generated = {} of String => Bool

count.times do
id = Nanoid.generate
(id.size).should eq(21)

(generated.has_key?(id)).should be_false
generated[id] = true
end
end

it "has no collisions for milion entries with 9 characters and base alphabet" do
count = 100_000
generated = {} of String => Bool

count.times do
id = Nanoid.generate(size: 9, alphabet: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
(id.size).should eq(9)

(generated.has_key?(id)).should be_false
generated[id] = true
end
end

it "generates with custom settings" do
Nanoid.generate(size: 12, alphabet: "1234567890abcdef")
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/nanoid"
47 changes: 47 additions & 0 deletions src/nanoid.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Nanoid
SAFE_ALPHABET = "_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def self.generate(size = 21, alphabet = SAFE_ALPHABET) : String
return simple_generate(size) if alphabet == SAFE_ALPHABET

complex_generate(size: size, alphabet: alphabet)
end

private def self.simple_generate(size : Int32) : String
bytes = random_bytes(size)

(0...size).reduce("") do |acc, i|
acc + SAFE_ALPHABET[bytes[i] & 63]
end
end

private def self.complex_generate(size : Int32, alphabet : String) : String
alphabet_size = alphabet.size
mask = (2 << (Math.log(alphabet_size - 1) / Math.log(2)).to_i) - 1
step = (1.6 * mask * size / alphabet_size).ceil.to_i

result = ""

loop do
break if result.size == size

bytes = random_bytes(size)
(0...step).each do |i|
byte = bytes[i]? ? (bytes[i] & mask) : false

next unless byte.is_a?(Int)
char = byte && alphabet[byte]?
next unless char

result = "#{result}#{char}"
break if result.size == size
end
end

return result
end

private def self.random_bytes(size)
Random::Secure.random_bytes(size).map(&.to_i).to_a
end
end
3 changes: 3 additions & 0 deletions src/nanoid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module NanoId
VERSION = "0.1.0"
end

0 comments on commit 41c6b4f

Please sign in to comment.