コード例 #1
0
 def pack(self):
     return bytes(
         integer_to_bytes(self.duration, 2) +
         integer_to_bytes(self.tracksOffset, 2) +
         integer_to_bytes(self.trackCount, 2) +
         integer_to_bytes(self.animationEvent, 1) +
         integer_to_bytes(self.specialColorType, 1))
コード例 #2
0
ファイル: pixels.py プロジェクト: tomas-dots/Pixels
 def light_up_face(self,
                   face,
                   color: Color32,
                   remapFace=0,
                   layoutIndex=255,
                   remapRot=255):
     data = []
     data.extend(integer_to_bytes(face, 1))
     data.extend(integer_to_bytes(remapFace, 1))
     data.extend(integer_to_bytes(layoutIndex, 1))
     data.extend(integer_to_bytes(remapRot, 1))
     data.extend(integer_to_bytes(color.to_rgb(), 4))
     self._send(MessageType.LightUpFace, *data)
コード例 #3
0
 def confirm_call(self, call):
     self._discard_ended_voips()
     if self.call_status(call.id) != VoIP.CALL_STATE_REQUESTED:
         return False
     voip = self.get_voip(call.id)
     dhc = self._get_dh_config()
     g_b = int.from_bytes(call.g_b, 'big')
     check_g(g_b, dhc.p)
     key = pow(g_b, voip.storage['a'], dhc.p)
     bytes_key = integer_to_bytes(key)
     key_fingerprint = calc_fingerprint(integer_to_bytes(key))
     call = self.client.send(
         ConfirmCall(key_fingerprint=key_fingerprint,
                     peer=InputPhoneCall(call.id, call.access_hash),
                     g_a=voip.storage['g_a'],
                     protocol=self._get_protocol())).phone_call
     return self._configure_voip(voip, call, bytes_key, key_fingerprint,
                                 voip.storage['g_a'])
コード例 #4
0
 def request_call(self, user_id):
     self._discard_ended_voips()
     peer = self.client.resolve_peer(user_id)
     dhc = self._get_dh_config()
     a = random.randint(2, dhc.p - 1)
     g_a = pow(dhc.g, a, dhc.p)
     check_g(g_a, dhc.p)
     g_a_hash = hashlib.sha256(integer_to_bytes(g_a)).digest()
     protocol = self._get_protocol()
     call = self.client.send(
         RequestCall(
             user_id=peer,
             random_id=random.randint(0, 0x7fffffff - 1),
             g_a_hash=g_a_hash,
             protocol=protocol,
         ))
     voip = self.voips[call.phone_call.id] = VoIP(
         True, peer.user_id,
         InputPhoneCall(call.phone_call.id, call.phone_call.access_hash),
         self, VoIP.CALL_STATE_REQUESTED, protocol)
     voip.storage = {'a': a, 'g_a': integer_to_bytes(g_a).ljust(256, b'\0')}
     return voip
コード例 #5
0
ファイル: pixels.py プロジェクト: tomas-dots/Pixels
 async def _upload_bulk_data(self,
                             data: bytes,
                             progress_callback,
                             timeout=DEFAULT_TIMEOUT):
     assert (len(data))
     assert (timeout >= 0)
     # Send setup message
     await self._send_and_ack(MessageType.BulkSetup,
                              integer_to_bytes(len(data), 2),
                              MessageType.BulkSetupAck, timeout)
     # Then transfer data
     total_size = len(data)
     remainingSize = total_size
     offset = 0
     while remainingSize > 0:
         size = min(remainingSize, PixelLink.PIXELS_MESSAGE_BULK_DATA_SIZE)
         header = [size] + integer_to_bytes(offset, 2)
         await self._send_and_ack(MessageType.BulkData,
                                  header + data[offset:offset + size],
                                  MessageType.BulkDataAck, timeout)
         if progress_callback != None:
             progress_callback(offset, total_size)
         remainingSize -= size
         offset += size
コード例 #6
0
def find_smallest_s(lower_bound, c):
    """
    Find the smallest s >= lower_bound,
    such that (c * s^e) (mod n) decrypts to a PKCS conforming string
    """
    s = lower_bound

    while True:
        attempt = (c * pow(s, e, n)) % n
        attempt = utils.integer_to_bytes(attempt)

        if oracle(attempt):
            return s

        s += 1
