Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $passwordHash-Path = "/keymgr-password-hash";
- $database-Path = "/keymgr-db";
- function md5 ($string) {
- $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
- $utf8 = New-Object -TypeName System.Text.UTF8Encoding;
- return [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($string)));
- }
- function compareHashes ($password) {
- if(md5($password) == Get-Content $passwordHash-Path) {
- return 1;
- } else {
- return 0;
- };
- }
- function requirePassword () {
- echo "Enter the password. Ctrl + C to abort.";
- $password = Read-Host -prompt "Password : ";
- if(!compareHashes($password)) {
- echo "Sorry, try again"
- requirePassword;
- };
- return $password;
- }
- function readFromDB ($password) {
- $contents = Get-Content $database-Path;
- $string = ConvertFrom-SecureString -secureString $contents -secureKey $password;
- $string.split("\n");
- foreach ($string as $value) {
- $value.split(":");
- };
- return $string;
- }
- function listAllPairs () {
- $password = requirePassword;
- echo readFromDB($password);
- }
- function writeToDB ($pairs, $password) {
- foreach ($pairs as $key => $value) {
- $string += $base64encode($key) + ":" + $base64encode($value) + "\n";
- };
- return ConvertTo-SecureString -string $string -secureKey $password > "/windows/keymgr.db";
- }
- function base64encode ($string) {
- $bytes = [System.Text.Encoding]::Unicode.GetBytes($string);
- return [Convert]::ToBase64String($bytes);
- }
- function setPair ($key, $value) {
- $password = requirePassword;
- $pairs = readFromDB($password);
- $pairs[$key] = $value;
- writeToDB($pairs, $password);
- }
- function removePair ($key, $value) {
- $password = requirePassword;
- $pairs = $readFromDB($password);
- $pairs[$key] = $value;
- }
- function keymgr ($operator, $key, $value) {
- switch ($operator) {
- "set" {
- setPair($key, $value);
- }
- "remove" {
- removePair($key);
- }
- default {
- listAllPairs;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement