Class: IronNails::Security::SecureString

Inherits:
Object
  • Object
show all
Extended by:
System::Runtime::InteropServices, System::Security::Cryptography
Defined in:
lib/ironnails/security/secure_string.rb

Constant Summary collapse

ENCRYPTION_SALT =
"SailsPasswordSalt".freeze
@@entropy =
System::Text::Encoding.unicode.get_bytes(ENCRYPTION_SALT)

Class Method Summary collapse

Class Method Details

.decrypt_string(encrypted_data) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ironnails/security/secure_string.rb', line 43

def decrypt_string(encrypted_data)
  begin
    decrypted_data = ProtectedData.unprotect(
            Convert.from_base64_string(encrypted_data),
            @@entropy,
            DataProtectionScope.current_user);
    secure_string(System::Text::Encoding.unicode.get_bytes(decrypted_data));
  rescue
    System::Security::SecureString.new
  end
end

.encrypt_string(input) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/ironnails/security/secure_string.rb', line 17

def encrypt_string(input)
  encrypted_data = ProtectedData.protect(
          System::Text::Encoding.unicode.get_bytes(unsecure_string(input)),
          @@entropy,
          DataProtectionScope.current_user)
  System::Convert.to_base64_string(encrypted_data)
end

.secure_string(input) ⇒ Object



25
26
27
28
29
30
# File 'lib/ironnails/security/secure_string.rb', line 25

def secure_string(input)
  secure = System::Security::SecureString.new
  input.to_s.to_clr_string.to_char_array.each {|c| secure.append_char c }
  secure.make_read_only
  secure
end

.unsecure_string(input) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/ironnails/security/secure_string.rb', line 32

def unsecure_string(input)
  result = ""
  ptr = System::Runtime::InteropServices::Marshal.SecureStringToBSTR(input);
  begin
    result = System::Runtime::InteropServices::Marshal.PtrToStringBSTR(ptr);
  ensure
    System::Runtime::InteropServices::Marshal.ZeroFreeBSTR(ptr);
  end
  result.to_s
end