Advertisement
vovan333

Untitled

May 4th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $passwordHash-Path = "/keymgr-password-hash";
  2. $database-Path     = "/keymgr-db";
  3.  
  4. function md5 ($string) {
  5.  
  6.     $md5  = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
  7.     $utf8 = New-Object -TypeName System.Text.UTF8Encoding;
  8.     return [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($string)));
  9.  
  10. }
  11.  
  12. function compareHashes ($password) {
  13.  
  14.     if(md5($password) == Get-Content $passwordHash-Path) {
  15.  
  16.         return 1;
  17.  
  18.     } else {
  19.  
  20.         return 0;
  21.  
  22.     };
  23.  
  24. }
  25.  
  26. function requirePassword () {
  27.  
  28.     echo "Enter the password. Ctrl + C to abort.";
  29.     $password = Read-Host -prompt "Password : ";
  30.  
  31.     if(!compareHashes($password)) {
  32.  
  33.         echo "Sorry, try again"
  34.         requirePassword;
  35.  
  36.     };
  37.  
  38.     return $password;
  39.  
  40. }
  41.  
  42. function readFromDB ($password) {
  43.  
  44.     $contents = Get-Content $database-Path;
  45.     $string = ConvertFrom-SecureString -secureString $contents -secureKey $password;
  46.  
  47.     $string.split("\n");
  48.     foreach ($string as $value) {
  49.  
  50.         $value.split(":");
  51.  
  52.     };
  53.  
  54.     return $string;
  55.  
  56. }
  57.  
  58. function listAllPairs () {
  59.  
  60.     $password = requirePassword;
  61.     echo readFromDB($password);
  62.  
  63. }
  64.  
  65. function writeToDB ($pairs, $password) {
  66.  
  67.     foreach ($pairs as $key => $value) {
  68.  
  69.         $string += $base64encode($key) + ":" + $base64encode($value) + "\n";
  70.  
  71.     };
  72.  
  73.     return ConvertTo-SecureString -string $string -secureKey $password > "/windows/keymgr.db";
  74.  
  75.  
  76. }
  77.  
  78. function base64encode ($string) {
  79.  
  80.     $bytes = [System.Text.Encoding]::Unicode.GetBytes($string);
  81.     return [Convert]::ToBase64String($bytes);
  82.  
  83. }
  84.  
  85. function setPair ($key, $value) {
  86.  
  87.     $password = requirePassword;
  88.  
  89.     $pairs = readFromDB($password);
  90.     $pairs[$key] = $value;
  91.  
  92.     writeToDB($pairs, $password);
  93.  
  94. }
  95.  
  96. function removePair ($key, $value) {
  97.  
  98.     $password = requirePassword;
  99.     $pairs = $readFromDB($password);
  100.     $pairs[$key] = $value;
  101.  
  102. }
  103.  
  104. function keymgr ($operator, $key, $value) {
  105.  
  106.     switch ($operator) {
  107.  
  108.         "set" {
  109.  
  110.             setPair($key, $value);
  111.  
  112.         }
  113.  
  114.         "remove" {
  115.  
  116.             removePair($key);
  117.  
  118.         }
  119.  
  120.         default {
  121.  
  122.             listAllPairs;
  123.  
  124.         }
  125.  
  126.     };
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement