Skip to content
This repository
Browse code

Never trust a Regexp to do a loops work

  • Loading branch information...
commit edfb4c0c993385adfdb19ede425bb994df1b04b6 1 parent 6569409
James Harton authored

Showing 1 changed file with 10 additions and 2 deletions. Show diff stats Hide diff stats

  1. +10 2 lib/lockbox.rb
12 lib/lockbox.rb
@@ -53,7 +53,7 @@ def self.encrypt(val)
53 53 hash = Digest::SHA2.new(256)
54 54 hash << val
55 55 max_size = (@@public_key.n.to_i.to_s(2).size / 8) - 11
56   - (([ hash.to_s ] + val.scan(Regexp.new ".{1,#{max_size}}")).collect { |part| @@public_key.public_encrypt(part) }).to_yaml
  56 + (([ hash.to_s ] + val.chunk(max_size)).collect { |part| @@public_key.public_encrypt(part) }).to_yaml
57 57 end
58 58 end
59 59
@@ -65,7 +65,7 @@ def self.decrypt(val)
65 65 if (yaml.is_a? Array) && (yaml.size > 1)
66 66 decrypted = yaml.collect { |part| @@private_key.private_decrypt(part) }
67 67 hash = decrypted[0]
68   - value = decrypted[1..-1] * ""
  68 + value = decrypted[1..-1].join
69 69 check = Digest::SHA2.new(256)
70 70 check << value
71 71 if check.to_s == hash
@@ -87,3 +87,11 @@ class StillLockedException < RuntimeError
87 87 end
88 88 class HashVerificationFailedException < RuntimeError
89 89 end
  90 +
  91 +# taken from http://stackoverflow.com/questions/754407/what-is-the-best-way-to-chop-a-string-into-chunks-of-a-given-length-in-ruby
  92 +# because scan() doesn't work with \n's etc.
  93 +class String
  94 + def chunk(size)
  95 + (0..(self.length-1)/size).map{|i|self[i*size,size]}
  96 + end
  97 +end

0 comments on commit edfb4c0

Please sign in to comment.
Something went wrong with that request. Please try again.