def toAsn1IntBytes(b):
    """Return a bytearray containing ASN.1 integer based on input bytearray.
    
    An ASN.1 integer is a big-endian sequence of bytes, with excess zero bytes
    at the beginning removed.  However, if the high bit of the first byte 
    would be set, a zero byte is prepended.  Note that the ASN.1 type/len
    fields are NOT added by this function.
    """    
    # Strip leading zeros
    while b[0] == 0 and len(b)>1:
        b = b[1:]
    # Add a leading zero if high bit is set
    if b[0] & 0x80:
        b = bytearray([0]) + b
    return b