2021-06-04 01:36:59 +00:00
|
|
|
$PASSWORD_STORE_DIR = (Get-Item "~\.password-store").FullName
|
2021-06-27 06:14:44 +00:00
|
|
|
$CLEAR_TIMEOUT = 45
|
2021-06-02 08:06:42 +00:00
|
|
|
|
|
|
|
function GeneratePassword {
|
|
|
|
param(
|
|
|
|
[Int] $Size = 10,
|
|
|
|
[Char[]] $Charsets = "ULNS",
|
|
|
|
[Char[]] $Exclude
|
|
|
|
)
|
|
|
|
|
|
|
|
$Chars = @(); $TokenSet = @()
|
|
|
|
If (!$TokenSets) {
|
|
|
|
$Global:TokenSets = @{
|
|
|
|
U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
L = [Char[]]'abcdefghijklmnopqrstuvwxyz'
|
|
|
|
N = [Char[]]'0123456789'
|
|
|
|
S = [Char[]]'!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$CharSets | ForEach {
|
|
|
|
$Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
|
|
|
|
If ($Tokens) {
|
|
|
|
$TokensSet += $Tokens
|
|
|
|
If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
|
|
|
|
return ($Chars | Sort-Object {Get-Random}) -Join ""
|
|
|
|
}
|
|
|
|
|
|
|
|
function EnsurePath {
|
|
|
|
param(
|
2021-06-27 06:11:57 +00:00
|
|
|
[String] $OrigPassPath,
|
|
|
|
[Bool] $CreateParents=$false
|
2021-06-02 08:06:42 +00:00
|
|
|
)
|
|
|
|
|
2021-06-27 06:11:57 +00:00
|
|
|
if (!$OrigPassPath) {
|
2021-06-12 02:48:43 +00:00
|
|
|
throw "path is empty!"
|
|
|
|
}
|
|
|
|
|
2021-06-27 06:11:57 +00:00
|
|
|
$PassPath = Join-Path $PASSWORD_STORE_DIR $OrigPassPath
|
2021-06-04 01:36:59 +00:00
|
|
|
if (!$PassPath.EndsWith(".gpg")) {
|
|
|
|
$PassPath = $PassPath + ".gpg"
|
|
|
|
}
|
|
|
|
$dir = Split-Path $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
if (!$dir) {
|
|
|
|
$dir = "."
|
|
|
|
}
|
2021-06-27 06:11:57 +00:00
|
|
|
if ($CreateParents -and !(Test-Path -PathType Container $dir)) {
|
|
|
|
# all output will be returned, suppress all output for all subcommands
|
|
|
|
New-Item -ItemType Directory -Path $dir > $null
|
2021-06-02 08:06:42 +00:00
|
|
|
}
|
2021-06-04 01:36:59 +00:00
|
|
|
return $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function GetUid {
|
|
|
|
if ((gpg --list-secret-keys | findstr uid) -match '<(.*?)>') {
|
|
|
|
$matches[1]
|
|
|
|
} else {
|
|
|
|
throw "unable to find default uid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:36:51 +00:00
|
|
|
function FzfPass {
|
|
|
|
Get-ChildItem $PASSWORD_STORE_DIR -Recurse -Filter *.gpg | %{
|
|
|
|
$_.FullName.SubString(
|
2021-06-04 01:36:59 +00:00
|
|
|
$PASSWORD_STORE_DIR.Length+1,
|
|
|
|
$_.FullName.Length-$PASSWORD_STORE_DIR.Length-5
|
2021-06-02 08:36:51 +00:00
|
|
|
)
|
2021-06-12 02:48:43 +00:00
|
|
|
} | fzf
|
2021-06-02 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:06:42 +00:00
|
|
|
function Edit-Pass {
|
|
|
|
[Cmdletbinding()]
|
|
|
|
param(
|
2021-06-04 01:36:59 +00:00
|
|
|
[String] $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
)
|
|
|
|
|
2021-06-04 01:36:59 +00:00
|
|
|
if (!$PassPath) {
|
|
|
|
$PassPath = FzfPass
|
2021-06-12 02:48:43 +00:00
|
|
|
if (!$?) {
|
|
|
|
return
|
|
|
|
}
|
2021-06-02 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 06:11:57 +00:00
|
|
|
$PassPath = EnsurePath -CreateParents $true $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
|
|
|
|
$tmpfile = (New-TemporaryFile).FullName
|
2021-06-04 01:36:59 +00:00
|
|
|
gpg --decrypt $PassPath > $tmpfile
|
2021-06-02 08:06:42 +00:00
|
|
|
nvim $tmpfile
|
|
|
|
|
|
|
|
if ($?) {
|
|
|
|
gpg -r (GetUid) -o "$tmpfile.gpg" --encrypt $tmpfile
|
2021-06-04 01:36:59 +00:00
|
|
|
Move-Item -Path "$tmpfile.gpg" -Destination "$PassPath" -Force
|
2021-06-02 08:06:42 +00:00
|
|
|
Remove-Item $tmpfile -Force
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function New-Pass {
|
|
|
|
[Cmdletbinding()]
|
|
|
|
param(
|
2021-06-04 01:36:59 +00:00
|
|
|
[Parameter(Mandatory=$true)] [String] $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
)
|
|
|
|
|
2021-06-27 06:11:57 +00:00
|
|
|
$PassPath = EnsurePath -CreateParents $true $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
$pass = GeneratePassword
|
2021-06-04 01:36:59 +00:00
|
|
|
if (Test-Path -PathType Leaf $PassPath) {
|
|
|
|
$text = gpg --decrypt $PassPath
|
|
|
|
if ($text -is [string]) {
|
|
|
|
$text = @($pass)
|
|
|
|
} else {
|
|
|
|
$text[0] = $pass
|
|
|
|
}
|
|
|
|
Remove-Item $PassPath -Force
|
2021-06-02 08:06:42 +00:00
|
|
|
} else {
|
|
|
|
$text = @($pass)
|
|
|
|
}
|
2021-06-04 01:36:59 +00:00
|
|
|
$text | gpg -r (GetUid) -o $PassPath --encrypt -
|
2021-06-02 08:06:42 +00:00
|
|
|
Set-Clipboard $pass
|
|
|
|
Write-Host $pass
|
|
|
|
Write-Host "new password is saved and copied to Clipboard"
|
|
|
|
}
|
|
|
|
|
|
|
|
function Get-Pass {
|
|
|
|
[Cmdletbinding()]
|
|
|
|
param(
|
2021-06-04 01:36:59 +00:00
|
|
|
[Parameter(Mandatory=$true)] [String] $PassPath,
|
2021-06-02 08:06:42 +00:00
|
|
|
[Bool] $Clipboard=$true
|
|
|
|
)
|
|
|
|
|
2021-06-04 01:36:59 +00:00
|
|
|
$PassPath = EnsurePath $PassPath
|
2021-06-02 08:06:42 +00:00
|
|
|
if ($Clipboard) {
|
2021-06-04 01:36:59 +00:00
|
|
|
$pass = gpg --decrypt $PassPath | Select -First 1
|
2021-06-02 08:06:42 +00:00
|
|
|
if ($pass) {
|
|
|
|
Set-Clipboard $pass
|
|
|
|
Write-Host "password is copied to Clipboard successfully"
|
2021-06-27 06:14:44 +00:00
|
|
|
if ($CLEAR_TIMEOUT -gt 0) {
|
|
|
|
Write-Host "and will be cleared out in $CLEAR_TIMEOUT seconds"
|
|
|
|
Start-Job -ArgumentList $pass,$CLEAR_TIMEOUT -ScriptBlock {
|
|
|
|
start-sleep -s $args[1] > $null
|
|
|
|
if ( (get-clipboard) -eq $args[0]) {
|
|
|
|
$null | clip.exe
|
|
|
|
}
|
|
|
|
} > $null
|
|
|
|
}
|
2021-06-02 08:06:42 +00:00
|
|
|
} else {
|
|
|
|
throw "password is empty"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-Host $pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Find-Pass {
|
2021-06-02 08:36:51 +00:00
|
|
|
$selected = FzfPass
|
2021-06-02 08:06:42 +00:00
|
|
|
|
|
|
|
if ($selected) {
|
|
|
|
Get-Pass $selected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:50:30 +00:00
|
|
|
function Find-Login {
|
|
|
|
$selected = FzfPass
|
|
|
|
|
|
|
|
if ($selected) {
|
|
|
|
$login = Split-Path -Leaf $selected
|
|
|
|
Write-Host $login
|
|
|
|
Set-Clipboard $login
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Export-ModuleMember -Function Edit-Pass,New-Pass,Get-Pass,Find-Pass,Find-Login
|
2021-06-02 08:06:42 +00:00
|
|
|
|