def test(itt):
    for i in range(itt):
        size_src = random.randint(-10000, 10000)
        size_key = random.randint(-10000, 10000)
        st = random.randint(-10000, 10000)
        le = random.randint(-10000, 10000)
        try:
            src = rnd.get_rnd(size_src)
            key = rnd.get_rnd(size_key)

            h0 = ncp.hash_r(src, 16)

            ncp.encoder_bits(src, key, st, le)
            ncp.decoder_bits(src, key, st, le)

            h1 = ncp.hash_r(src, 16)
            if h0 == h1:
                print('[%4d] %8d %8d %8d %8d - test bits - ok' %
                      (i, size_src, size_key, st, le))
            else:
                print('[%4d] %8d %8d %8d %8d - test bits - hash !=' %
                      (i, size_src, size_key, st, le))
                return (1)
        except:
            print('[%4d] %8d %8d %8d %8d - test bits -error' %
                  (i, size_src, size_key, st, le))
            return (1)
    return (0)
Exemple #2
0
def encode(filename_in, filename_out, word):
    fi = None
    fo = None
    try:
        fi = open(filename_in, 'rb')
        fo = open(filename_out, 'wb')
    except:
        print('error open files')
        exit()

    # 2
    data = fi.read()
    data_out = b''
    size_data = len(data)
    size_key = 128
    key = ncp.hash_r(word.encode(), size_key)
    j = 0
    while j < size_data:
        k = j + size_key
        buf = data[j:k]
        ncp.encoder_bits(buf, key, 0, 0)
        ncp.reverse(buf, 0, 0)
        data_out = data_out + buf
        j += size_key

    # 1
    data = data_out
    data_out = b''
    size_data = len(data)
    size_key = 64
    key = ncp.hash_r(word.encode(), size_key)
    j = 0
    while j < size_data:
        k = j + size_key
        buf = data[j:k]
        ncp.encoder_bits(buf, key, 0, 0)
        ncp.reverse(buf, 0, 0)
        data_out = data_out + buf
        j += size_key

    fo.write(data_out)
    fi.close()
    fo.close()
Exemple #3
0
#!/usr/bin/env python3
# test crypt.so

import sys
import ncp
import prn

# ---------------------------------------------------------------------------- #
# parse arguments
src = ''
size = 0

try:
    src = sys.argv[1]
    size = int(sys.argv[2])
except:
    print('Usage: <string> <size>')
    exit()

print('src  = ', src)
print('size = ', size)
src = src.encode('utf-8')

# ---------------------------------------------------------------------------- #
# hash_r
h = ncp.hash_r(src, size)
prn.print_bytes(h, 16)


Exemple #4
0
def hash(filename):
    f = open(filename, 'rb')
    data = f.read()
    prn.print_bytes_hex(ncp.hash_r(data, 16))
    f.close()