-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlate
executable file
·178 lines (167 loc) · 4.98 KB
/
xlate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/local/bin/python
# Binary translator
# Copyright 2014 smaptas
# This is a command-line version of Paul Schou's TRANSLATOR, BINARY page at
# http://paulschou.com/tools/xlate/
import argparse
import sys
import fileinput
from Crypto.Hash import MD2
from Crypto.Hash import MD4
from Crypto.Hash import MD5
from Crypto.Hash import SHA
from Crypto.Hash import SHA224
from Crypto.Hash import SHA256
from Crypto.Hash import SHA384
from Crypto.Hash import SHA512
from Crypto.Hash import RIPEMD
import binascii
formats = ['all', 'text', 'binary', 'oct', 'hex', 'base32', 'base64', 'ascii85',
'char', 'info']
def text2binary(lines):
outLines = []
for S in lines:
outStr = ''
for c in S:
outStr += '{0:08b}'.format(ord(c)) + ' '
outLines.append(outStr[:-1])
return '\n'.join(outLines)
def binary2text(lines):
outLines = []
for S in lines:
outStr = ''
for i in range(0, len(S), 8):
outStr += chr(int(S[i:i+8], 2))
outLines.append(outStr)
return '\n'.join(outLines)
def text2base64(lines):
str64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
outLines = []
for S in lines:
binStr = ''
for c in S:
binStr += '{0:08b}'.format(ord(c))
endStr = ['', '==' , '='][len(binStr) % 6 / 2]
binStr += '0' * 2 * len(endStr)
outStr = ''
for i in range(0, len(binStr), 6):
outStr += str64[int(binStr[i:i+6], 2)]
outLines.append(outStr + endStr)
return '\n'.join(outLines)
def base64_to_text(lines):
str64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
outLines = []
for S in lines:
binStr = ''
pad = ''
for i in range(len(S)):
c = S[i]
if c == '=':
pad = binStr[-2:] + pad
binStr = binStr[:-2]
else:
binStr += '{0:06b}'.format(str64.find(c))
#for c in S:
#if c == '=':
#print "PAD:", binStr[-2:]
#binStr = binStr[:-2]
#else:
#binStr += '{0:06b}'.format(str64.find(c))
if pad == '':
#print "PAD: 00"
pass
else:
print "PAD:", pad
outStr = ''
for i in range(0, len(binStr), 8):
outStr += chr(int(binStr[i:i+8], 2))
outLines.append(outStr)
return '\n'.join(outLines)
def textinfo(lineList):
'''
Other hashes planned:
ripemd128
ripemd256
ripemd320
whirlpool
tiger128,3
tiger160,3
tiger192,3
tiger128,4
tiger160,4
tiger192,4
snefru
snefru256
gost
adler32
crc32
crc32b
salsa10
salsa20
haval128,3
haval160,3
haval192,3
haval224,3
haval256,3
haval128,4
haval160,4
haval192,4
haval224,4
haval256,4
haval128,5
haval160,5
haval192,5
haval224,5
haval256,5
'''
numLines = len(lineList)
numWords = 0
numBytes = 0
hashes = [('md2', MD2.new()),
('md4', MD4.new()),
('md5', MD5.new()),
('sha1', SHA.new()),
('sha224', SHA224.new()),
('sha256', SHA256.new()),
('sha384', SHA384.new()),
('sha512', SHA512.new()),
('ripemd160', RIPEMD.new())]
crc32 = binascii.crc32('')
for line in lineList:
numWords += len(line.strip().split())
numBytes += len(line)
for h in hashes:
h[1].update(line)
crc32 = binascii.crc32(line, crc32) & 0xffffffff
outStr = ''
outStr += 'lines: {} words: {} bytes: {}\n'.format(numLines, numWords,
numBytes)
for h in hashes:
outStr += '{}: {}\n'.format(h[0], h[1].hexdigest())
outStr += 'crc32: {0:x}'.format(crc32)
return outStr
def main():
parser = argparse.ArgumentParser()
parser.add_argument('inFormat', choices=formats[1:-1],
help='Input formats: ' + str(formats[1:-1]))
parser.add_argument('outFormat', choices=formats,
help='Output format: ' + str(formats))
parser.add_argument('input', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
args = parser.parse_args()
lines = map(lambda x: x.strip(), args.input.readlines())
if args.inFormat == 'text' and args.outFormat == 'info':
print textinfo(lines)
if args.inFormat == 'text' and args.outFormat == 'binary':
print text2binary(lines)
if args.inFormat == 'text' and args.outFormat == 'base64':
print text2base64(lines)
if args.inFormat == 'base64' and args.outFormat == 'text':
print base64_to_text(lines)
if args.inFormat == 'binary' and args.outFormat == 'text':
print binary2text(lines)
if args.outFormat == 'all':
for x in formats[1:]:
convert(args.input, args.inFormat, x)
if __name__ == '__main__':
main()