Skip to content

Commit 639ad4a

Browse files
committed
update to version 1.1
1 parent 4d01715 commit 639ad4a

12 files changed

Lines changed: 290 additions & 212 deletions

File tree

clkeys

Lines changed: 211 additions & 135 deletions
Large diffs are not rendered by default.

clkeys.sig

0 Bytes
Binary file not shown.

clsha1

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
* File: clsha1
5-
* Version : 1.0
5+
* Version : 1.1
66
* License : BSD-3-Clause
77
*
88
*
@@ -39,37 +39,36 @@
3939
import sys, os
4040
from base64 import *
4141
from binascii import *
42+
4243
try:
4344
from cryptlib_py import *
4445
except:
4546
ERR_IMPORT = """
46-
The python3 library is not installed. You need to install
47-
the packages cryptlib-python3 and cryptlib.
48-
You will find them for a variety of operating systems here:
47+
The python3 library is not installed. You need to install the packages cryptlib-python3 and cryptlib.
48+
You will find them for a variety of operating systems here:
4949
https://senderek.ie/cryptlib
5050
or in the Fedora repository.
5151
"""
5252
print(ERR_IMPORT)
53-
exit (5)
53+
exit (-1)
5454

55-
Version = "clsha1 version 1.0"
55+
Version = "clsha1 version 1.1"
5656
HashSize = 20
5757
OK = 0
5858
ERR_NOBYTES = 1
5959
ERR_PERM = 2
6060
MaxBytes = 500000000
6161
Source = "file"
6262
Mode = "hex"
63-
Text = ""
64-
InputBytes = ""
63+
InputBytes = bytearray()
6564
# we try to read all bytes in a single pass
6665
ReadMoreBytes = False
6766
FileName = ""
6867

6968
def print_help():
7069
Help = """
71-
SHA1 hashvalues of files or standard input
72-
70+
SHA1 hash values of files or standard input
71+
7372
usage: clsha1 [OPTION] [FILE]
7473
7574
No FILE or "-" reads from standard input.
@@ -79,12 +78,12 @@ Options are:
7978
--version prints the version
8079
--base64 prints the hashvalue in base64 encoding instead of hex
8180
82-
Full documentation <https://senderek.ie/cryptlib/tools/clsha1>
81+
Full documentation <https://senderek.ie/cryptlib/tools/clsha1>
8382
8483
This program depends on two packages providing the cryptlib shared object
8584
library and the python3-bindings to this library.
8685
87-
You can download both packages in RPM format at
86+
You can download both packages in RPM format at
8887
https://senderek.ie/cryptlib/downloads
8988
9089
Or in FEDORA you can install the packages cryptlib and cryptlib-python3
@@ -103,7 +102,7 @@ if len(sys.argv) > 1 :
103102
if sys.argv[1] == "--version":
104103
print ( Version )
105104
exit( OK )
106-
105+
107106
if sys.argv[1] == "--base64" or sys.argv[1] == "-b64" :
108107
Mode = "base64"
109108
try:
@@ -115,14 +114,17 @@ if len(sys.argv) > 1 :
115114
except:
116115
pass
117116

117+
# all input is read as bytes
118118
if len(sys.argv) > 1 :
119119
if sys.argv[1] == "-":
120120
try:
121-
Text = sys.stdin.read( MaxBytes )
121+
InputBytes = sys.stdin.buffer.read( MaxBytes )
122122
Source = "stdin"
123123
FileName = "-"
124124
except:
125+
print("Error: reading from stdin")
125126
exit ( ERR_PERM )
127+
126128
elif os.path.isfile(sys.argv[1]):
127129
FileName = sys.argv[1]
128130
try:
@@ -133,12 +135,12 @@ if len(sys.argv) > 1 :
133135
exit ( ERR_PERM )
134136
else:
135137
try:
136-
Text = sys.stdin.read( MaxBytes )
138+
InputBytes = sys.stdin.buffer.read( MaxBytes )
137139
Source = "stdin"
138140
FileName = "-"
139141
except:
140-
pass
141-
142+
print("Error: reading from stdin")
143+
exit ( ERR_PERM )
142144

143145
##### Begin Cryptlib code #####
144146

@@ -152,23 +154,20 @@ namesize = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_NAME_ALGO, Algo )
152154

153155
# hashedData must be modifiable Buffer
154156
hashedData = bytearray()
155-
if Text:
156-
hashedData.extend( Text.encode() )
157-
else:
158-
hashedData.extend( InputBytes )
157+
hashedData.extend( InputBytes )
159158

160159
nullData = bytearray( b'' )
161160

162161
if len(hashedData) < MaxBytes:
163162
# we only need one pass, no looping required
164163
cryptEncrypt( hashContext, hashedData )
165-
164+
166165
else:
167166
# use all bytes that have been read already
168167
cryptEncrypt( hashContext, hashedData )
169168
# there are more bytes to be read
170169
ReadMoreBytes = True
171-
while ReadMoreBytes:
170+
while ReadMoreBytes:
172171
# start with empty bytearray
173172
del( hashedData )
174173
hashedData = bytearray()
@@ -180,13 +179,13 @@ else:
180179
# no more input
181180
F.close()
182181
else:
183-
Text = sys.stdin.read( MaxBytes )
184-
hashedData.extend( Text.encode() )
185-
if len( Text ) < MaxBytes:
182+
InputBytes = sys.stdin.buffer.read( MaxBytes )
183+
hashedData.extend( InputBytes )
184+
if len( InputBytes ) < MaxBytes:
186185
ReadMoreBytes = False
187186
cryptEncrypt( hashContext, hashedData )
188187

189-
if hashedData:
188+
if hashedData:
190189
# end the operation with null bytes input
191190
if hashedData:
192191
cryptEncrypt( hashContext, nullData )
@@ -195,17 +194,16 @@ if hashedData:
195194

196195
# the hashvalue needs a minimum of 20 Bytes
197196
hashvalue = bytearray( b' '*HashSize )
198-
num = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_HASHVALUE, hashvalue )
197+
num = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_HASHVALUE, hashvalue )
199198

200199
# output
201200
if num == HashSize:
202201
if Mode == "base64":
203202
print ( b64encode( hashvalue ).decode() + " " + FileName )
204-
else:
203+
else:
205204
print( hexlify( hashvalue ).decode() + " " + FileName )
206205

207206
cryptDestroyContext( hashContext )
208207

209208
cryptEnd()
210209
sys.exit( OK )
211-

clsha1.sig

0 Bytes
Binary file not shown.

clsha2

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
* File: clsha2
5-
* Version : 1.0
5+
* Version : 1.1
66
* License : BSD-3-Clause
77
*
88
*
@@ -44,35 +44,32 @@ try:
4444
from cryptlib_py import *
4545
except:
4646
ERR_IMPORT = """
47-
The python3 library is not installed. You need to install
48-
the packages cryptlib-python3 and cryptlib.
47+
The python3 library is not installed. You need to install the packages cryptlib-python3 and cryptlib.
4948
You will find them for a variety of operating systems here:
5049
https://senderek.ie/cryptlib
5150
or in the Fedora repository.
5251
"""
5352
print(ERR_IMPORT)
54-
exit (5)
53+
exit (-1)
5554

56-
57-
Version = "clsha2 version 1.0"
55+
Version = "clsha2 version 1.1"
5856
HashSize = 32
5957
OK = 0
6058
ERR_NOBYTES = 1
6159
ERR_PERM = 2
6260
MaxBytes = 500000000
6361
Source = "file"
6462
Mode = "hex"
65-
Text = ""
66-
InputBytes = ""
63+
InputBytes = bytearray()
6764
# we try to read all bytes in a single pass
6865
ReadMoreBytes = False
6966
FileName = ""
7067

7168
def print_help():
7269
Help = """
73-
SHA2 hashvalues of files or standard input
74-
75-
usage: clsha1 [OPTION] [FILE]
70+
SHA2 hash values of files or standard input
71+
72+
usage: clsha2 [OPTION] [FILE]
7673
7774
No FILE or "-" reads from standard input.
7875
@@ -81,12 +78,12 @@ Options are:
8178
--version prints the version
8279
--base64 prints the hashvalue in base64 encoding instead of hex
8380
84-
Full documentation <https://senderek.ie/cryptlib/tools/clsha2>
81+
Full documentation <https://senderek.ie/cryptlib/tools/clsha2>
8582
8683
This program depends on two packages providing the cryptlib shared object
8784
library and the python3-bindings to this library.
8885
89-
You can download both packages in RPM format at
86+
You can download both packages in RPM format at
9087
https://senderek.ie/cryptlib/downloads
9188
9289
Or in FEDORA you can install the packages cryptlib and cryptlib-python3
@@ -105,7 +102,7 @@ if len(sys.argv) > 1 :
105102
if sys.argv[1] == "--version":
106103
print ( Version )
107104
exit( OK )
108-
105+
109106
if sys.argv[1] == "--base64" or sys.argv[1] == "-b64" :
110107
Mode = "base64"
111108
try:
@@ -117,14 +114,17 @@ if len(sys.argv) > 1 :
117114
except:
118115
pass
119116

