Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dibaotian committed Jan 22, 2021
1 parent 19f5694 commit 01c2ba8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#### 1 create dedicated length random vector
#### 2 calculate the crc32
#### 3 save to file

### create_crc_data.py
59 changes: 45 additions & 14 deletions create_crc_data.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,76 @@
import sys
import random



def getCrc32(filename):
with open(filename, 'rb') as f:
return crc32(f.read())&0xffffffff

def putCrc32(file_path, msg):
f=open(file_path,"w")
f.write(msg)
def putCrc32(file_name, data):
f=open(file_name,"w")
f.write(data)
f.close

# 通过random.randint(a, b)方法得到随机数字
def generate_random_str(length):
random_str=""
base_str="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789!@#$%^&*()"
n=len(base_str)-1
for i in range(int(length)):
random_str+=base_str[random.randint(0,n)]
return random_str

random_str=""
# base_str="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789!@#$%^&*"
base_str="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789"
print(type(base_str))
print ("base_str %s" % len(base_str))
n=len(base_str)-1
# n=61

for i in range(int(length)):
random_str+=base_str[random.randint(0,n)]

return random_str

if __name__ == '__main__':

if len(sys.argv) < 2:
print ("Usage: python create_crc_data.py <data_length>")
exit(-1)

if (False == os.path.isdir('data')):
os.mkdir('data')

data_length = sys.argv[1]
file_name = str(data_length) +'.dat'

putCrc32(file_name, generate_random_str(data_length))
#todo 参数检查

if (data_length.endswith('b') | data_length.endswith('B')):
data_len = data_length[0:-1]
lenth = int(data_len)
elif (data_length.endswith('k') | data_length.endswith('K')):
data_len = data_length[0:-1]
print (data_len)
lenth = int(data_len)*1024
elif (data_length.endswith('m') | data_length.endswith('M')):
data_len = data_length[0:-1]
lenth = int(data_len)*1024*1024
else:
lenth = int(data_length)

print ("data length is %s----%s byte" % (data_length, lenth))

# name the input file
file_name = "data/"+ str(data_length) +'.dat'
print("input_file_name %s" % file_name)

#reserve the random data file
putCrc32(file_name, generate_random_str(lenth))

# CRC32 calculation
checksum=getCrc32(file_name)

#name the output file
output_file = file_name.replace('.dat','_crc32.dat')

#reserve the crc32 result
putCrc32(output_file, hex(checksum))

print ("Reserve the crc data in %s" % output_file)
print ("Reserve crc data in %s" % output_file)

print ("Crc:",(hex(checksum)))

Expand Down

0 comments on commit 01c2ba8

Please sign in to comment.