Python3(簡単な処理メモ)

2つのリストから辞書を作成

list1 = ['a','b','c']
list2 = [1,2,3]

dic = dict(zip(list1,list2))
print(dic)
# {'a': 1, 'b': 2, 'c': 3}

文字列から文字コードに変換、文字コードから文字列に変換

codeData = ord('a')
print(codeData)
# 97

strData = chr(codeData)
print(strData)
# a

bytesからintに変換

inputData = b'\x01\x02\x03\x04'

intData = int.from_bytes(inputData,'big')
print("{:08X}".format(intData))
# 01020304

intData = int.from_bytes(inputData,'little')
print("{:08X}".format(intData))
# 04030201

intからbytesに変換

inputData = 0x01020304

bytesData = inputData.to_bytes(4,'big')
print(bytesData)
# b'\x01\x02\x03\x04'

bytesData = inputData.to_bytes(4,'little')
print(bytesData)
# b'\x04\x03\x02\x01'

Base64エンコード・デコード

import base64

inputData = b'Hello World!'
encoded = base64.b64encode(inputData)
print(encoded)
# b'SGVsbG8gV29ybGQh'

decoded = base64.b64decode(encoded)
print(decoded)
# b'Hello World!'

変形Base64エンコード・デコード

異なるBase64テーブル(大文字⇔小文字)を使用

import base64

base64Table   = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
mybase64Table = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/'
x = str.maketrans(mybase64Table, base64Table)

inputData = b'Hello World!'
encoded = base64.b64encode(inputData)
encoded = encoded.decode('utf-8').translate(x)
print(encoded.encode('utf-8'))
# b'sgvSBg8Gv29YBgqH'

decoded = base64.b64decode(encoded.translate(x))
print(decoded)
# b'Hello World!'

RC4暗号化・復号

from Crypto.Cipher import ARC4

inputData = b'Hello World!'

key = 'password'
cipher = ARC4.new(key)
enc = cipher.encrypt(inputData)
print(enc)
# b'\xb7\x90Tb#\x1e\xe3\xc1\x13=\xcf]'

cipher = ARC4.new(key)
dec = cipher.decrypt(enc)
print(dec)
# b'Hello World!'

RC4暗号化・復号(PyCrypto無し)

def rc4Init(key):
    index1 = index2 = 0
    S = bytearray(range(256))
    for i in range(256):
        index2 = (key[index1]+S[i]+index2)&0xFF
        S[i],S[index2] = S[index2],S[i]
        index1 += 1
        if(index1 == len(key)):
            index1 = 0
    return S

def rc4(buffer,S):
    x = y = 0
    for i in range(len(buffer)):
        x = (x+1)&0xFF
        y = (S[x]+y)&0xFF
        S[x],S[y] = S[y],S[x]
        buffer[i] ^= S[(S[x]+S[y])&0xFF]
    return bytes(buffer)

def rc4Full(buffer,key):
    S = rc4Init(key)
    out = rc4(buffer,S)
    return out

inputData = b'Hello World!'
key = b'password'

buffer = bytearray(inputData)
enc = rc4Full(buffer,key)
print(enc)
# b'\xb7\x90Tb#\x1e\xe3\xc1\x13=\xcf]'

buffer = bytearray(enc)
dec = rc4Full(buffer,key)
print(dec)
# b'Hello World!'

AES-CBC暗号化・復号

from Crypto.Cipher import AES
from Crypto import Random

BS = AES.block_size
key = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
iv = Random.new().read(BS)
MODE = AES.MODE_CBC

def addPad(raw):
    padding = BS-len(raw)%BS
    return raw + padding.to_bytes(1,'big')*padding

def delPad(raw):
    padsize = raw[-1]
    return raw[:-padsize]

def encryptData(raw):
    temp = addPad(raw)
    cipher = AES.new(key,MODE,iv)
    return cipher.encrypt(temp)

def decryptData(raw):
    cipher = AES.new(key, MODE, iv)
    dec = cipher.decrypt(raw)
    return delPad(dec)

inputData = b'Hello World!'
enc = encryptData(inputData)

dec = decryptData(enc)
print(dec)
# b'Hello World!'

HexTextからバイナリに変換

import binascii

hexText = '0123456789ABCDEF'
binData = binascii.unhexlify(hexText)
print(binData)
# b'\x01#Eg\x89\xab\xcd\xef'

バイナリからHexTextに変換

import binascii

binData = b'\x00\x01\x02\x03'
hexText = binascii.hexlify(binData)
print(hexText.decode('utf-8'))
# 00010203

バイナリを逆順にする

binData = b'\x00\x01\x02\x03'
revData = binData[::-1]
print(revData)
# b'\x03\x02\x01\x00'

GZIPでデータを圧縮・展開

import gzip

binData = b'abcdefg'
packed = gzip.compress(binData,9)
print(packed)
# b'\x1f\x8b\x08\x00\x82`\x0bX\x02\xffKLJNIMK\x07\x00\xa6j*1\x07\x00\x00\x00'

unpacked = gzip.decompress(packed)
print(unpacked)
# b'abcdefg'

