def pack(self): """Return a string representation of this keyring.""" items = self.recognized + self.unrecognized # Scramble all the items, just to make sure that no broken # implementations rely on their oreder. getCommonPRNG().shuffle(items) encoded = [] for tp, val in items: encoded.append(struct.pack("!BH", tp, len(val))) encoded.append(val) return "".join(encoded)
def pack(self): """Return a string representation of this keyring.""" items = self.recognized+self.unrecognized # Scramble all the items, just to make sure that no broken # implementations rely on their oreder. getCommonPRNG().shuffle(items) encoded = [] for tp, val in items: encoded.append(struct.pack("!BH", tp, len(val))) encoded.append(val) return "".join(encoded)
def getFragments(self, s, paddingPRNG=None): """Given a string of length self.length, whiten it, pad it, and fragmment it. Return a list of the fragments, in order. (Note -- after building the fragment packets, be sure to shuffle them into a random order.)""" if paddingPRNG is None: paddingPRNG = getCommonPRNG() self.getFEC() assert len(s) == self.length s = whiten(s) s += paddingPRNG.getBytes(self.paddingLen) assert len(s) == self.paddedLen chunks = [] for i in xrange(self.nChunks): chunks.append( s[i*self.chunkSize:(i+1)*self.chunkSize] ) del s fragments = [] for i in xrange(self.nChunks): blocks = [] for j in xrange(self.k): blocks.append( chunks[i][j*self.fragCapacity: (j+1)*self.fragCapacity] ) chunks[i] = None for j in xrange(self.n): fragments.append( self.fec.encode(j, blocks) ) return fragments
def getFragments(self, s, paddingPRNG=None): """Given a string of length self.length, whiten it, pad it, and fragmment it. Return a list of the fragments, in order. (Note -- after building the fragment packets, be sure to shuffle them into a random order.)""" if paddingPRNG is None: paddingPRNG = getCommonPRNG() self.getFEC() assert len(s) == self.length s = whiten(s) s += paddingPRNG.getBytes(self.paddingLen) assert len(s) == self.paddedLen chunks = [] for i in xrange(self.nChunks): chunks.append(s[i * self.chunkSize:(i + 1) * self.chunkSize]) del s fragments = [] for i in xrange(self.nChunks): blocks = [] for j in xrange(self.k): blocks.append(chunks[i][j * self.fragCapacity:(j + 1) * self.fragCapacity]) chunks[i] = None for j in xrange(self.n): fragments.append(self.fec.encode(j, blocks)) return fragments
def getBatch(self): n = self._getBatchSize() count = self.count() if n == 0 or count == 0: return [] msgProbability = n / float(count) rng = getCommonPRNG() return rng.shuffle([h for h in self.getAllMessages() if rng.getFloat() < msgProbability])
def pickRandom(self, count=None): """Returns a list of 'count' handles to messages in this filestore. The messages are chosen randomly, and returned in a random order. If there are fewer than 'count' messages in the filestore, all the messages will be included.""" handles = self.getAllMessages() # handles locking return getCommonPRNG().shuffle(handles, count)
def openNewMessage(self): """Returns (file, handle) tuple to create a new message. Once you're done writing, you must call finishMessage to commit your changes, or abortMessage to reject them.""" while 1: f, handle = getCommonPRNG().openNewFile(self.dir, "inp_", 1, "msg_") return f, handle raise AssertionError # unreached; appease pychecker
def __init__(self, s=None, isJunk=0, callback=None): if isJunk: self.s = getCommonPRNG().getBytes(1<<15) else: self.s = s self.j = isJunk self.cb = callback self._failed = 0 self._succeeded = 0
def __init__(self, s=None, isJunk=0, callback=None): if isJunk: self.s = getCommonPRNG().getBytes(1 << 15) else: self.s = s self.j = isJunk self.cb = callback self._failed = 0 self._succeeded = 0
def getBatch(self): n = self._getBatchSize() count = self.count() if n == 0 or count == 0: return [] msgProbability = n / float(count) rng = getCommonPRNG() return rng.shuffle([ h for h in self.getAllMessages() if rng.getFloat() < msgProbability ])
def _writeEncryptedFile(fname, password, magic, data): """Write 'data' into an encrypted file named 'fname', replacing it if necessary. Encrypts the data with the password 'password', and uses the filetype 'magic'.""" assert len(magic) == MAGIC_LEN prng = getCommonPRNG() length = struct.pack("!L", len(data)) paddingLen = ceilDiv(len(data), 1024)*1024 - len(data) padding = prng.getBytes(paddingLen) data = "".join([length,data,padding]) salt = prng.getBytes(SALT_LEN) key = sha1(salt+password+salt)[:AES_KEY_LEN] digest = sha1("".join([data,salt,magic])) encrypted = ctr_crypt(data+digest, key) contents = "".join([magic,"\x00",salt,encrypted]) writeFile(fname, armorText(contents, "TYPE III KEYRING", [("Version","0.1")]))
def _writeEncryptedFile(fname, password, magic, data): """Write 'data' into an encrypted file named 'fname', replacing it if necessary. Encrypts the data with the password 'password', and uses the filetype 'magic'.""" assert len(magic) == MAGIC_LEN prng = getCommonPRNG() length = struct.pack("!L", len(data)) paddingLen = ceilDiv(len(data), 1024) * 1024 - len(data) padding = prng.getBytes(paddingLen) data = "".join([length, data, padding]) salt = prng.getBytes(SALT_LEN) key = sha1(salt + password + salt)[:AES_KEY_LEN] digest = sha1("".join([data, salt, magic])) encrypted = ctr_crypt(data + digest, key) contents = "".join([magic, "\x00", salt, encrypted]) writeFile(fname, armorText(contents, "TYPE III KEYRING", [("Version", "0.1")]))
def getBatch(self): msgProbability = self._getFraction() rng = getCommonPRNG() return rng.shuffle([ h for h in self.getAllMessages() if rng.getFloat() < msgProbability ])
def __init__(self): self.contents = getCommonPRNG().getBytes(1<<15)
def __init__(self): self.contents = getCommonPRNG().getBytes(1 << 15)