Sha256: 0f41752078c93157c68af7d2dac631225b6141367dcb03449b0b7b5af4ea781d

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module IronNails

  module Security

    class SecureString

      ENCRYPTION_SALT = "SailsPasswordSalt".freeze



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

      class << self
        include System::Security::Cryptography
        include System::Runtime::InteropServices

        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

        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

        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

        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

      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ironnails-0.0.3 lib/ironnails/security/secure_string.rb
ironnails-0.0.1 lib/ironnails/security/secure_string.rb