Powershell脚本的散列 [英] Powershell script for hash

查看:122
本文介绍了Powershell脚本的散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件(逗号分隔值)
文件如下所示:

  20171108,120909470 ,SO1244,12,101 
20171109,122715740,AG415757,11,101

我需要隐藏数据(例如)第3列,并且不会影响文件中的任何其他条目。

我想使用SHA1或MD5等哈希算法执行此操作,所以我们需要将数据发送给第三方,并且某些列包含敏感信息(例如,客户名)。我需要这个文件是完整的,并且在一个字符串被替换的地方,我需要在每次遇到它的时候以相同的方式完成它(以便保留任何映射或分组)。它不需要军事加密,只是很难扭转。正如我需要间歇性地,脚本化的解决方案将是理想的。



使用命令行工具或脚本实现此目的的最简单方法是什么?



首先,我想要一个PowerShell脚本,因为它不需要任何额外的软件来实现...



问题似乎是重复的我需要在CSV文件中散列(混淆)一列数据。脚本首选,但提出的解决方案没有解决我的问题,并引发以下错误:

 您无法调用方法在一个空值表达式上。 
在C:\ Users \ mey \ Hashr.ps1:4 char:5
+ $ _。column3 = $ _。column3.gethashcode()

脚本如下

 ( Import-Csv .\results.csv -delimiter',')| ForEach-Object {
$ _。column3 = $ _。column3.gethashcode()
$ _
} | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter','

更新:



以下是我正在运行的程序,并且已由@BaconBits提出:

 参数(
[参数(必须= $ true,ValueFromPipeline = $ true,Position = 0)]
[String []]
$ String,
[Parameter(Position = 1)]
[ValidateSet('SHA1','MD5','SHA256','SHA384','SHA512')]
[String]
$ HashName ='SHA256'


$ {
$ StringBuilder = [System.Text.StringBuilder] :: new(128)
[System.Security.Cryptography.HashAlgorithm] :: Create( $ HashName).ComputeHash([System.Text.Encoding] :: UTF8.GetBytes($ String))| ForEach-Object {
[Void] $ StringBuilder.Append($ _。ToString(x2))
}
$ StringBuilder.ToString()
}
}
$ csv = Import-Csv .\results.csv -delimiter','
foreach($ csv中的$ line){
$ line.column1 = Get-StringHash $ line。 column1
}
$ csv | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter','

我的csv文件import是另一个java程序的输出,它不创建头文件,它只是用值填充csv文件。



我得到这个错误
Get- StringHash:无法将参数绑定到参数'String',因为它是null。

解决方案

基于文档,你不会想要使用 GetHashCode()这种方式:


