Class: Cloudcrypt::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcrypt/main.rb

Constant Summary collapse

ENCRYPTED_FILE_EXTENSION =
'.encrypted'
ENCRYPTED_VI_EXTENSION =
'.vi'
ENCRYPTED_KEY_EXTENSION =
'.key'
TMP =
'/tmp'
FILE_SEPARATOR =
'/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key_file, private_key_file = nil) ⇒ Main

Returns a new instance of Main.



20
21
22
23
24
25
26
# File 'lib/cloudcrypt/main.rb', line 20

def initialize(public_key_file,private_key_file=nil)
    @public_key_file = public_key_file
    @private_key_file = private_key_file
    


end

Instance Attribute Details

#dst_encrypted_fileObject (readonly)

Returns the value of attribute dst_encrypted_file.



18
19
20
# File 'lib/cloudcrypt/main.rb', line 18

def dst_encrypted_file
  @dst_encrypted_file
end

#dst_encrypted_iv_fileObject (readonly)

Returns the value of attribute dst_encrypted_iv_file.



18
19
20
# File 'lib/cloudcrypt/main.rb', line 18

def dst_encrypted_iv_file
  @dst_encrypted_iv_file
end

#dst_encrypted_key_fileObject (readonly)

Returns the value of attribute dst_encrypted_key_file.



18
19
20
# File 'lib/cloudcrypt/main.rb', line 18

def dst_encrypted_key_file
  @dst_encrypted_key_file
end

#dst_unencrypted_fileObject (readonly)

Returns the value of attribute dst_unencrypted_file.



18
19
20
# File 'lib/cloudcrypt/main.rb', line 18

def dst_unencrypted_file
  @dst_unencrypted_file
end

#dst_zip_fileObject (readonly)

Returns the value of attribute dst_zip_file.



18
19
20
# File 'lib/cloudcrypt/main.rb', line 18

def dst_zip_file
  @dst_zip_file
end

Class Method Details

.is_mac?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/cloudcrypt/main.rb', line 109

def self.is_mac?
  # universal-darwin9.0 shows up for RUBY_PLATFORM on os X leopard with the bundled ruby. 
  # Installing ruby in different manners may give a different result, so beware.
  # Examine the ruby platform yourself. If you see other values please comment
  # in the snippet on dzone and I will add them.
    RUBY_PLATFORM.downcase.include?("darwin")
end

.is_windows?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/cloudcrypt/main.rb', line 117

def self.is_windows?
    RUBY_PLATFORM.downcase.include?("mingw32")
end

Instance Method Details

#decrypt(file_path_zip, dst = TMP) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloudcrypt/main.rb', line 54

def decrypt(file_path_zip,dst=TMP)
    unzip(file_path_zip,dst)
    
    raise_unless_exists(@private_key_file)
    private_key = File.read(@private_key_file)
    
    # where files where extracted from the zip file
    dir_basename = dst
    # gets the filename without the extension .zip
    file_basename = File.basename(file_path_zip,'.zip') 
    
    
    # Where to look for key and iv files
    file_uri = dir_basename + FILE_SEPARATOR + file_basename
    source_encrypted_file = raise_unless_exists(file_uri + ENCRYPTED_FILE_EXTENSION)
    source_encrypted_key_file = raise_unless_exists(file_uri + ENCRYPTED_KEY_EXTENSION)
    source_encrypted_iv_file = raise_unless_exists(file_uri + ENCRYPTED_VI_EXTENSION)
    @dst_unencrypted_file = dst + '/' + file_basename
    
    iv = AsymmetricalCrypt.decrypt(File.read(@private_key_file),File.open(source_encrypted_iv_file,'rb').read)
    key = AsymmetricalCrypt.decrypt(File.read(@private_key_file),File.open(source_encrypted_key_file,'rb').read)
    
    s = SymmetricalCrypt.new(source_encrypted_file,@dst_unencrypted_file)
    s.decrypt(key,iv)
    #windows thinks it's opened
    #File.delete(source_encrypted_file,source_encrypted_key_file,source_encrypted_iv_file)
end

#encrypt(file_path, dst = TMP) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudcrypt/main.rb', line 28

def encrypt(file_path,dst=TMP)
    raise_unless_exists(@public_key_file)
    public_key = File.read(@public_key_file)
    
    file_basename=File.basename(file_path) #gets the filename without the extension
    file_uri = dst + FILE_SEPARATOR + file_basename
    @dst_encrypted_file = file_uri + ENCRYPTED_FILE_EXTENSION       
    @dst_encrypted_key_file = raise_if_exists(file_uri + ENCRYPTED_KEY_EXTENSION)
    @dst_encrypted_iv_file = raise_if_exists(file_uri + ENCRYPTED_VI_EXTENSION)
    
    s=SymmetricalCrypt.new(file_path,@dst_encrypted_file)
    s.encrypt
    
    File.open(@dst_encrypted_key_file,'wb') {|f|
           f << AsymmetricalCrypt.encrypt(public_key,s.random_key)
     }
    
    File.open(@dst_encrypted_iv_file,'wb') {|f|
           f << AsymmetricalCrypt.encrypt(public_key,s.random_iv)
     }
     @dst_zip_file = file_path + '.zip'
     zip(@dst_zip_file, [@dst_encrypted_file,@dst_encrypted_key_file,@dst_encrypted_iv_file])
     File.delete(@dst_encrypted_file,@dst_encrypted_key_file,@dst_encrypted_iv_file)
     
end

#md5(file) ⇒ Object



104
105
106
107
# File 'lib/cloudcrypt/main.rb', line 104

def md5(file)
    return false unless File.exists?(file)
    return Digest::MD5.hexdigest(File.read(file))
end

#unzip(file_zip, dst = TMP) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/cloudcrypt/main.rb', line 93

def unzip(file_zip,dst=TMP)
    Zip::ZipFile.open(file_zip) do |zip|
        zip.each do |f|
            f_path=File.join(dst, f.name)
            #Chef::Log.info(f_path)
            FileUtils.mkdir_p(File.dirname(f_path)) unless File.directory?(f_path)
            zip.extract(f, f_path) unless File.exist?(f_path)
        end
    end     
end

#zip(new_zip_file, files = []) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/cloudcrypt/main.rb', line 84

def zip(new_zip_file,files=[])
    Zip::ZipFile.open(new_zip_file,Zip::ZipFile::CREATE) do |zip|
        files.each do |file|
            file_basename = File.basename(file)
            zip.file.open(file_basename,'wb') {|f|f << File.open(file,'rb').read }
        end
    end
end