Exemplo n.º 1
0
def MGF1(mgfSeed, maskLen):
    if maskLen > 2**32:
        print("maskLen too large")
        sys.exit()

    hLen = 20  #sha1 outputs 20 bytes
    T = bytearray()

    #0 to \ceil (maskLen / hLen) - 1
    #skip -1 because range does not include endpoint
    for counter in range(math.ceil(maskLen / hLen)):
        C = I2OSP(counter, 4)
        #T = T || Hash(mgfSeed || C)
        #T =
        mgfSeed_bytearray = fc.hex_to_bytearray(mgfSeed)
        T = T + fc.hex_to_bytearray(
            hashlib.sha1(mgfSeed_bytearray + C).hexdigest())

    #Output the leading maskLen octets of T as the octet string mask
    return str(binascii.hexlify(T[:maskLen]))[2:][:-1]
Exemplo n.º 2
0
def encode_key(p, q, e):
    version = 0
    n = p * q
    d = modinv(e, (p - 1) * (q - 1))
    exp1 = d % (p - 1)  #FEL
    exp2 = d % (q - 1)  #FEL
    coef = modinv(q, p)  #CORRECT

    pem = str(
        der_encode(version) + der_encode(n) + der_encode(e) + der_encode(d) +
        der_encode(p) + der_encode(q) + der_encode(exp1) + der_encode(exp2) +
        der_encode(coef))
    L = int(len(pem) / 2)
    seq = "30"
    if L > 127:
        seq = seq + "81" + fc.int2hex(L).zfill(
            2) if L < 256 else seq + "82" + fc.int2hex(L).zfill(4)
    else:
        seq = seq + fc.int2hex(L).zfill(2)
    print(seq)
    pem = base64.b64encode(fc.hex_to_bytearray(seq + pem))
    return str(pem)[2:][:len(pem)]
Exemplo n.º 3
0
def encode_key(p, q, e):
    V = ''.join(map(encode_integer_tlv, compute_values(p, q, e)))
    base64_key = base64.b64encode(fc.hex_to_bytearray(encode_sequence_tlv(V)))
    return str(base64_key)[2:][:-1]
Exemplo n.º 4
0
 def test_hex_to_bytearray(self):
     self.assertEqual(list(format_converter.hex_to_bytearray("000001f4")),
                      [0, 0, 1, 244])
Exemplo n.º 5
0
def f(x_i, y_i):
    x_bytearray = fc.hex_to_bytearray(x_i)
    y_bytearray = fc.hex_to_bytearray(y_i)
    return fc.hex_to_int(fc.hash_bytearray(x_bytearray + y_bytearray))
Exemplo n.º 6
0
def hash(mgfSeed, val):
    return hashlib.sha1(fc.hex_to_bytearray(mgfSeed) + val).hexdigest()
Exemplo n.º 7
0
def transform_func(hex):
    return fc.hash_bytearray(fc.hex_to_bytearray(hex))
Exemplo n.º 8
0
def f(x_i, y_i):
    #return fc.bytearray_to_int(fc.hash_bytearray(fc.hex_to_bytearray(x_i) + fc.hex_to_bytearray(y_i)))
    x_bytearray = fc.hex_to_bytearray(x_i)
    y_bytearray = fc.hex_to_bytearray(y_i)
    return fc.hex_to_int(fc.hash_bytearray(x_bytearray + y_bytearray))