You shouldn’t extend the String class since it is public and not in your name space. Think about all the SketchUp newbies who see this and will be hit by a truck.
The File.open method has a second argument for the read/write mode. It defaults to text (“t”), so try out to use “b” for binary:
Thank you so much… Here is the final test which proved to be successful…
require 'openssl'
module RafaelRivera
module SuCrypt
refine String do
def encrypt(key)
cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
cipher.key = Digest::SHA1.hexdigest key
s = cipher.update(self) + cipher.final
s.unpack('H*')[0].upcase
end #####
def decrypt(key)
cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').decrypt
cipher.key = Digest::SHA1.hexdigest key
s = [self].pack("H*").unpack("C*").pack("c*")
cipher.update(s) + cipher.final
end ####
end #### refine ####
end #### module ####
end #### module ####
module RafaelRivera
module SuCrypt
using RafaelRivera::SuCrypt
class Main
def initialize
key = 'secret'
@file = ENV['HOME'] + '/Desktop/sup/test.skp'
@file2 = ENV['HOME'] + '/Desktop/sup/test_encrypted.skp'
@file3 = ENV['HOME'] + '/Desktop/sup/test_decrypted.skp'
## Encrypt
su_encrypt(key)
## Decrypt
su_decrypt(key)
end ####
def su_encrypt(key)
## Plain
File.open(@file, "rb") { |f|
@plain = f.read()
}
File.open(@file2, "wb") { |f|
f.write( @plain.encrypt(key) )
}
end ####
def su_decrypt(key)
## Cipher
File.open(@file2, "rb") { |f|
@cipher = f.read()
}
File.open(@file3, "wb") { |f|
f.write(@cipher.decrypt(key))
}
end ####
end #### class ####
end #### module ####
end #### module ####
Sketchup.active_model.select_tool RafaelRivera::SuCrypt::Main.new
Yes, I should have known that
In my project, I am using Ruby on Rails so I’ll be able to check that rubyzip gem out… Thanks for the suggestion. I’ll check it out later and report back to you guys. Again thanks for the help!
Besides that, it is rather recommended to do no heavy work in the initialize method, only variable initialization, and do the heavy work in a method (run or apply…) that you call on the initialized instance.
Two examples where this will become a problem: Once you design test cases and just need to initialize an object of that type as stub (without running all its code), or when you want to create data objects for serialization.
The new code refines the String class and adds two new methods within my own module namespace. I hope this will not cause any issues with other extensions.
Then by adding ‘using RafaelRivera::SuCrypt’ I can use the new String methods in my code.