diff --git a/FolderEncoder/.gitattributes b/FolderEncoder/.gitattributes new file mode 100644 index 0000000..59236a3 --- /dev/null +++ b/FolderEncoder/.gitattributes @@ -0,0 +1,3 @@ +*.ps1 text eol=lf +*.psd1 text eol=lf +*.md text eol=lf diff --git a/FolderEncoder/FolderEncoder.psd1 b/FolderEncoder/FolderEncoder.psd1 index 7f403b9..ebca8db 100644 --- a/FolderEncoder/FolderEncoder.psd1 +++ b/FolderEncoder/FolderEncoder.psd1 @@ -12,7 +12,7 @@ # RootModule = '' # Version number of this module. -ModuleVersion = '2.0.0' +ModuleVersion = '2.0.1' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/FolderEncoder/README.md b/FolderEncoder/README.md index 57ad2b2..5a9da17 100644 --- a/FolderEncoder/README.md +++ b/FolderEncoder/README.md @@ -4,8 +4,6 @@ Used as backups encoding solution. Encoding performed with 7z password protection -[![PowerShell 3](https://Stadub-Gh.visualstudio.com/PowershellScripts/_apis/build/status/Bookmarks?branchName=master)](https://Stadub-Gh.visualstudio.com/PowershellScripts/_build/latest?definitionId=3?branchName=master) -[![PowerShell 4, 5 & Core on Windows build](https://ci.appveyor.com/api/projects/status/7tmg8wy30ipanjsd?svg=true)](https://ci.appveyor.com/project/stadub/powershellscripts) [![Linux & MacOS build](https://img.shields.io/travis/stadub/PowershellScripts/master.svg?label=linux/macos+build)](https://travis-ci.org/stadub/PowershellScripts) [![latest version](https://img.shields.io/powershellgallery/v/FolderEncoder.svg?label=latest+version)](https://www.powershellgallery.com/packages/FolderEncoder/) [![downloads](https://img.shields.io/powershellgallery/dt/FolderEncoder.svg?label=download)](https://www.powershellgallery.com/packages/FolderEncoder) @@ -117,3 +115,12 @@ encoding, download tools, prompt, or create key/archive files. CI checks manifes import/export and parameter contracts only. The legacy encoder algorithm, external tools and archive roundtrip are unchanged and are not validated by these checks. There is no Gallery publication route or release package in this source repair. + +## Release preparation + +Version 2.0.1 preserves the existing MIT LicenseUri and restores the documented +Invoke-FolderEncode import/export contract. Package generation must not substitute +the repository BSD license for this module's existing license declaration. +Source and installed import/manifest/parameter tests do not validate the legacy +encoder, external downloads or archive roundtrips. Review those limitations before +any actual publication; no software was uploaded during preparation. diff --git a/FolderEncoder/out/FolderEncoder.psd1 b/FolderEncoder/out/FolderEncoder.psd1 new file mode 100644 index 0000000..ebca8db --- /dev/null +++ b/FolderEncoder/out/FolderEncoder.psd1 @@ -0,0 +1,126 @@ +# +# Module manifest for module 'EncodeFolder' +# +# Generated by: Dmitry Stadub +# +# Generated on: 18.04.2019 +# + +@{ + +# Script module or binary module file associated with this manifest. +# RootModule = '' + +# Version number of this module. +ModuleVersion = '2.0.1' + +# Supported PSEditions +# CompatiblePSEditions = @() + +# ID used to uniquely identify this module +GUID = '41419ba2-f50f-4b1b-af69-dc1128805339' + +# Author of this module +Author = 'Dmitry Stadub' + +# Company or vendor of this module +CompanyName = '' + +# Copyright statement for this module +Copyright = '(c) Dmitry Stadub. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'Encode files from folder for(for example) uploading to cloud' + +# Minimum version of the PowerShell engine required by this module +# PowerShellVersion = '' + +# Name of the PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. +# CLRVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +# RequiredModules = @() + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +# FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +NestedModules = @('Functions.ps1') + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = @('Invoke-FolderEncode') + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = @() + +# Variables to export from this module +VariablesToExport = '*' + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = @('*') + +# DSC resources to export from this module +# DscResourcesToExport = @() + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = @('7zip','encode','zip', 'archive', 'backup', 'compress', '7-Zip', 'Encrypt') + + # A URL to the license for this module. + LicenseUri = 'http://opensource.org/licenses/MIT' + + # A URL to the main website for this project. + ProjectUri = 'https://github.com/stadub/PowershellScripts/tree/master/FolderEncoder' + + # A URL to an icon representing this module. + IconUri = 'https://raw.githubusercontent.com/stadub/PowershellScripts/master/FolderEncoder/Icon.ico' + + # ReleaseNotes of this module + ReleaseNotes = ' + This is the first piblic reliease. + ' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +HelpInfoURI = 'https://github.com/stadub/PowershellScripts/blob/master/FolderEncoder/README.md' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} + diff --git a/FolderEncoder/out/Functions.ps1 b/FolderEncoder/out/Functions.ps1 new file mode 100644 index 0000000..c25a66a --- /dev/null +++ b/FolderEncoder/out/Functions.ps1 @@ -0,0 +1,326 @@ +ο»Ώ<#PSScriptInfo + +.VERSION 2.0 + +.GUID 6d06e54e-f29e-4c27-9fb2-ed1dfbd0dc79 + +.AUTHOR Dima Stadub + +.COMPANYNAME + +.COPYRIGHT + +.TAGS 7zip encode zip archive + +.LICENSEURI + +.PROJECTURI https://github.com/stadub/PowershellScripts + +.ICONURI + +.EXTERNALMODULEDEPENDENCIES + +.REQUIREDSCRIPTS + +.EXTERNALSCRIPTDEPENDENCIES + +.RELEASENOTES + + +#> + + +<# +.SYNOPSIS + Encode files from folder for(for example) uploading to cloud +.DESCRIPTION +Used as backups encoding solution. +Encoding performed with 7z password protection + +There was no reasons (thank goodness) decoding stored data so currently there no decoder script +It you need - please create github issue and I'll add scrpit for folder decoding. +Manually files can be decoded with the next algorithm: +1)Concat value from .masterKey and add ':' to beginign and by using resulting key extract .keys file from .keys.7z +2)Find apropriate row in .key for file that shoudl be decoded. +3)Concat first row from .masterKey with first row from .key file and ':' to beginign +4)Use key from previouse step as 7z password for decode file +.EXAMPLE + Invoke-FolderEncode "c:\windows\System42" "d:\System" +.PARAMETER SrcFolder (optional) + Folder to be encoded +.PARAMETER DestFolder + Destanation folder where encoded files be created + #> +param ( + [string]$SrcFolder = (Get-Location).Path, + + [string]$DestFolder = (Get-Location).Path +) + +#$DebugPreference = "Continue" + +$Slash = [IO.Path]::DirectorySeparatorChar; + + +$binDir="$PSScriptRoot\bin\${MyInvocation.MyCommand.Name}" +$7zWin = "$binDir\7za.exe" +$sha1 = "$binDir\fciv.exe" +$7zWinDownloadDir='https://www.7-zip.org/a/7za920.zip' + +$sha1Win="https://download.microsoft.com/download/c/f/4/cf454ae0-a4bb-4123-8333-a1b6737712f7/Windows-KB841290-x86-ENU.exe" + + +$script:7zPath = $null; +$script:shaTool = $null + + +function DownloadUtil { + param ( + [string]$name, + [string]$file, + [string]$url + ) + + $reply = Read-Host -Prompt "Would you like to download '$name'?[y/n] ` + [Y] Yes [N] No [S] Suspend(default is ""Yes""):" + + if ( -not $reply -match "[yY]" -and $null -ne $reply ) { + throw "Execution aborted" + } + + Write-Output "Starting download '$name'" + + Write-Debug "Downoad url:${url}" + + (New-Object System.Net.WebClient).DownloadFile($url, $file) + + Write-Debug "File downloaded to ${file}." + +} + + + +function CheckUtils { + $os = [System.Environment]::OSVersion.Platform + #windows + if($os -like 'win*'){ + Write-Debug "Os:Windows" + + #7-zip + Write-Debug "Trying to locate 7-zip by the path: ${7zWin}" + + if( ! (Test-Path $7zWin)){ + Write-Output '7Zip didn''t found.' + + $file = [System.IO.Path]::GetTempFileName() + Write-Debug "Temp file: ${file}" + + DownloadUtil "7-Zip" $file $7zWinDownloadDir + Write-Debug "Extracting to directory ${binDir}" + if ( ! (Test-Path $binDir )) {mkdir $binDir} + Add-Type -AssemblyName System.IO.Compression.FileSystem + [System.IO.Compression.ZipFile]::ExtractToDirectory($file, $binDir) + } + $script:7zPath = $7zWin + + #sha1 + Write-Debug "Trying to locate sha1 util by the path: ${sha1Win}" + if( ! (Test-Path $sha1)){ + Write-Output 'fciv didn''t found.' + + $file = [System.IO.Path]::GetTempFileName() + Write-Debug "Temp file: ${file}" + + DownloadUtil "fciv" $file $sha1Win + if ( ! (Test-Path $binDir )) {mkdir $binDir} + & $7zPath e $file "-o${binDir}" -y + } + $script:shaTool = "${sha1} -sha1 "; + + } + else{ + #linux or macOs + get-command p7zip -Erroraction 'silentlycontinue'| ForEach-Object{ $7zPath= $_.Path} + if ( ! $7zPath ){ + #sudo apt-get install p7zip + $script:aborted = $true + throw "p7zip application is not installed. + If you using debian/ubuntu run `sudo apt-get install p7zip` then restart the script" + } + $shaTool = "shasum " + } +} + +$script:aborted = $false + +$Keyfile = ".keys"; +$KeyfilePath = $DestFolder + $Slash + $Keyfile; +$MaserKeyfilePath = $DestFolder + $Slash + ".masterKey" + + +function ZipFile { + param ( + [ValidateScript( { Test-Path ( $SrcFolder + $Slash + $_ ) -pathType leaf })] + [Parameter(Mandatory = $true)] + [string]$FileName, + + [Parameter(Mandatory = $true)] + [string]$Key, + [string]$SrcFolder, + [string]$DestFolder + ) + Write-Output "Creating Archive for file ""$FileName""" + + $_srcFile = $SrcFolder + $Slash + $FileName + $_dstFile = $DestFolder + $Slash + $FileName.Replace('.', '_') + + + if( Test-Path $DestFolder -PathType Leaf){ + Write-Error "The destanation path ${DestFolder} is file." + } + if( ! (Test-Path $DestFolder) ){ + Write-Information "The destanation path ${DestFolder} doesn't exist. Creating folder." + mkdir $DestFolder + } + + if ( Test-Path "${_dstFile}.7z" ) { + Write-Error "Arhive ""$FileName"" allready exist in folder ""$DestFolder"", please select another destination." + return -1; + } + + $AllArgs = @('a', "$_dstFile", "$_srcFile", "-p:$Key"); + + $process = Start-Process -FilePath $7zPath -ArgumentList $AllArgs -NoNewWindow -PassThru -Wait + + $code = $process.ExitCode.ToString(); + + Write-Output "File compressed. Return code: $code" + return $process.ExitCode + +} + +function GetFileHash { + param ( + [ValidateScript( { Test-Path ($_ ) -pathType leaf })] + [Parameter(Mandatory = $true)] + [string]$FilePath + ) + + # --- getting file sha1 checksum + + $_hash = Invoke-Expression "$shaTool $FilePath" + #remove extra lines + $_hash = $_hash | ForEach-Object { + if ( ![string]::IsNullOrEmpty($_.ToString()) -and !$_.ToString().StartsWith("//")) { + Write-Output $_; + } + }; + #extract hash + $_hash = $_hash.Split(' ')[0]; + + + Write-Output $_hash +} + +function EncodeFile() { + param ( + [Parameter(Mandatory = $true)] + [string]$Salt, + [Parameter(Mandatory = $true)] + [string]$SrcFolder, + [Parameter(Mandatory = $true)] + [string]$DestFolder, + [ValidateScript( { Test-Path ( $SrcFolder + $Slash + $_ ) -pathType leaf })] + [Parameter(Mandatory = $true)] + [string]$FileName + ) + + Write-Output "Encoding file ""$FileName""" + + $srcFile = $SrcFolder + $Slash + $FileName; + + + $hash = GetFileHash -FilePath $srcFile; + + $key = $Salt + $hash; + + Write-Output "$hash $FileName">>$KeyfilePath; + + $result = ZipFile -FileName $FileName -Key $Key -SrcFolder $SrcFolder -DestFolder $DestFolder; + + if ( $result -eq "0" ) { + Write-Error "Encoding aborted. Some error occured." + $script:aborted = $false + } + +} + +<# + .Synopsis + Encode files from folder for(for example) uploading to cloud + + .Description + Used as backups encoding solution. + Encoding performed with 7z password protection + + .Parameter DestFolder + Destanation folder where encoded files be created. + + .Parameter SrcFolder + Folder to be encoded. + + .Example + # Add bookmark with name. + />Invoke-FolderEncode "c:\windows\System42" "d:\System\" +#> +function Invoke-FolderEncode { + param ( + [Parameter(Mandatory = $true)][string]$DestFolder, + [Parameter(Mandatory = $false)][string]$SrcFolder + ) + $ErrorActionPreference ="Stop" + + if ( [string]::IsNullOrEmpty($SrcFolder) ) { + $SrcFolder = (Get-Location).Path + } + CheckUtils + if ( Test-Path $KeyfilePath ) { + Write-Error "Key file already exist" -ErrorAction:Continue + + $reply = Read-Host -Prompt "Would you like to remove it? [y/n] ` + [Y] Yes [N] No [S] Suspend(default is ""Yes""):" + if ( -not $reply -match "[yY]" -and $null -ne $reply ) { + Write-Error "Execution aborted" + return -1; + } + + Remove-Item -Force $KeyfilePath | out-null + } + + $salt = [System.Guid]::NewGuid().ToString(); + + Write-Output "Start encoding the folder ${SrcFolder}" + Get-ChildItem $SrcFolder | ForEach-Object { + if ($script:aborted -eq $true) { return; } + $FileName = $_.Name; + if( $_.Attributes -eq [System.IO.FileAttributes]::Directory ){ + Write-Warning -Message "Cannot process the deirectory ${FileName}. Directories are not supported." + }else{ + EncodeFile $salt $SrcFolder $DestFolder $FileName + } + + } + + EncodeFile $salt $SrcFolder $DestFolder $Keyfile + + $KeysFileHash = Get-Content $KeyfilePath | Select-Object -last 1 + $KeysFileHash = $KeysFileHash.Split(' ')[0]; + + Write-Output "$salt $KeysFileHash">$MaserKeyfilePath +} + +#CheckUtils + +#EncodFolder $SrcFolder $DestFolder; + +Export-ModuleMember -Function Invoke-FolderEncode \ No newline at end of file diff --git a/FolderEncoder/out/Icon.ico b/FolderEncoder/out/Icon.ico new file mode 100644 index 0000000..893beeb Binary files /dev/null and b/FolderEncoder/out/Icon.ico differ diff --git a/FolderEncoder/out/README.md b/FolderEncoder/out/README.md new file mode 100644 index 0000000..5a9da17 --- /dev/null +++ b/FolderEncoder/out/README.md @@ -0,0 +1,126 @@ +# Invoke-FolderEncode - Encode files from a folder for(for example) uploading to the cloud + +Used as backups encoding solution. + +Encoding performed with 7z password protection + +[![Linux & MacOS build](https://img.shields.io/travis/stadub/PowershellScripts/master.svg?label=linux/macos+build)](https://travis-ci.org/stadub/PowershellScripts) +[![latest version](https://img.shields.io/powershellgallery/v/FolderEncoder.svg?label=latest+version)](https://www.powershellgallery.com/packages/FolderEncoder/) +[![downloads](https://img.shields.io/powershellgallery/dt/FolderEncoder.svg?label=download)](https://www.powershellgallery.com/packages/FolderEncoder) + + [![https://www.powershellgallery.com/packages/FolderEncoder/](https://img.shields.io/badge/PowerShell%20Gallery-download-blue.svg?style=popout&logo=powershell)](https://www.powershellgallery.com/packages/FolderEncoder/2.0.0) + +![ConsoleDemo](https://raw.githubusercontent.com/stadub/PowershellScripts/master/FolderEncoder/Assets/demo.gif) + +## πŸ“˜ Commands + +```powershell +Invoke-FolderEncode - Encode folder + [-DestFolder] + [-SrcFolder] (Optional) +``` + +## πŸ“ƒ Usage + +```powershell +/>Invoke-FolderEncode -DestFolder "d:\System\" -SrcFolder "c:\windows\System42" +/> cd c:\Windows; Invoke-FolderEncode "d:\backup\" #use current folder as src +``` + +## Decoding + +There is no decoding script. Because currently used only for backups and(thank goodness) there was no necessity to decode it. +To decrypt manually: + +There were no reasons (thank goodness) decoding stored data so currently there no decoder script. +If you need - please create GitHub issue and I'll add a script for folder decoding. + +Manually files can be decoded with the next algorithm: +1 Concat value from .masterKey and add ':' to the beginning and by using resulting key extract .keys file from .keys.7z + +2 Find the appropriate row in .key for a file that should be decoded. + +3 Concat first row from .masterKey with the first row from .key file and ':' to beginning + +4 Use the key from the previous step as 7z password for decode file + +## πŸ“ˆ Changelog + +### [v2.0](https://github.com/stadub/PowershellScripts/releases/tag/v0.2.0) + +* Added dependencies auto download. +* Added platform-specific tools for Linux/MacOs + +### [v1.0.2](https://github.com/stadub/PowershellScripts/releases/tag/v0.1.0) + +* Fixed key file check. + +### [v1.0.1](https://github.com/stadub/PowershellScripts/releases/tag/v0.1.0) + +* Moved module to an individual folder. +* Added module methods description. + +### [v1.0.0](https://github.com/stadub/PowershellScripts/releases/tag/v0.1.0) + +* Created description and usage examples + +### [v0.9](https://github.com/stadub/PowershellScripts/releases/tag/v0.1.0) Aug 15, 2015 + +* Create Windows folder encoding scripts: + +```powershell +ZipFile + [FileName] + [Key] + [SrcFolder] + [DestFolder] + +GetFileHash + [FilePath] + +EncodeFile + [Salt] + [SrcFolder] + [DestFolder] + [FileName] + +Invoke-FolderEncode + [SrcFolder] + [DestFolder] + +``` + +## πŸ’² Motivation + +The modules are actively maintained during evenings and weekends, +so if you found it useful you can Buy me a beer/cup of tea 😊. + +[![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://www.buymeacoffee.com/dima) [![Support by Yandex](https://raw.githubusercontent.com/GitStatic/Resources/master/yaMoney.png)](https://money.yandex.ru/to/410014572567962/200) + + + +## πŸ“¬ Suggestions and feedback + +If you have any idea or suggestion - please add a github issue and I'll try to implement it as soon as possibleπŸ˜‰ + + +## Source import validation + +The missing Functions.ps1 is restored unchanged from the last repository version +before its deletion in 4a336c7. The manifest now exports the documented +Invoke-FolderEncode command. Importing the module does not invoke CheckUtils or +encoding, download tools, prompt, or create key/archive files. CI checks manifest, +import/export and parameter contracts only. The legacy encoder algorithm, external +tools and archive roundtrip are unchanged and are not validated by these checks. +There is no Gallery publication route or release package in this source repair. + +## Release preparation + +Version 2.0.1 preserves the existing MIT LicenseUri and restores the documented +Invoke-FolderEncode import/export contract. Package generation must not substitute +the repository BSD license for this module's existing license declaration. +Source and installed import/manifest/parameter tests do not validate the legacy +encoder, external downloads or archive roundtrips. Review those limitations before +any actual publication; no software was uploaded during preparation.