|
| 1 | +--- |
| 2 | +RFC: 0018 |
| 3 | +Author: Ilya Sazonov |
| 4 | +Status: Draft |
| 5 | +SupercededBy: n/a |
| 6 | +Version: 0.1 |
| 7 | +Area: Cmdlet |
| 8 | +Comments Due: 3/2/2017 |
| 9 | +--- |
| 10 | + |
| 11 | +# Get-StringHash cmdlet |
| 12 | + |
| 13 | +Add the new cmdlet to PowerShell to get cryptographically strong hashes for strings. |
| 14 | + |
| 15 | +Current .Net implementation of GetHashCode method calculates a hash that is not a global (across application domains) safe and is not a permanent value. |
| 16 | +According to the documentation, this method has many practical limitations. |
| 17 | +https://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx |
| 18 | +https://msdn.microsoft.com/en-us/library/system.string.gethashcode%28v=vs.110%29.aspx |
| 19 | +>As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted. |
| 20 | +
|
| 21 | +Currently Powershell only has Get-FileHash cmdlet to get a file hashes. |
| 22 | +Users are forced to use a workaround to get a string hashes: |
| 23 | +```powershell |
| 24 | +Get-FileHash -InputStream ([System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes("test string"))) |
| 25 | +``` |
| 26 | + |
| 27 | +## Motivation |
| 28 | + |
| 29 | +With the new cmdlet users can use native Powershell cmdlet syntax without a workaround. |
| 30 | + |
| 31 | +With the new cmdlet users can get hashes which: |
| 32 | + |
| 33 | +* is a cryptographically strong hashes |
| 34 | +* is across-platform |
| 35 | +* may be sended across application domains |
| 36 | +* may be saved in databases |
| 37 | +* may be used as key in collections |
| 38 | +* may be used to compare strings and texts |
| 39 | + |
| 40 | +## Specification |
| 41 | + |
| 42 | +### Output |
| 43 | + |
| 44 | +The cmdlet output objects of `StringHashInfo` type: |
| 45 | + |
| 46 | +```powershell |
| 47 | +public class StringHashInfo |
| 48 | +{ |
| 49 | + public string Algorithm { get; set;} |
| 50 | + public string Hash { get; set;} |
| 51 | + public string Encoding { get; set;} |
| 52 | + public string HashedString { get; set;} |
| 53 | +} |
| 54 | +``` |
| 55 | +`Hash` is a hash of the `HashedString` string calculated with `Algorithm` algorithm. |
| 56 | + |
| 57 | +`Encoding` is a encoding of the `HashedString` string. |
| 58 | + |
| 59 | +### Parameters |
| 60 | + |
| 61 | +1. InputString |
| 62 | + |
| 63 | + Type = array of strings |
| 64 | + |
| 65 | + Position = 0 |
| 66 | + |
| 67 | + ##### Attributes |
| 68 | + |
| 69 | + ValueFromPipeline = true |
| 70 | + |
| 71 | + AllowNull() |
| 72 | + |
| 73 | + AllowEmptyString() |
| 74 | + |
| 75 | +2. Algorithm |
| 76 | + |
| 77 | + Type = string |
| 78 | + |
| 79 | + Position = 1 |
| 80 | + |
| 81 | + ##### Attributes |
| 82 | + |
| 83 | + ValidateSet = SHA1, SHA256, SHA384, SHA512, MD5 |
| 84 | + |
| 85 | +3. Encoding |
| 86 | + |
| 87 | + Type = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding] |
| 88 | + |
| 89 | + Position = 2 |
| 90 | + |
| 91 | +### Encoding |
| 92 | + |
| 93 | +.Net hash algorithm methods work on byte buffer. So the cmdlet must convert a input string to byte array. |
| 94 | +This process is a encoding sensitive. |
| 95 | +To correctly process the user must specify the encoding of incoming strings. |
| 96 | + |
| 97 | +### InputString accept null and empty input strings |
| 98 | + |
| 99 | +.Net hash algorithm methods can calculate a hash for empty string without exception. |
| 100 | + |
| 101 | +.Net hash algorithm methods throw for null input buffer. |
| 102 | +This behavior is not convenient when processing an array of strings by using a pipeline: |
| 103 | +```powershell |
| 104 | + "test1", $null, "test2" | Get-StringHash |
| 105 | +``` |
| 106 | +Best behavior is to create null as result hash for null string and generate non-terminating error. |
| 107 | + |
| 108 | +## Alternate Proposals and Considerations |
| 109 | + |
| 110 | +None |
0 commit comments