digest-vlh 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +0 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/digest-vlh.gemspec +12 -0
- data/lib/digest/vlh.rb +62 -0
- data/test/test_vlh.rb +50 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9613e1223a32ab00d61b34a80f4f8bc4f1fe7a23d9ed1dd9d0a156f3542b3f51
|
4
|
+
data.tar.gz: 8665bb71f5f0a8222cadb9372682ba550a15773012e65e7b702a64593fa311de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7cda881e8fa83acfc47d3e6a06b653675ae543a508252ab91ca45ca79b4aba389b65b503c69bf424fe0835edd57e7327546248312079d98c9495e885956cd84d
|
7
|
+
data.tar.gz: 5d16d74c555569ea5af1f915fec844113ce87f752dc2115682025b9f65ddda87d45a2c8d5a23c6bc2d96de2f8f29342bd68b4f789b324fb9af1032201b46f6ca
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 SNakano
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# variable length ouptput hash for ruby
|
2
|
+
|
3
|
+
*IMPORTANT: it library is very tiny hash implement. don't use for encryption or something.*
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem 'digest-vlh'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ gem install digest-vlh
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```
|
28
|
+
require 'digest/vlh'
|
29
|
+
|
30
|
+
# Compute a digest
|
31
|
+
Digest::VLH.hexdigest('abc', 6) # => "fa0f4f"
|
32
|
+
Digest::VLH.hexdigest('abc', 9) # => "4ae5fa448"
|
33
|
+
|
34
|
+
Digest::VLH.digest('abc', 7) # => "4770719"
|
35
|
+
|
36
|
+
Digest::VLH.new(200).hexdigest 'abc' # => "4b55d39a2..."
|
37
|
+
Digest::VLH.new(200).digest 'abc' # => "862686787..."
|
38
|
+
|
39
|
+
# Compute digest by chunk
|
40
|
+
vlh = Digest::VLH.new(9)
|
41
|
+
vlh.update("ab")
|
42
|
+
vlh << "c"
|
43
|
+
vlh.hexdigest # => "4ae5fa448"
|
44
|
+
|
45
|
+
# Reset and another digest
|
46
|
+
vlh.reset
|
47
|
+
vlh << "message"
|
48
|
+
vlh.digest # => "255023916"
|
49
|
+
```
|
data/digest-vlh.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "digest-vlh"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.license = "MIT"
|
5
|
+
s.summary = "variable length ouptput hash"
|
6
|
+
s.description = "a tiny implement variable length ouptput hash"
|
7
|
+
s.authors = ["SNakano"]
|
8
|
+
s.email = ["s-nakano@s-style.co.jp"]
|
9
|
+
s.homepage = "https://github.com/SNakano/digest-vlh-ruby"
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
12
|
+
end
|
data/lib/digest/vlh.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class VLH
|
5
|
+
BASE = 16
|
6
|
+
|
7
|
+
def self.hexdigest(str, len)
|
8
|
+
self.new(len).hexdigest(str)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.digest(str, len)
|
12
|
+
self.new(len).digest(str)
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(len)
|
16
|
+
@len = len
|
17
|
+
@sha512 = Digest::SHA512.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def update(str)
|
21
|
+
@sha512.update(str)
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def reset
|
26
|
+
@sha512.reset
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def digest(str = "")
|
31
|
+
update(str)
|
32
|
+
digest = expand_digest.to_i(BASE) % div_num
|
33
|
+
sprintf("%0#{@len}s", digest)
|
34
|
+
end
|
35
|
+
|
36
|
+
def hexdigest(str = "")
|
37
|
+
update(str)
|
38
|
+
digest = (expand_digest.to_i(BASE) % div_hex).to_s(BASE)
|
39
|
+
sprintf("%0#{@len}s", digest)
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :<<, :update
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def expand_digest
|
47
|
+
strlen = @sha512.digest_length * 2
|
48
|
+
digest = @sha512.hexdigest
|
49
|
+
repeat = (@len.to_f / strlen).ceil
|
50
|
+
|
51
|
+
digest * repeat
|
52
|
+
end
|
53
|
+
|
54
|
+
def div_hex
|
55
|
+
("f" * @len).to_i(BASE)
|
56
|
+
end
|
57
|
+
|
58
|
+
def div_num
|
59
|
+
10 ** @len
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/test/test_vlh.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path("lib"))
|
2
|
+
require 'digest/vlh'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class VLHTest < Test::Unit::TestCase
|
6
|
+
def test_instance_hexdigest
|
7
|
+
length = 24
|
8
|
+
vlh = Digest::VLH.new(length)
|
9
|
+
vlh.update("Lorem ipsum dolor sit")
|
10
|
+
digest = vlh.hexdigest
|
11
|
+
|
12
|
+
assert_equal(length, digest.length)
|
13
|
+
assert(/^[a-f0-9]+$/ =~ digest)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_class_method_hexdigest
|
17
|
+
length = 24
|
18
|
+
digest = Digest::VLH.hexdigest("Lorem ipsum dolor sit", length)
|
19
|
+
|
20
|
+
assert_equal(length, digest.length)
|
21
|
+
assert(/^[a-f0-9]+$/ =~ digest)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_instance_digest
|
25
|
+
length = 24
|
26
|
+
vlh = Digest::VLH.new(length)
|
27
|
+
vlh.update("Lorem ipsum dolor sit")
|
28
|
+
digest = vlh.digest
|
29
|
+
|
30
|
+
assert_equal(length, digest.length)
|
31
|
+
assert(/^[0-9^a-f]+$/ =~ digest)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_class_method_digest
|
35
|
+
length = 24
|
36
|
+
digest = Digest::VLH.digest("Lorem ipsum dolor sit", length)
|
37
|
+
|
38
|
+
assert_equal(length, digest.length)
|
39
|
+
assert(/^[0-9^a-f]+$/ =~ digest)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_reset
|
43
|
+
vlh = Digest::VLH.new(12)
|
44
|
+
vlh << "Lorem ipsum dolor sit"
|
45
|
+
d1 = vlh.hexdigest
|
46
|
+
vlh.reset
|
47
|
+
d2 = vlh.hexdigest
|
48
|
+
assert_not_equal(d1, d2)
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digest-vlh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SNakano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: a tiny implement variable length ouptput hash
|
14
|
+
email:
|
15
|
+
- s-nakano@s-style.co.jp
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- digest-vlh.gemspec
|
25
|
+
- lib/digest/vlh.rb
|
26
|
+
- test/test_vlh.rb
|
27
|
+
homepage: https://github.com/SNakano/digest-vlh-ruby
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.7.6
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: variable length ouptput hash
|
51
|
+
test_files:
|
52
|
+
- test/test_vlh.rb
|