Repository navigation
Expand file tree
/
Copy pathGet-StringHash.ps1
More file actions
90 lines (64 loc) · 2.84 KB
/
Copy pathGet-StringHash.ps1
File metadata and controls
90 lines (64 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env powershell
<#
.SYNOPSIS
Compute the hash value for a string by using a specific hash algorithm.
.DESCRIPTION
The Get-StringHash script computes the hash value for a string by using a specified hash algorithm. A hash value is a unique value that corresponds to the content of the string.
By default, the Get-StringHash script uses the SHA256 algorithm, although any hash algorithm that is supported by the target operating system can be used.
.PARAMETER Algorithm
Specifies the cryptographic hash function to use for computing the hash value of the contents of the specified file. A cryptographic hash function includes the property that it is not possible to find two distinct inputs that generate the same hash values. Hash functions are commonly used with digital signatures and for data integrity. The acceptable values for this parameter are:
-- SHA1
-- SHA256
-- SHA384
-- SHA512
-- MD5
If no value is specified, or if the parameter is omitted, the default value is SHA256.
.PARAMETER String
Specifies the string that is to be hashed.
.OUTPUTS
StringHashInfo
Get-StringHash returns an object that represents the string that was hashed, the value of the computed hash, and the algorithm used to compute the hash.
.EXAMPLE
Get-StringHash.ps1 -String "HashThisString" | Format-List
Hash : BE767EABA134CB2F01E8D1755A8DD3B18BC8B063049CFF5E6228F5F7143FF777
Algorithm : SHA256
SourceString : HashThisString
.EXAMPLE
"PipelineString" | Get-StringHash.ps1 | Format-List
Hash : 26F5E3BDA4FED8F257BD76B4653CB3D49FE572031891DE74E532E6AE088FE892
Algorithm : SHA256
SourceString : PipelineString
.EXAMPLE
Get-StringHash.ps1 -String "UseMD5" -Algorithm MD5 | Format-List
Hash : 1634763BFA8CFCB2C795A05B894554CA
Algorithm : MD5
SourceString : UseMD5
#>
param (
[Parameter(Position=0,mandatory=$true, ValueFromPipeline=$true)]
[String] $String,
[ValidateSet("SHA1","SHA256","SHA384","SHA512","MD5")]
[Parameter(Position=1,mandatory=$false)]
[String] $Algorithm = "SHA256"
)
# The StringHashInfo Class is used to make the return data similar to FileHashInfo for consistency.
class StringHashInfo {
[String] $Hash;
[String] $Algorithm;
[String] $SourceString;
hidden [int] $HashCode;
StringHashInfo([Microsoft.PowerShell.Commands.FileHashInfo] $FileHashInfo, [String] $StringInput) {
$this.Algorithm = $FileHashInfo.Algorithm;
$this.Hash = $FileHashInfo.Hash;
$this.HashCode = $FileHashInfo.GetHashCode();
$this.SourceString = $StringInput
}
[int] GetHashCode() {
return $this.HashCode
}
}
$byteArray = [System.Text.Encoding]::UTF8.GetBytes($String)
$stream = [System.IO.MemoryStream]::new($byteArray)
$hash = Get-FileHash -InputStream $stream -Algorithm $Algorithm
$stream.Close()
[StringHashInfo]::new($hash, $String)