http://docs.python.jp/3/library/gzip.html#gzip.GzipFile
引数 compresslevel は 0 から 9 の整数を取り、圧縮レベルを制御します;
1 は最も高速で最小限の圧縮を行い、9 は最も低速ですが最大限の圧縮を行います。
0 は圧縮しません。デフォルトは 9 です。

GZIPでファイルに圧縮・ファイルから展開

import gzip

inputData = b'abcdefghijklmnopqrstuvwxyz'
with gzip.open('packed.gz','wb') as f:
    f.write(inputData)

with gzip.open('packed.gz','rb') as g:
    unpacked = g.read()
print(unpacked)
# b'abcdefghijklmnopqrstuvwxyz'
$ xxd -g 1 packed.gz 
0000000: 1f 8b 08 08 f3 63 0b 58 02 ff 70 61 63 6b 65 64  .....c.X..packed
0000010: 00 4b 4c 4a 4e 49 4d 4b cf c8 cc ca ce c9 cd cb  .KLJNIMK........
0000020: 2f 28 2c 2a 2e 29 2d 2b af a8 ac 02 00 bd 50 27  /(,*.)-+......P'
0000030: 4c 1a 00 00 00                                   L....

GZIPマジックナンバーは「1F 8B」

JSONを扱う

import json

jsonStr = '{"data":[{"name":"taro","age":20},{"name":"jiro","age":18}]}'
jsonData = json.loads(jsonStr)

print(json.dumps(jsonData,indent=2))
# {
#  "data": [
#    {
#      "name": "taro",
#      "age": 20
#    },
#    {
#      "name": "jiro",
#      "age": 18
#    }
#  ]
# }

JSONファイルを扱う

import json

with open('jsonFile.json','r') as f:
    jsonData = json.load(f)

print(json.dumps(jsonData,indent=2))
# {
#  "data": [
#    {
#      "name": "taro",
#      "age": 20
#    },
#    {
#      "name": "jiro",
#      "age": 18
#    }
#  ]
# }

JSONを扱う2

import json

jsonStr = '{"data1":[{"name":"taro","age":20},{"name":"jiro","age":18}],"data2":[{"name":"hanako","age":19},{"name":"yoko","age":17}]}'
jsonData = json.loads(jsonStr)

print(jsonData["data1"][0]["name"])
# taro

for data in jsonData:
    for list in jsonData[data]:
        print("name: " + list["name"] + " age: " + str(list["age"]))
# name: taro age: 20
# name: jiro age: 18
# name: hanako age: 19
# name: yoko age: 17

ハッシュ値を計算する

import hashlib

inputData = b'hello'

md5 = hashlib.md5(inputData)
print(md5.hexdigest())
# 5d41402abc4b2a76b9719d911017c592

sha1 = hashlib.sha1(inputData)
print(sha1.hexdigest())
# aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

ハッシュ値を計算する2

from Crypto.Hash import MD5,SHA

inputData = b'hello'

md5 = MD5.new()
md5.update(inputData)
print(md5.hexdigest())
# 5d41402abc4b2a76b9719d911017c592

sha1 = SHA.new()
sha1.update(inputData)
print(sha1.hexdigest())
# aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

PBKDF2(HMACSHA1)

import hashlib

KS = 16

salt = b'\x01\x02\x03\x04\x05\x06\x07\x08'
password  = b'\x00\x00\x00\x00\x00\x00\x00\x00'

dk = hashlib.pbkdf2_hmac('sha1',password,salt,1000)
key = dk[:KS]

print(key)
# b';\x8f\x82\x03C\xb4\xb0\xd4PT\xffD\xda\xf4\xa6l'

LZNT1で圧縮・展開(RtlCompressBuffer,RtlDecompressBuffer)

# Windows上でのみ動作
import ctypes

def compressBuffer(data):
    size = len(data)
    compressed = ctypes.create_string_buffer(size * 13)
    workSize = ctypes.c_ulong(0)
    workFragSize = ctypes.c_ulong(0)
    ctypes.windll.ntdll.RtlGetCompressionWorkSpaceSize(2,ctypes.byref(workSize),ctypes.byref(workFragSize))

    workspace = ctypes.create_string_buffer(workSize.value)
    finalSize = ctypes.c_ulong(0)
    ctypes.windll.ntdll.RtlCompressBuffer(2, ctypes.c_char_p(data), size, compressed, size * 3 , 4096 , ctypes.byref(finalSize),workspace)
    return compressed[0:finalSize.value]

def decompressBuffer(data):
    size = len(data)
    uncompressed = ctypes.create_string_buffer(size * 40)
    finalSize = ctypes.c_ulong(0)
    ctypes.windll.ntdll.RtlDecompressBuffer(2, uncompressed, size * 3, ctypes.c_char_p(data), size, ctypes.byref(finalSize))
    return uncompressed[0:finalSize.value]

inputData = b'Hello World!'

compressed = compressBuffer(inputData)
print(compressed)
# b'\r\xb0\x00Hello Wo\x00rld!'

decompressed = decompressBuffer(compressed)
print(decompressed)
# b'Hello World!'