Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
    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
Пример #4
0
    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
Пример #5
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])
Пример #6
0
    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)
Пример #7
0
 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
Пример #8
0
 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
Пример #9
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
Пример #10
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
Пример #11
0
    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)
Пример #12
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
     ])
Пример #13
0
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")]))
Пример #14
0
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")]))
Пример #15
0
 def getBatch(self):
     msgProbability = self._getFraction()
     rng = getCommonPRNG()
     return rng.shuffle([
         h for h in self.getAllMessages() if rng.getFloat() < msgProbability
     ])
Пример #16
0
 def __init__(self):
     self.contents = getCommonPRNG().getBytes(1<<15)
Пример #17
0
 def __init__(self):
     self.contents = getCommonPRNG().getBytes(1 << 15)
Пример #18
0
 def getBatch(self):
     msgProbability = self._getFraction()
     rng = getCommonPRNG()
     return rng.shuffle([ h for h in self.getAllMessages()
                          if rng.getFloat() < msgProbability ])