ruby_xid 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_xid.rb +18 -21
- data/lib/xid/base32.rb +2 -22
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc05ca0b9b3c67ac29d68ea4b957b089ee503a2090b95747cfe9514c101940ed
|
4
|
+
data.tar.gz: 53518ae32bc8ecffe4e0f32b6d02284f447df4905d4cce72d84375a601a4950b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24fb58cbe29a05bb81fdfb8db83a61d5c90dde101aa11a6e357331e9b23135201128b27a7fd2849760d71d00dca99d67002988788299633dae3ec696513e725a
|
7
|
+
data.tar.gz: 691f01a19ad332bf78a64a384be0a583e729a0bc058caefa5deffc599f6f8f6b1c4439cfe526cd026a6738ee3cd01e3efd7dacd2883cf62194e3fa2bdf5c3c84
|
data/lib/ruby_xid.rb
CHANGED
@@ -19,7 +19,7 @@ class Xid
|
|
19
19
|
# Decoded array
|
20
20
|
@value = id
|
21
21
|
else
|
22
|
-
|
22
|
+
generate_xid
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -55,7 +55,7 @@ class Xid
|
|
55
55
|
def string
|
56
56
|
# type: () -> str
|
57
57
|
byte_value = bytes
|
58
|
-
Base32.b32encode(byte_value)
|
58
|
+
Base32.b32encode(byte_value)[0..TRIM_LEN - 1]
|
59
59
|
end
|
60
60
|
|
61
61
|
def bytes
|
@@ -104,10 +104,8 @@ class Xid
|
|
104
104
|
def real_machine_id
|
105
105
|
# type: () -> List[int]
|
106
106
|
hostname = Socket.gethostname.encode('utf-8')
|
107
|
-
|
108
|
-
|
109
|
-
val = md5.digest[0..3]
|
110
|
-
val.scan(/.{1}/m).map(&:ord)
|
107
|
+
val = Digest::MD5.digest(hostname)[0..3]
|
108
|
+
val.chars.map(&:ord)
|
111
109
|
rescue
|
112
110
|
SecureRandom.hex(3).scan(/.{2}/m).map(&:hex)
|
113
111
|
end
|
@@ -115,30 +113,29 @@ class Xid
|
|
115
113
|
def generate_xid
|
116
114
|
# type: () -> List[int]
|
117
115
|
now = Time.now.to_i
|
118
|
-
|
116
|
+
@value = Array.new(RAW_LEN, 0)
|
119
117
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
118
|
+
@value[0] = (now >> 24) & 0xff
|
119
|
+
@value[1] = (now >> 16) & 0xff
|
120
|
+
@value[2] = (now >> 8) & 0xff
|
121
|
+
@value[3] = now & 0xff
|
124
122
|
|
125
|
-
|
126
|
-
|
127
|
-
|
123
|
+
@value[4] = machine_id[0]
|
124
|
+
@value[5] = machine_id[1]
|
125
|
+
@value[6] = machine_id[2]
|
128
126
|
|
129
|
-
|
130
|
-
|
127
|
+
@value[7] = (@pid >> 8) & 0xff
|
128
|
+
@value[8] = @pid & 0xff
|
131
129
|
|
132
130
|
@mutex.synchronize do
|
133
131
|
@rand_int += 1
|
134
132
|
end
|
135
|
-
i = @rand_int
|
136
133
|
|
137
|
-
|
138
|
-
|
139
|
-
|
134
|
+
@value[9] = (@rand_int >> 16) & 0xff
|
135
|
+
@value[10] = (@rand_int >> 8) & 0xff
|
136
|
+
@value[11] = @rand_int & 0xff
|
140
137
|
|
141
|
-
|
138
|
+
@value
|
142
139
|
end
|
143
140
|
end
|
144
141
|
|
data/lib/xid/base32.rb
CHANGED
@@ -12,7 +12,7 @@ class Xid::Base32
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def b32encode(src)
|
15
|
-
src = src.
|
15
|
+
src = src.chars.map(&:ord)
|
16
16
|
encode(src, ENCODE_HEX)
|
17
17
|
end
|
18
18
|
|
@@ -24,7 +24,6 @@ class Xid::Base32
|
|
24
24
|
return '' if src_str.empty?
|
25
25
|
|
26
26
|
dst = []
|
27
|
-
src_len = 0
|
28
27
|
while src_str && !src_str.empty?
|
29
28
|
src_len = src_str.length
|
30
29
|
next_byte = [0] * 8
|
@@ -58,19 +57,6 @@ class Xid::Base32
|
|
58
57
|
src_str = src_str[5..src_str.length]
|
59
58
|
end
|
60
59
|
|
61
|
-
dst[-1] = '=' if src_len < 5
|
62
|
-
if src_len < 4
|
63
|
-
dst[-2] = '='
|
64
|
-
dst[-3] = '='
|
65
|
-
end
|
66
|
-
if src_len < 3
|
67
|
-
dst[-4] = '='
|
68
|
-
end
|
69
|
-
if src_len < 2
|
70
|
-
dst[-5] = '='
|
71
|
-
dst[-6] = '='
|
72
|
-
end
|
73
|
-
|
74
60
|
dst.join('')
|
75
61
|
end
|
76
62
|
|
@@ -91,13 +77,7 @@ class Xid::Base32
|
|
91
77
|
break
|
92
78
|
end
|
93
79
|
char = src[i]
|
94
|
-
|
95
|
-
end_loop = true
|
96
|
-
src_len = i
|
97
|
-
break
|
98
|
-
else
|
99
|
-
dbuf[i] = decode_hex_map[char]
|
100
|
-
end
|
80
|
+
dbuf[i] = decode_hex_map[char]
|
101
81
|
end
|
102
82
|
|
103
83
|
if src_len >= 8
|