Skip to content

Instantly share code, notes, and snippets.

@r4um
Created April 18, 2012 13:47
Show Gist options
  • Save r4um/2413666 to your computer and use it in GitHub Desktop.
Save r4um/2413666 to your computer and use it in GitHub Desktop.
System::Security::Cryptography example
require 'System'
require 'pp'
dotnet_path = System::Runtime::InteropServices::RuntimeEnvironment.GetRuntimeDirectory()
system_security = dotnet_path + "\\" + "System.Security.dll"
require system_security
include System::Security::Cryptography
class MyCredentialStore
def initialize(file, entropy = "Foo")
@file = file
@encoding = System::Text::ASCIIEncoding.new
@entropy = @encoding.get_bytes(entropy)
end
def write(data)
d_a = @encoding.get_bytes(data)
e_a = ProtectedData.Protect(d_a, @entropy, DataProtectionScope.CurrentUser)
fstream = System::IO::File.open(@file, System::IO::FileMode.open_or_create)
swriter = System::IO::BinaryWriter.new(fstream)
swriter.write(e_a)
swriter.close()
fstream.close()
end
def read()
fstream = System::IO::File.open(@file, System::IO::FileMode.open)
sreader = System::IO::BinaryReader.new(fstream)
e_a = sreader.ReadBytes(fstream.length)
d_a = ProtectedData.Unprotect(e_a, @entropy, DataProtectionScope.CurrentUser)
sreader.close()
fstream.close()
@encoding.get_string(d_a)
end
end
cs = MyCredentialStore.new("foo.dat")
cs.write("foo")
pp cs.read
app_data_dir = System::Environment.GetFolderPath(System::Environment::SpecialFolder.LocalApplicationData)
app_data_dir += "\\Foo"
pp app_data_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment