rcnb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +3 -0
- data/lib/rcnb.rb +151 -0
- data/lib/rcnb/version.rb +3 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 82d4a0468703176185dc596b3e2461063b47409158d69775b4e4691acad53992
|
4
|
+
data.tar.gz: ea815585427f99a094e58447d789b424187677767666d6a49cc6772aabe12ce6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e28c283e4c9b445e1f26958739632c8c58ef40e751733813c21af19ad7d206c4dfebaab8b8640addc5363d2837dc8bc36efbc3a4ba0783dbb9e23acdef42b678
|
7
|
+
data.tar.gz: 38e9dd00f4bde23241a7e09483e57d48b51c4ce354f6b9f3a2e7089c7c5874496ed7d9b8c31e572c7e9c21d4861115802fb95f9fcdf97b3c1a41a64c12796304
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 liggest
|
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.
|
data/README.md
ADDED
data/lib/rcnb.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
|
2
|
+
# = rcnb
|
3
|
+
# A ruby implementation of RCNB
|
4
|
+
# ---
|
5
|
+
|
6
|
+
# RCNB模块
|
7
|
+
#
|
8
|
+
# == Example
|
9
|
+
# require 'rcnb'
|
10
|
+
#
|
11
|
+
# RCNB.encode('Who NB?')
|
12
|
+
# => ȐȼŃƅȓčƞÞƦȻƝƃŖć
|
13
|
+
#
|
14
|
+
# RCNB.decode('ȐĉņþƦȻƝƃŔć')
|
15
|
+
# => RCNB!
|
16
|
+
module RCNB
|
17
|
+
# :stopdoc:
|
18
|
+
CR=:rRŔŕŖŗŘřƦȐȑȒȓɌɍ
|
19
|
+
CC=:cCĆćĈĉĊċČčƇƈÇȻȼ
|
20
|
+
CN=:nNŃńŅņŇňƝƞÑǸǹȠȵ
|
21
|
+
CB=:bBƀƁƃƄƅßÞþ
|
22
|
+
|
23
|
+
def self.getIndexHash(s)
|
24
|
+
s=s.to_s unless s.is_a? String
|
25
|
+
Hash[s.each_char.with_index.to_a]
|
26
|
+
end
|
27
|
+
|
28
|
+
private_class_method :getIndexHash
|
29
|
+
IR=getIndexHash(CR)
|
30
|
+
IC=getIndexHash(CC)
|
31
|
+
IN=getIndexHash(CN)
|
32
|
+
IB=getIndexHash(CB)
|
33
|
+
|
34
|
+
SR=CR.size
|
35
|
+
SC=CC.size
|
36
|
+
SN=CN.size
|
37
|
+
SB=CB.size
|
38
|
+
|
39
|
+
SRC=SR*SC
|
40
|
+
SNB=SN*SB
|
41
|
+
SCNB=SC*SNB
|
42
|
+
# :startdoc:
|
43
|
+
def self.encodeByte(i)
|
44
|
+
raise ArgumentError,'rc/nb overflow' if i>0xFF
|
45
|
+
if i>0x7F
|
46
|
+
i&=0x7F
|
47
|
+
d,m=i.divmod(SB)
|
48
|
+
"#{CN[d]}#{CB[m]}"
|
49
|
+
else
|
50
|
+
d,m=i.divmod(SC)
|
51
|
+
"#{CR[d]}#{CC[m]}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.encodeShort(i)
|
56
|
+
raise ArgumentError,'rc/nb overflow' if i>0xFFFF
|
57
|
+
reverse=false
|
58
|
+
if i>0x7FFF
|
59
|
+
i&=0x7FFF
|
60
|
+
reverse=true
|
61
|
+
end
|
62
|
+
result=[
|
63
|
+
CR[i/SCNB],
|
64
|
+
CC[i%SCNB/SNB],
|
65
|
+
CN[i%SNB/SB],
|
66
|
+
CB[i%SB]
|
67
|
+
]
|
68
|
+
result=[ result[2],result[3],result[0],result[1] ] if reverse
|
69
|
+
result.join
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.decodeByte(c)
|
73
|
+
nb=false
|
74
|
+
idx=[ IR[c[0]],IC[c[1]] ]
|
75
|
+
if !idx.all?
|
76
|
+
nb=true
|
77
|
+
idx=[ IN[c[0]],IB[c[1]] ]
|
78
|
+
end
|
79
|
+
raise ArgumentError,'not rc/nb' if !idx.all?
|
80
|
+
result=nb ? idx[0]*SB+idx[1] : idx[0]*SC+idx[1]
|
81
|
+
raise ArgumentError,'rc/nb overflow' if result>0x7F
|
82
|
+
nb ? result|0x80 : result
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.decodeShort(c)
|
86
|
+
reverse=!IR[c[0]]
|
87
|
+
if reverse
|
88
|
+
idx=[ IR[c[2]],IC[c[3]],IN[c[0]],IB[c[1]] ]
|
89
|
+
else
|
90
|
+
idx=[ IR[c[0]],IC[c[1]],IN[c[2]],IB[c[3]] ]
|
91
|
+
end
|
92
|
+
raise ArgumentError,'not rc/nb' if !idx.all?
|
93
|
+
result=idx[0]*SCNB+idx[1]*SNB+idx[2]*SB+idx[3]
|
94
|
+
raise ArgumentError,'rc/nb overflow' if result>0x7FFF
|
95
|
+
reverse ? result|0x8000 : result
|
96
|
+
end
|
97
|
+
|
98
|
+
private_class_method :encodeByte,:encodeShort,:decodeByte,:decodeShort
|
99
|
+
|
100
|
+
# @@encoding='utf-8'
|
101
|
+
# def self.defaultEncoding
|
102
|
+
# @@encoding || __ENCODING__.name
|
103
|
+
# end
|
104
|
+
|
105
|
+
# def self.defaultEncoding=(encoding)
|
106
|
+
# @@encoding=encoding
|
107
|
+
# end
|
108
|
+
|
109
|
+
# 将文本编码为RCNB密文
|
110
|
+
# [str] 文本
|
111
|
+
# [encoding] 文本编码
|
112
|
+
# ---
|
113
|
+
# [return] 密文
|
114
|
+
def self.encode(str,encoding=nil)
|
115
|
+
str=str.encode(encoding) if encoding
|
116
|
+
arr=str.unpack('C*')
|
117
|
+
result=''
|
118
|
+
0.upto( (arr.size>>1)-1 ) do |i|
|
119
|
+
i2=i*2
|
120
|
+
result+=encodeShort( arr[i2]<<8 | arr[i2+1] )
|
121
|
+
end
|
122
|
+
result+=encodeByte(arr[arr.size-1]) unless (arr.size & 1).zero?
|
123
|
+
result
|
124
|
+
end
|
125
|
+
|
126
|
+
# 将RCNB密文解码为文本
|
127
|
+
# [str] 密文
|
128
|
+
# [encoding] 文本编码
|
129
|
+
# ---
|
130
|
+
# [return] 文本
|
131
|
+
def self.decode(str,encoding=nil)
|
132
|
+
raise ArgumentError,'invalid length' unless (str.size & 1).zero?
|
133
|
+
arr=[]
|
134
|
+
0.upto( (str.size>>2)-1 ) do |i|
|
135
|
+
i4=i*4
|
136
|
+
short=decodeShort(str[i4,4])
|
137
|
+
arr<< (short>>8)
|
138
|
+
arr<< (short&0xFF)
|
139
|
+
end
|
140
|
+
arr<< decodeByte(str[-2,2]) unless (str.size & 2).zero?
|
141
|
+
result=arr.pack('C*')
|
142
|
+
if encoding
|
143
|
+
result=result.force_encoding(encoding)
|
144
|
+
result=result.encode(__ENCODING__)
|
145
|
+
else
|
146
|
+
result=result.force_encoding(__ENCODING__)
|
147
|
+
end
|
148
|
+
result
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
data/lib/rcnb/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rcnb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- liggest
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- LICENSE
|
20
|
+
- README.md
|
21
|
+
- lib/rcnb.rb
|
22
|
+
- lib/rcnb/version.rb
|
23
|
+
homepage: https://github.com/liggest/rcnb.rb
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message: 流石RC,的确NB
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubygems_version: 3.0.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: a ruby implementation of RCNB
|
46
|
+
test_files: []
|