检查字符串是否在 PowerShell 中进行了 Base64 编码
check the string is Base64 encoded in PowerShell
我正在用PowerShell做条件选择,需要判断字符串是否是Base64编码的,
最简单直接的方法是什么?
if ($item -is [?base64])
{
# handle strings or characters
}
以下 returns $true
如果 $item
包含有效的 Base64 编码字符串,否则 $false
:
try { $null=[Convert]::FromBase64String($item); $true } catch { $false }
上面使用 System.Convert.FromBase64String
尝试将输入字符串 $item
转换为它表示的字节数组。
调用成功则忽略输出字节数组($null = ...
),输出$true
否则,进入catch
块,返回$false
。
警告:即使是常规字符串也可能意外地成为技术上有效的 Base64 编码字符串,即如果它们碰巧只包含来自 Base64 字符集的字符并且字符数是 4 的倍数。
例如,上面的测试为 "word"
产生 $true
(仅 Base64 字符,并且是 4 的倍数),但不为 "words"
(不是 4 个字符的倍数)产生。
例如,在 if
语句的上下文中:
- 注意:为了使
try
/ catch
语句 在if
有条件,$()
,必须使用subexpression operator。
# Process 2 sample strings, one Base64-encoded, the other not.
foreach ($item in 'foo', 'SGFwcHkgSG9saWRheXM=') {
if ($(try { $null=[Convert]::FromBase64String($item); $true } catch { $false })) {
'Base64-encoded: [{0}]; decoded as UTF-8: [{1}]' -f
$item,
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($item))
}
else {
'NOT Base64-encoded: [{0}]' -f $item
}
}
以上结果:
NOT Base64-encoded: [foo]
Base64-encoded: [SGFwcHkgSG9saWRheXM=]; decoded as UTF-8: [Happy Holidays]
很容易将功能包装在自定义 辅助函数中 , Test-Base64
:
# Define function.
# Accepts either a single string argument or multiple strings via the pipeline.
function Test-Base64 {
param(
[Parameter(ValueFromPipeline)]
[string] $String
)
process {
try { $null=[Convert]::FromBase64String($String); $true } catch { $false }
}
}
# Test two sample strings.
foreach ($item in 'foo', 'SGFwcHkgSG9saWRheXM=') {
if (Test-Base64 $item) {
"YES: $item"
}
else {
"NO: $item"
}
}
有关将字节与 Base64 编码字符串相互转换的信息,请参阅this answer。
我正在用PowerShell做条件选择,需要判断字符串是否是Base64编码的,
最简单直接的方法是什么?
if ($item -is [?base64])
{
# handle strings or characters
}
以下 returns $true
如果 $item
包含有效的 Base64 编码字符串,否则 $false
:
try { $null=[Convert]::FromBase64String($item); $true } catch { $false }
上面使用
System.Convert.FromBase64String
尝试将输入字符串$item
转换为它表示的字节数组。调用成功则忽略输出字节数组(
$null = ...
),输出$true
否则,进入
catch
块,返回$false
。
警告:即使是常规字符串也可能意外地成为技术上有效的 Base64 编码字符串,即如果它们碰巧只包含来自 Base64 字符集的字符并且字符数是 4 的倍数。
例如,上面的测试为 "word"
产生 $true
(仅 Base64 字符,并且是 4 的倍数),但不为 "words"
(不是 4 个字符的倍数)产生。
例如,在 if
语句的上下文中:
- 注意:为了使
try
/catch
语句 在if
有条件,$()
,必须使用subexpression operator。
# Process 2 sample strings, one Base64-encoded, the other not.
foreach ($item in 'foo', 'SGFwcHkgSG9saWRheXM=') {
if ($(try { $null=[Convert]::FromBase64String($item); $true } catch { $false })) {
'Base64-encoded: [{0}]; decoded as UTF-8: [{1}]' -f
$item,
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($item))
}
else {
'NOT Base64-encoded: [{0}]' -f $item
}
}
以上结果:
NOT Base64-encoded: [foo]
Base64-encoded: [SGFwcHkgSG9saWRheXM=]; decoded as UTF-8: [Happy Holidays]
很容易将功能包装在自定义 辅助函数中 , Test-Base64
:
# Define function.
# Accepts either a single string argument or multiple strings via the pipeline.
function Test-Base64 {
param(
[Parameter(ValueFromPipeline)]
[string] $String
)
process {
try { $null=[Convert]::FromBase64String($String); $true } catch { $false }
}
}
# Test two sample strings.
foreach ($item in 'foo', 'SGFwcHkgSG9saWRheXM=') {
if (Test-Base64 $item) {
"YES: $item"
}
else {
"NO: $item"
}
}
有关将字节与 Base64 编码字符串相互转换的信息,请参阅this answer。