crypto-toolbox 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29fa5f2f86ce56a6756c9fb2c6864a576b65a0e0
4
- data.tar.gz: dc97f15a602319cce3ee4b9338d4c0f61b07c4f4
3
+ metadata.gz: 86a2fd01ae617a93c0e256d09fb62a2661b3ef44
4
+ data.tar.gz: ca135fcb538a076ccf803a49dc4f96620cadaba2
5
5
  SHA512:
6
- metadata.gz: d3e04a78bf74013d08f10a5d700335213207f0112eb888b8f608ec58d83221c39c1bc8e57558160687b742710a57c44094fa545667d04bfa12ac942d482d9aac
7
- data.tar.gz: 1eb3488edb26cbdf687b8771e4fb55699edca41a49b4ce891e5829b18125e349dcfd2d8563165bf9ca9166fb1a11f36f75ebe0343c4c450d6cce2ff7162b41e9
6
+ metadata.gz: 46f18410638436519532a66670195bc60cb581c8f8dbb86386649e7bfe04bd4353702a8843064308b1fd89c9b2d732cf92f7059a9ea55c5a816a1b07d2ba46fb
7
+ data.tar.gz: 8b7f6cb7ec2c3944e7733e31372342a2f7d7c2da56fe2f7d9ef5cdaed84503d942df2caa72fbbb59421a89f5ed1d2392159ce30515d6ef58d3f7cc107e1a30a8
@@ -1 +1,4 @@
1
1
  require 'crypto-toolbox/crypt_buffer.rb'
2
+ require 'crypto-toolbox/key_filter.rb'
3
+ require 'crypto-toolbox/spell_checker.rb'
4
+ require 'crypto-toolbox/ciphers/caesar.rb'
@@ -0,0 +1,40 @@
1
+ require 'crypto-toolbox/crypt_buffer'
2
+
3
+ module Ciphers
4
+ class InvalidCaesarShift < RuntimeError; end
5
+
6
+ class Caesar
7
+ def self.encode(msg,shift)
8
+ ::Ciphers::Caesar.new.encode(msg,shift)
9
+ end
10
+
11
+ def self.decode(msg,shift)
12
+ ::Ciphers::Caesar.new.decode(msg,shift)
13
+ end
14
+
15
+ def encode(message,shift)
16
+ assert_valid_shift!(shift)
17
+ real_shift = convert_shift(shift)
18
+ CryptBuffer.new(message).add(real_shift, mod: 91, offset: 65).str
19
+ end
20
+
21
+ def decode(message,shift)
22
+ assert_valid_shift!(shift)
23
+ real_shift = convert_shift(shift)
24
+ # first reduce by 65 to map A to 0 ; then mod-sub with "A"(91)-65; and re-add the 65 to convert back to real ascii A value
25
+ result = CryptBuffer(message).sub(65).mod_sub(real_shift,mod: 91-65).add(65)
26
+ result.str
27
+ end
28
+ private
29
+
30
+ def assert_valid_shift!(shift)
31
+ raise InvalidCaesarShift,shift unless shift =~ /[A-Z]{1}/
32
+ end
33
+
34
+ def convert_shift(shift)
35
+ ("A".."Z").to_a.each_with_index.inject({}){|memo,(val,index)| memo[val] = index; memo }[shift]
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -1,7 +1,5 @@
1
- require 'rubygems'
2
- require 'pry'
3
- require 'pp'
4
1
  require 'aes'
2
+ require 'openssl'
5
3
 
6
4
  class CryptBuffer
7
5
  attr_accessor :bytes
@@ -44,16 +42,21 @@ class CryptBuffer
44
42
 
45
43
 
46
44
  def mod_sub(n,mod: 256)
45
+ tmp = bytes.map do |byte|
46
+ val = byte.to_bn.mod_sub(n,mod).to_i
47
+ end
48
+ CryptBuffer(tmp)
47
49
  end
50
+
48
51
  def sub(n)
49
-
52
+ CryptBuffer( bytes.map{|byte| byte -n } )
50
53
  end
51
54
  def add(n, mod: 256, offset: 0)
52
55
  real_mod = [256,mod].min
53
56
 
54
57
  tmp = bytes.map do |b|
55
58
  val = (b + n) % real_mod
56
- val > offset ? val : val+offset
59
+ val >= offset ? val : val+offset
57
60
  end
58
61
  CryptBuffer(tmp)
59
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto-toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Sivia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aes
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - lib/crypto-toolbox.rb
49
49
  - lib/crypto-toolbox/break_vigenere.rb
50
+ - lib/crypto-toolbox/ciphers/caesar.rb
50
51
  - lib/crypto-toolbox/crypt_buffer.rb
51
52
  - lib/crypto-toolbox/key_filter.rb
52
53
  - lib/crypto-toolbox/spell_checker.rb