コード例 #7
0
 def complete_call(self, call):
     self._discard_ended_voips()
     voip = self.get_voip(call.id)
     if self.call_status(
             call.id) != VoIP.CALL_STATE_ACCEPTED or not voip.storage.get(
                 'b'):
         return False
     dhc = self._get_dh_config()
     if hashlib.sha256(call.g_a_or_b).digest() != voip.storage['g_a_hash']:
         raise RuntimeError('Invalid g_a')
     g_a_or_b = int.from_bytes(call.g_a_or_b, 'big')
     check_g(g_a_or_b, dhc.p)
     key = pow(g_a_or_b, voip.storage['b'], dhc.p)
     bytes_key = integer_to_bytes(key)
     key_fingerprint = calc_fingerprint(bytes_key)
     if key_fingerprint != call.key_fingerprint:
         raise RuntimeError('Invalid fingerprint')
     return self._configure_voip(voip, call, bytes_key, key_fingerprint,
                                 call.g_a_or_b)
コード例 #8
0
 def accept_call(self, call):
     self._discard_ended_voips()
     if self.call_status(call.id) != VoIP.CALL_STATE_ACCEPTED:
         return False
     dhc = self._get_dh_config()
     b = random.randint(2, dhc.p - 1)
     g_b = pow(dhc.g, b, dhc.p)
     check_g(g_b, dhc.p)
     try:
         res = self.client.send(
             AcceptCall(peer=call,
                        g_b=integer_to_bytes(g_b),
                        protocol=self._get_protocol()))
     except errors.Error as e:
         if e.CODE == 'CALL_ALREADY_ACCEPTED':
             return True
         elif e.CODE == 'CALL_ALREADY_DECLINED':
             return False
         raise e
     self.get_voip(res.phone_call.id).storage['b'] = b
     return True
コード例 #9
0
def find_s_in_range(a, b, prev_s, B, c):
    """
    Given the interval [a, b], reduce the search
    only to relevant regions (determined by r)
    and stop when an s value that gives
    a PKCS1 conforming string is found.
    """
    ri = ceil(2 * (b * prev_s - 2 * B), n)

    while True:
        si_lower = ceil(2 * B + ri * n, b)
        si_upper = ceil(3 * B + ri * n, a)

        for si in range(si_lower, si_upper):
            attempt = (c * pow(si, e, n)) % n
            attempt = utils.integer_to_bytes(attempt)

            if oracle(attempt):
                return si

        ri += 1
コード例 #10
0
def bleichenbacher(ciphertext):
    """
    Perform Bleichenbacher attack as described in his paper.
    """

    # Step 1. is only needed when the ciphertext is
    # not PKCS1 conforming

    # integer value of ciphertext
    c = utils.bytes_to_integer(ciphertext)

    B = 2**(8 * (k - 2))

    M = [Interval(2 * B, 3 * B - 1)]

    # Step 2.A.
    s = find_smallest_s(ceil(n, 3 * B), c)

    M = update_intervals(M, s, B)

    while True:
        # Step 2.B.
        if len(M) >= 2:
            s = find_smallest_s(s + 1, c)

        # Step 2.C.
        elif len(M) == 1:
            a, b = M[0]

            # Step 4.
            if a == b:
                return utils.integer_to_bytes(a % n)

            s = find_s_in_range(a, b, s, B, c)

        M = update_intervals(M, s, B)
コード例 #11
0
def encrypt_string(public_key, message):
    integer = utils.bytes_to_integer(message)
    enc_integer = encrypt_integer(public_key, integer)
    enc_string = utils.integer_to_bytes(enc_integer)

    return enc_string
コード例 #12
0
def decrypt_string(secret_key, ciphertext):
    enc_integer = utils.bytes_to_integer(ciphertext)
    integer = decrypt_integer(secret_key, enc_integer)
    message = utils.integer_to_bytes(integer)

    return message
コード例 #13
0
ファイル: pixels.py プロジェクト: tomas-dots/Pixels
 def force_LED_color(self, ledIndex, color: Color32):
     """ Led index starts at 0 """
     data = []
     data.extend(integer_to_bytes(ledIndex, 1))
     data.extend(integer_to_bytes(color.to_rgb(), 4))
     self._send(MessageType.SetLEDToColor, *data)
コード例 #14
0
ファイル: pixels.py プロジェクト: tomas-dots/Pixels
 def force_LEDs_color(self, color: Color32):
     c = integer_to_bytes(color.to_rgb(), 4)
     self._send(MessageType.SetAllLEDsToColor, *c)
コード例 #15
0
ファイル: pixels.py プロジェクト: tomas-dots/Pixels
 def append(dword):
     data.extend(integer_to_bytes(dword, 2))
コード例 #16
0
 def pack(self):
     return bytes(
         integer_to_bytes(self.trackOffset, 2) +
         integer_to_bytes(self.ledIndex, 1) +
         [0]) # padding
コード例 #17
0
 def pack(self):
     return bytes(
         integer_to_bytes(self.keyframesOffset, 2) +
         integer_to_bytes(self.keyFrameCount, 1) +
         [0]) # padding
コード例 #18
0
 def pack(self):
     return integer_to_bytes(self.timeAndColor, 2)