当前位置: 首页>>代码示例>>Python>>正文


Python openssl.rand_bytes方法代码示例

本文整理汇总了Python中shadowsocks.crypto.openssl.rand_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python openssl.rand_bytes方法的具体用法?Python openssl.rand_bytes怎么用?Python openssl.rand_bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在shadowsocks.crypto.openssl的用法示例。


在下文中一共展示了openssl.rand_bytes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: client_udp_pre_encrypt

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def client_udp_pre_encrypt(self, buf):
        if self.user_key is None:
            if b':' in to_bytes(self.server_info.protocol_param):
                try:
                    items = to_bytes(self.server_info.protocol_param).split(':')
                    self.user_key = self.hashfunc(items[1]).digest()
                    self.user_id = struct.pack('<I', int(items[0]))
                except:
                    pass
            if self.user_key is None:
                self.user_id = rand_bytes(4)
                self.user_key = self.server_info.key
        authdata = rand_bytes(3)
        mac_key = self.server_info.key
        md5data = hmac.new(mac_key, authdata, self.hashfunc).digest()
        uid = struct.unpack('<I', self.user_id)[0] ^ struct.unpack('<I', md5data[:4])[0]
        uid = struct.pack('<I', uid)
        rand_len = self.udp_rnd_data_len(md5data, self.random_client)
        encryptor = encrypt.Encryptor(
            to_bytes(base64.b64encode(self.user_key)) + to_bytes(base64.b64encode(md5data)), 'rc4')
        out_buf = encryptor.encrypt(buf)
        buf = out_buf + rand_bytes(rand_len) + authdata + uid
        return buf + hmac.new(self.user_key, buf, self.hashfunc).digest()[:1] 
开发者ID:PaperDashboard,项目名称:shadowsocks,代码行数:25,代码来源:auth_chain.py

示例2: server_udp_pre_encrypt

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def server_udp_pre_encrypt(self, buf, uid):
        if uid in self.server_info.users:
            user_key = self.server_info.users[uid]
        else:
            uid = None
            if not self.server_info.users:
                user_key = self.server_info.key
            else:
                user_key = self.server_info.recv_iv
        authdata = rand_bytes(7)
        mac_key = self.server_info.key
        md5data = hmac.new(mac_key, authdata, self.hashfunc).digest()
        rand_len = self.udp_rnd_data_len(md5data, self.random_server)
        encryptor = encrypt.Encryptor(to_bytes(base64.b64encode(user_key)) + to_bytes(base64.b64encode(md5data)), 'rc4')
        out_buf = encryptor.encrypt(buf)
        buf = out_buf + rand_bytes(rand_len) + authdata
        return buf + hmac.new(user_key, buf, self.hashfunc).digest()[:1] 
开发者ID:PaperDashboard,项目名称:shadowsocks,代码行数:19,代码来源:auth_chain.py

示例3: random_string

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def random_string(length):
    try:
        return os.urandom(length)
    except NotImplementedError as e:
        return openssl.rand_bytes(length) 
开发者ID:hao35954514,项目名称:shadowsocksR-b,代码行数:7,代码来源:encrypt.py

示例4: rnd_data

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def rnd_data(self, buf_size, buf, last_hash, random):
        rand_len = self.rnd_data_len(buf_size, last_hash, random)

        rnd_data_buf = rand_bytes(rand_len)

        if buf_size == 0:
            return rnd_data_buf
        else:
            if rand_len > 0:
                start_pos = self.rnd_start_pos(rand_len, random)
                return rnd_data_buf[:start_pos] + buf + rnd_data_buf[start_pos:]
            else:
                return buf 
开发者ID:PaperDashboard,项目名称:shadowsocks,代码行数:15,代码来源:auth_chain.py

示例5: pack_auth_data

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def pack_auth_data(self, auth_data, buf):
        data = auth_data
        data = data + (struct.pack('<H', self.server_info.overhead) + struct.pack('<H', 0))
        mac_key = self.server_info.iv + self.server_info.key

        check_head = rand_bytes(4)
        self.last_client_hash = hmac.new(mac_key, check_head, self.hashfunc).digest()
        check_head += self.last_client_hash[:8]

        if b':' in to_bytes(self.server_info.protocol_param):
            try:
                items = to_bytes(self.server_info.protocol_param).split(b':')
                self.user_key = items[1]
                uid = struct.pack('<I', int(items[0]))
            except:
                uid = rand_bytes(4)
        else:
            uid = rand_bytes(4)
        if self.user_key is None:
            self.user_key = self.server_info.key

        encryptor = encrypt.Encryptor(
            to_bytes(base64.b64encode(self.user_key)) + self.salt, 'aes-128-cbc', b'\x00' * 16)

        uid = struct.unpack('<I', uid)[0] ^ struct.unpack('<I', self.last_client_hash[8:12])[0]
        uid = struct.pack('<I', uid)
        data = uid + encryptor.encrypt(data)[16:]
        self.last_server_hash = hmac.new(self.user_key, data, self.hashfunc).digest()
        data = check_head + data + self.last_server_hash[:4]
        self.encryptor = encrypt.Encryptor(
            to_bytes(base64.b64encode(self.user_key)) + to_bytes(base64.b64encode(self.last_client_hash)), 'rc4')
        return data + self.pack_client_data(buf) 
开发者ID:PaperDashboard,项目名称:shadowsocks,代码行数:34,代码来源:auth_chain.py

示例6: auth_data

# 需要导入模块: from shadowsocks.crypto import openssl [as 别名]
# 或者: from shadowsocks.crypto.openssl import rand_bytes [as 别名]
def auth_data(self):
        utc_time = int(time.time()) & 0xFFFFFFFF
        if self.server_info.data.connection_id > 0xFF000000:
            self.server_info.data.local_client_id = b''
        if not self.server_info.data.local_client_id:
            self.server_info.data.local_client_id = rand_bytes(4)
            logging.debug("local_client_id %s" % (binascii.hexlify(self.server_info.data.local_client_id),))
            self.server_info.data.connection_id = struct.unpack('<I', rand_bytes(4))[0] & 0xFFFFFF
        self.server_info.data.connection_id += 1
        return b''.join([struct.pack('<I', utc_time),
                         self.server_info.data.local_client_id,
                         struct.pack('<I', self.server_info.data.connection_id)]) 
开发者ID:PaperDashboard,项目名称:shadowsocks,代码行数:14,代码来源:auth_chain.py


注:本文中的shadowsocks.crypto.openssl.rand_bytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。