哈希代码旨在有效插入并查找基于散列表的
集合。哈希码不是
永久值。出于这个原因:


  • 不要序列化哈希码值或将它们存储在数据库中。


  • 不要使用哈希码作为从键控集合中检索对象的键。

  • 不要发送哈希码跨应用程序域或进程。在某些情况下,散列码可以根据每个进程或
    每个应用程序域来计算。
  • 不要使用散列代码如果您需要密码强大的
    散列值,则可以使用加密散列函数返回的值。对于加密散列,请使用从
    System.Security.Cryptography.HashAlgorithm或
    System.Security.Cryptography.KeyedHashAlgorithm类派生的类。


  • 不要测试散列码的相等性,以确定两个对象是否相等。 (不相等的对象可以有相同的散列码。)要
    测试是否相等,请调用ReferenceEquals或Equals方法。




  • 项目符号4是主要问题。不能保证哈希是不可逆的。使用的散列函数是一个实现细节,而不是像SHA这样的安全加密函数。



    我会使用这样一个函数:

     函数Get-StringHash {
    [CmdletBinding()]

    param(
    [Parameter(Mandatory = $
    $ String $ $ $ b $ String
    [ValidationSet('SHA1'),
    [Parameter(Position = 1)]
    [ ,'MD5','SHA256','SHA384','SHA512')]
    [String]
    $ HashName ='SHA256'


    process {
    $ StringBuilder = [System.Text.StringBuilder] :: new(128)
    [System.Security.Cryptography.HashAlgorithm] :: Create($ HashName).ComputeHash([System.Text.Encoding] :: UTF8.GetBytes($ String))| ForEach-Object {
    [Void] $ StringBuilder.Append($ _。ToString(x2))
    }
    $ StringBuilder.ToString()
    }
    }
    $ b $ csv = Import-Csv .\results.csv -delimiter',' - 标题栏1,栏2,栏3,栏4,栏5
    foreach($ csv中的$行){
    $ line.column3 = Get-StringHash $ line.column3
    }
    $ csv | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter','

    我相信我基于函数关闭这一个,但已经有一段时间了,因为我已经写下它。



    由LotPings编辑显示哈希结果

     column1,column2,column3,column4,column5
    20171108,120909470,0cdd3c3acdb7cfa107286565c044c5a0f1e58268f6f10e7e3415ff84942e577d,12,101
    20171109,122715740,0a7fb9f6bb7a180f2fd9429b0fbd1e7b0a83597b6a64aa6a123cef3e84700fe3,11,101


    I have a CSV file (Comma Separated Values) The file looks like this:

    20171108,120909470,SO1244,12,101 
    20171109,122715740,AG415757,11,101
    

    I need to obscure the data in (for example) columns 3 and, without affecting any of the other entries in the file.

    I want to do this using a hashing algorithm like SHA1 or MD5, so that the same strings will resove to the same hash values anywhere they are encountered.

    I need to send data to a third party, and certain columns contain sensitive information (e.g. customer names). I need the file to be complete, and where a string is replaced, I need it to be done in the same way every time it is encountered (so that any mapping or grouping remains). It does not need military encryption, just to be difficult to reverse. As I need to to this intermittently, a scripted solution would be ideal.

    What is the easiest way to achieve this using a command line tool or script?

    By preference, I would like a PowerShell script, since that does not require any additional software to achieve...

    This question seems like a duplicate of I need to hash (obfuscate) a column of data in a CSV file. Script preferred but the proposed solution didn't resolve my problem and throws the following error

    You cannot call a method on a null-valued expression.
    At C:\Users\mey\Hashr.ps1:4 char:5
    +     $_.column3 = $_.column3.gethashcode()
    

    The script is the following

    (Import-Csv .\results.csv -delimiter ',' ) | ForEach-Object{ 
     $_.column3 = $_.column3.gethashcode()
     $_
    } | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter ','
    

    Update:

    Here's the program i am running and that has been proposed by @BaconBits:

        param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [String[]]
        $String,
        [Parameter(Position = 1)]
        [ValidateSet('SHA1', 'MD5', 'SHA256', 'SHA384', 'SHA512')]
        [String]
        $HashName = 'SHA256'
    )
    
    process {
        $StringBuilder = [System.Text.StringBuilder]::new(128)
        [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | ForEach-Object { 
            [Void]$StringBuilder.Append($_.ToString("x2")) 
        } 
        $StringBuilder.ToString() 
    }
    }
    $csv = Import-Csv .\results.csv -delimiter ',' 
    foreach ($line in $csv) {
    $line.column1 = Get-StringHash $line.column1
    }
    $csv | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter ','
    

    The csv file i am importing is an output from another java program i made and it creates no header, it just fill the csv file with values

    I am getting this error Get-StringHash : Cannot bind argument to parameter 'String' because it is null.

    解决方案

    Based on the doc, you're not going to want to use GetHashCode() this way:

    A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value. For this reason:

    • Do not serialize hash code values or store them in databases.

    • Do not use the hash code as the key to retrieve an object from a keyed collection.

    • Do not send hash codes across application domains or processes. In some cases, hash codes may be computed on a per-process or per-application domain basis.

    • Do not use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.

    • Do not test for equality of hash codes to determine whether two objects are equal. (Unequal objects can have identical hash codes.) To test for equality, call the ReferenceEquals or Equals method.

    Bullet point 4 is the main problem. There's no guarantee that the hashing isn't reversible. The hashing function used is an implementation detail, not a secure cryptographic function like SHA.

    I'd use a function like this one:

    function Get-StringHash { 
        [CmdletBinding()]
    
        param (
            [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
            [String[]]
            $String,
            [Parameter(Position = 1)]
            [ValidateSet('SHA1', 'MD5', 'SHA256', 'SHA384', 'SHA512')]
            [String]
            $HashName = 'SHA256'
        )
    
        process {
            $StringBuilder = [System.Text.StringBuilder]::new(128)
            [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | ForEach-Object { 
                [Void]$StringBuilder.Append($_.ToString("x2")) 
            } 
            $StringBuilder.ToString() 
        }
    }
    
    $csv = Import-Csv .\results.csv -delimiter ',' -Header column1,column2,column3,column4,column5
    foreach ($line in $csv) {
        $line.column3 = Get-StringHash $line.column3
    }
    $csv | Export-Csv .\myobfuscated.csv -NoTypeInformation -delimiter ','
    

    I believe I based that function off of this one, but it's been awhile since I've written it.

    Edit by LotPings to show results of hash

    "column1","column2","column3","column4","column5"
    "20171108","120909470","0cdd3c3acdb7cfa107286565c044c5a0f1e58268f6f10e7e3415ff84942e577d","12","101 "
    "20171109","122715740","0a7fb9f6bb7a180f2fd9429b0fbd1e7b0a83597b6a64aa6a123cef3e84700fe3","11","101"
    

    这篇关于Powershell脚本的散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