開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の8章(スタティックライブラリとダイナミックライブラリ: ホットスワップ可能なコード)、makeマグネット(p.361)をpythonで考えてみる。
makeマグネット(p.361)
コード(BBEdit, Emacs)
hfsecurity.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def encrypt(b): for i in range(len(b)): b[i] = b[i] ^ 31 def checksum(s): c = 0 b = s.encode('utf-8') for n in b: c += c ^ n return c
bank_vault.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import hfsecurity import sys for path in sys.path: print(path) s = 'Speak friend and enter' b = bytearray(s.encode('utf-8')) hfsecurity.encrypt(b) s = bytes(b).decode('utf-8') print('{0}に暗号化しました。'.format(s)) print('チェックサムは{0}です。'.format(hfsecurity.checksum(s))) b = bytearray(s.encode('utf-8')) hfsecurity.encrypt(b) s = bytes(b).decode('utf-8') print('{0}に復号化しました。'.format(s)) print('チェックサムは{0}です。'.format(hfsecurity.checksum(s)))
入出力結果(Terminal, IPython)
$ ./bank_vault.py /Users/kamimura/Documents/py/hfc/ch8/hfsecurity /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Scrapy-0.22.2-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/cssselect-0.9.1-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pyOpenSSL-0.14-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/lxml-3.3.5-py3.4-macosx-10.9-x86_64.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/queuelib-1.1.1-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/w3lib-1.5-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/Twisted-14.0.0-py3.4.egg /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4 /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/readline /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyObjC Loz~t?ymvzq{?~q{?zqkzmに暗号化しました。 チェックサムは89561741です。 Speak friend and enterに復号化しました。 チェックサムは89548156です。 $
0 コメント:
コメントを投稿