117+
# all input is read as bytes
120118
if len(sys.argv) > 1 :
121119
if sys.argv[1] == "-":
122120
try:
123-
Text = sys.stdin.read( MaxBytes )
121+
InputBytes = sys.stdin.buffer.read( MaxBytes )
124122
Source = "stdin"
125123
FileName = "-"
126124
except:
125+
print("Error: reading from stdin")
127126
exit ( ERR_PERM )
127+
128128
elif os.path.isfile(sys.argv[1]):
129129
FileName = sys.argv[1]
130130
try:
@@ -135,12 +135,12 @@ if len(sys.argv) > 1 :
135135
exit ( ERR_PERM )
136136
else:
137137
try:
138-
Text = sys.stdin.read( MaxBytes )
138+
InputBytes = sys.stdin.buffer.read( MaxBytes )
139139
Source = "stdin"
140140
FileName = "-"
141141
except:
142-
pass
143-
142+
print("Error: reading from stdin")
143+
exit ( ERR_PERM )
144144

145145
##### Begin Cryptlib code #####
146146

@@ -154,23 +154,20 @@ namesize = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_NAME_ALGO, Algo )
154154

155155
# hashedData must be modifiable Buffer
156156
hashedData = bytearray()
157-
if Text:
158-
hashedData.extend( Text.encode() )
159-
else:
160-
hashedData.extend( InputBytes )
157+
hashedData.extend( InputBytes )
161158

162159
nullData = bytearray( b'' )
163160

164161
if len(hashedData) < MaxBytes:
165162
# we only need one pass, no looping required
166163
cryptEncrypt( hashContext, hashedData )
167-
164+
168165
else:
169166
# use all bytes that have been read already
170167
cryptEncrypt( hashContext, hashedData )
171168
# there are more bytes to be read
172169
ReadMoreBytes = True
173-
while ReadMoreBytes:
170+
while ReadMoreBytes:
174171
# start with empty bytearray
175172
del( hashedData )
176173
hashedData = bytearray()
@@ -182,13 +179,13 @@ else:
182179
# no more input
183180
F.close()
184181
else:
185-
Text = sys.stdin.read( MaxBytes )
186-
hashedData.extend( Text.encode() )
187-
if len( Text ) < MaxBytes:
182+
InputBytes = sys.stdin.buffer.read( MaxBytes )
183+
hashedData.extend( InputBytes )
184+
if len( InputBytes ) < MaxBytes:
188185
ReadMoreBytes = False
189186
cryptEncrypt( hashContext, hashedData )
190187

191-
if hashedData:
188+
if hashedData:
192189
# end the operation with null bytes input
193190
if hashedData:
194191
cryptEncrypt( hashContext, nullData )
@@ -197,17 +194,16 @@ if hashedData:
197194

198195
# the hashvalue needs a minimum of 32 Bytes
199196
hashvalue = bytearray( b' '*HashSize )
200-
num = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_HASHVALUE, hashvalue )
197+
num = cryptGetAttributeString( hashContext, CRYPT_CTXINFO_HASHVALUE, hashvalue )
201198

202199
# output
203200
if num == HashSize:
204201
if Mode == "base64":
205202
print ( b64encode( hashvalue ).decode() + " " + FileName )
206-
else:
203+
else:
207204
print( hexlify( hashvalue ).decode() + " " + FileName )
208205

209206
cryptDestroyContext( hashContext )
210207

211208
cryptEnd()
212209
sys.exit( OK )
213-

clsha2.sig

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)