tj_caesar_cipher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tj_caesar_cipher.rb +46 -0
  2. metadata +45 -0
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ class TJCaesarCipher < String
4
+ def rot_unicode! key, range
5
+ self.gsub!(%r([#{'\u{%X}' % range.min}-#{'\u{%X}' % range.max}])){|chr|
6
+ (range.min + (chr.ord - range.min + key) % range.count).chr(Encoding::UTF_8)
7
+ }
8
+ end
9
+
10
+ def rot_unicode *args
11
+ rot_unicode! *args
12
+ end
13
+
14
+ def rot_cjk! key = nil
15
+ [
16
+ (0x3040..0x309F), # Hiragana
17
+ (0x30A0..0x30FF), # Katakana
18
+ (0x3400..0x4DB5), # CJKUI Ext A block
19
+ (0x4E00..0x9FCC), # CJK Unified Ideographs block
20
+ (0x20000..0x2A6D6), # CJKUI Ext B block
21
+ (0x2A700..0x2B734), # CJKUI Ext C block.
22
+ (0x2B740..0x2B81D) # CJKUI Ext D block.
23
+ ].each{|range|
24
+ range = (range.min..range.max+1) if range.count.odd?
25
+ self.rot_unicode!(key || range.count/2, range)
26
+ }
27
+ self
28
+ end
29
+
30
+ def rot_cjk *args
31
+ s = TJCaesarCipher.new(self)
32
+ s.rot_cjk! *args
33
+ end
34
+
35
+ def rot_en! key = 13
36
+ [(0x41..0x5A), (0x61..0x7A)].each{|range|
37
+ self.rot_unicode!(key, range)
38
+ }
39
+ self
40
+ end
41
+
42
+ def rot_en *args
43
+ s = TJCaesarCipher.new(self)
44
+ s.rot_en! *args
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tj_caesar_cipher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tony Jian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Caesar Cipher implementation that supports both English and CJK.
15
+ email: tonytonyjan@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/tj_caesar_cipher.rb
21
+ homepage: http://tonytonyjan.net/2013/03/20/tj-caesar-cipher/
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.25
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Caesar Cipher implementation that supports both English and CJK.
45
+ test_files: []