def encode(self, message, poly=False, k=None, return_string=True):
        '''Encode a given string or list of values (between 0 and gf2_charac)
        with reed-solomon encoding. Returns a list of values (or a string if return_string is true)
        with the k message bytes and n-k parity bytes at the end.
        
        If a message is < k bytes long, it is assumed to be padded at the front
        with null bytes.

        The sequence returned is always n bytes long.

        If poly is not False, returns the encoded Polynomial object instead of
        the polynomial translated back to a string (useful for debugging)
        '''
        n = self.n
        if not k: k = self.k

        if len(message)>k:
            raise ValueError("Message length is max %d. Message was %d" % (k,
                len(message)))

        # If we were given a string, convert to a list (important to support fields above 2^8)
        if isinstance(message, _str):
            message = [ord(x) for x in message]

        # Encode message as a polynomial:
        m = Polynomial([GF2int(x) for x in message])

        # Shift polynomial up by n-k by multiplying by x^(n-k) to reserve the first n-k coefficients for the ecc. This effectively pad the message with \0 bytes for the lower coefficients (where the ecc will be placed).
        mprime = m * Polynomial([GF2int(1)] + [GF2int(0)]*(n-k))

        # mprime = q*g + b for some q
        # so let's find b, the code word (ecc block):
        b = mprime % self.g[k]

        # Subtract out b, so now c = q*g, which is a way of xoring mprime and code word b, which is a way of just saying that we append the polynomial ecc code to the original message (we replace the padded 0 bytes of mprime with the code word)
        c = mprime - b
        # Since c is a multiple of g, it has (at least) n-k roots: α^1 through
        # α^(n-k)

        if not poly:
            # Turn the polynomial c back into a string
            ret = self._list_rjust(c.coefficients, n, 0) # rjust is useful for the nostrip feature
            if return_string and self.gf2_charac < 256:
                ret = self._list2str(ret)
        else:
            ret = c

        return ret
def encode ( self , message , poly = False , k = None , return_string = True ) : n = self . n if not k : k = self . k if len ( message ) > k : raise ValueError ( "Message length is max %d. Message was %d" % ( k , len ( message ) ) ) if isinstance ( message , _str ) : message = [ ord ( x ) for x in message ] m = Polynomial ( [ GF2int ( x ) for x in message ] ) mprime = m * Polynomial ( [ GF2int ( 1 ) ] + [ GF2int ( 0 ) ] * ( n - k ) ) b = mprime % self . g [ k ] c = mprime - b if not poly : ret = self . _list_rjust ( c . coefficients , n , 0 ) if return_string and self . gf2_charac < 256 : ret = self . _list2str ( ret ) else : ret = c return ret
Encode a given string or list of values (between 0 and gf2_charac)
        with reed-solomon encoding. Returns a list of values (or a string if return_string is true)
        with the k message bytes and n-k parity bytes at the end.
        
        If a message is < k bytes long, it is assumed to be padded at the front
        with null bytes.

        The sequence returned is always n bytes long.

        If poly is not False, returns the encoded Polynomial object instead of
        the polynomial translated back to a string (useful for debugging)