sash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -0
- data/ext/extconf.rb +4 -0
- data/ext/sash.c +75 -0
- data/test/test_sash.rb +10 -0
- metadata +67 -0
data/README.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
= Sash
|
2
|
+
|
3
|
+
Sash computes a 64bit hash using sdbm hash function and outputs a compact string representation.
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
require 'sash'
|
8
|
+
Sash.base64 "hi, this is sash - so how was your day ?" #=> "ixeLC8U8SC"
|
9
|
+
|
10
|
+
= License
|
11
|
+
|
12
|
+
{CC BY-SA 3.0}[http://creativecommons.org/licenses/by-sa/3.0/]
|
data/ext/extconf.rb
ADDED
data/ext/sash.c
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <ruby/encoding.h>
|
3
|
+
|
4
|
+
#define TO_S(v) rb_funcall(v, rb_intern("to_s"), 0)
|
5
|
+
#define CSTRING(v) RSTRING_PTR(TO_S(v))
|
6
|
+
|
7
|
+
#undef SIZET2NUM
|
8
|
+
#undef NUM2SIZET
|
9
|
+
|
10
|
+
#ifdef HAVE_LONG_LONG
|
11
|
+
#define SIZET2NUM(x) ULL2NUM(x)
|
12
|
+
#define NUM2SIZET(x) NUM2ULL(x)
|
13
|
+
#else
|
14
|
+
#define SIZET2NUM(x) ULONG2NUM(x)
|
15
|
+
#define NUM2SIZET(x) NUM2ULONG(x)
|
16
|
+
#endif
|
17
|
+
|
18
|
+
static char *digestlookup = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
|
19
|
+
|
20
|
+
/* djb2 string hash function */
|
21
|
+
uint64_t djhash(void *string) {
|
22
|
+
uint64_t result = 5381;
|
23
|
+
unsigned char *p;
|
24
|
+
|
25
|
+
p = (unsigned char *)string;
|
26
|
+
|
27
|
+
while (*p) {
|
28
|
+
result = ((result << 5) ^ result ) ^ (*p);
|
29
|
+
++p;
|
30
|
+
}
|
31
|
+
|
32
|
+
return result;
|
33
|
+
}
|
34
|
+
|
35
|
+
/* sdbm hash function */
|
36
|
+
uint64_t sdbm(void *string) {
|
37
|
+
uint64_t result = 0;
|
38
|
+
unsigned char *p;
|
39
|
+
|
40
|
+
p = (unsigned char *)string;
|
41
|
+
|
42
|
+
while (*p) {
|
43
|
+
result = *p + (result << 6) + (result << 16) - result;
|
44
|
+
++p;
|
45
|
+
}
|
46
|
+
|
47
|
+
return result;
|
48
|
+
}
|
49
|
+
|
50
|
+
#define STRING_HASH sdbm
|
51
|
+
|
52
|
+
VALUE rb_digest(VALUE self, VALUE string) {
|
53
|
+
return SIZET2NUM(STRING_HASH(CSTRING(string)));
|
54
|
+
}
|
55
|
+
|
56
|
+
VALUE rb_base64(VALUE self, VALUE string) {
|
57
|
+
uint64_t idx, n, digest = STRING_HASH(CSTRING(string));
|
58
|
+
char buffer[12];
|
59
|
+
|
60
|
+
idx = 0;
|
61
|
+
while (digest > 0) {
|
62
|
+
n = digest >> 6;
|
63
|
+
buffer[idx++] = digestlookup[digest - (n << 6)];
|
64
|
+
digest = n;
|
65
|
+
}
|
66
|
+
|
67
|
+
return rb_str_new(buffer, idx);
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
void Init_sash() {
|
72
|
+
VALUE mSash = rb_define_module("Sash");
|
73
|
+
rb_define_singleton_method(mSash, "digest", RUBY_METHOD_FUNC(rb_digest), 1);
|
74
|
+
rb_define_singleton_method(mSash, "base64", RUBY_METHOD_FUNC(rb_base64), 1);
|
75
|
+
}
|
data/test/test_sash.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Bharanee Rathna
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-12-01 00:00:00 +11:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: "[\"computes a 64bit hash using sdbm hash function and outputs a compact string representation\", \"http://github.com/deepfryed/sash\"]"
|
22
|
+
email: deepfryed@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions:
|
26
|
+
- ext/extconf.rb
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- ext/sash.c
|
31
|
+
- ext/extconf.rb
|
32
|
+
- README.rdoc
|
33
|
+
- test/test_sash.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/deepfryed/sash
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: compute a compact base64 string hash
|
66
|
+
test_files: []
|
67
|
+
|