将Python循环转换为PowerShell

时间:2017-08-26 02:25:22

标签: python powershell

我正在尝试将Python脚本转换为PowerShell,并且我已经开始了解Python脚本中循环中的内容。以下是Python脚本的示例。

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
 RewriteRule ^(.*) http://newdomain.com.ar/?param=data&uparam2=data

到目前为止,这是我为PowerShell转换提出的,但我没有任何Python经验,并且已经了解了循环中发生的事情。

import time

def obfuscateApiKey () :
    seed = 'f1avad34567a'
    now = str(long(time.time() * 1000))
    n = now[-6:]
    r = str(int(n) >> 1).zfill(6)
    key = ""
    for i in range(0, len(n), 1):
        key += seed[int(n[i])]
    for j in range(0, len(r), 1):
        key += seed[int(r[j])+2]

    print "Timestamp:%s     Key:%s" % (now, key)

obfuscateApiKey()

任何人都有关于如何在PowerShell中执行此部分的提示?

$seed = 'f1avad34567a'
$Now = ([int](get-date -UFormat %s) *1000).ToString()
$n = $now.Substring( [math]::Max( 0, $now.Length – 6 ) )
$r = $n -shr 1
$key = @()

1 个答案:

答案 0 :(得分:1)

这如何模糊密钥?它不需要参数并且取决于当前时间,因此它看起来是不可逆的且不可重复的。如果唯一的东西是“可验证的” - 即其他人可以读取打印的时间戳,知道种子,并检查生成的密钥。如果这是真的,那么说它运行一次并且用固定的打印语句'Timestamp:1503715652844 Key:3da5aada53aa'替换该函数是非常诱人的,这将永远有效。

原件:

def obfuscateApiKey():
    seed = 'f1avad34567a'               # string
    now = str(long(time.time() * 1000)) # current timestamp with
                                        #   milliseconds rolled in, as string
    n = now[-6:]                        # last 6 chars
    r = str(int(n) >> 1).zfill(6)       # right bit shift 1 and
                                        #   left pad to len 6 with zeros

    key = ""                            # empty string

    for i in range(0, len(n), 1):       # 0,1,2,3,4,5 (because n is len 6)
        key += seed[int(n[i])]          # string index in n, lookup in seed

    for j in range(0, len(r), 1):       # 0,1,2,3,4,5 again (?), r is len 6
        key += seed[int(r[j])+2]        # string index in r, lookup in seed

    print "Timestamp:%s     Key:%s" % (now, key)    # print

的PowerShell:

$seed = 'f1avad34567a'

# Bit of a mess, to handle get-date returning local time
# but Unix timestamps needing to be in UTC timezone
$now = [string][math]::Floor([double](Get-Date -Date (Get-Date).ToUniversalTime() -UFormat %s) * 1000)

# Your substring line
$n = $now.Substring([math]::Max(0, $now.Length–6))

# Your shift-right, but $n was a string, so convert to [int]
# and PadLeft is the equivalent of zfill
$r = "$([int]$n -shr 1)".PadLeft(6, '0')

# string concatenation works in PS, for small uses it's fine.
$key = ''

# The loops and indexing almost translate. I've made them fixed
# ranges because it seems the lengths of the strings are fixed.
# The casting is awkward, but the result of looking up a character
# from a string is type [char] and they cast to int as the 
# ascii/unicode codepoint value, not the numeric value of the content.
0..5 | ForEach-Object {
    $key += $seed[[int][string]$n[$_]]
}

0..5 | ForEach-Object {
    $key += $seed[[int][string]$r[$_]+2]
}

'Timestamp:{0}    Key:{1}' -f $now, $key