python - How to convert a string to a base64 format standard RFC-2045 MIME? -
i'm trying convert string base64 format standard rfc-2045.
my code is
import base64 auth_header = base64.b64encode('user:abcd'.decode('ascii'))
don't know base64 standard whether using rfc-2045
(base64 encoding in python)[https://docs.python.org/3/library/base64.html] : provides encoding , decoding functions encodings specified in rfc 3548, defines base16, base32, , base64 algorithms, , de-facto standard ascii85 , base85 encodings.
rfc-2045 mime used email. refer :
encoding-of-headers-in-mimetext
docs :
email.mime: creating email , mime objects scratch
i think solution looking code base64 encoding rfc 2045, check this:
# copyright (c) 2002-2006 python software foundation # author: ben gertzfield # contact: email-sig@python.org """base64 content transfer encoding per rfcs 2045-2047. module handles content transfer encoding method defined in rfc 2045 encode arbitrary 8-bit data using 3 8-bit bytes in 4 7-bit characters encoding known base64. used in mime standards email attach images, audio, , text using 8-bit character sets messages. module provides interface encode , decode both headers , bodies base64 encoding. rfc 2045 defines method including character set information in `encoded-word' in header. method commonly used 8-bit real names in to:, from:, cc:, etc. fields, subject: lines. module not line wrapping or end-of-line character conversion necessary proper internationalized headers; dumb encoding , decoding. deal various line wrapping issues, use email.header module. """ __all__ = [ 'base64_len', 'body_decode', 'body_encode', 'decode', 'decodestring', 'encode', 'encodestring', 'header_encode', ] binascii import b2a_base64, a2b_base64 email.utils import fix_eols crlf = '\r\n' nl = '\n' emptystring = '' # see charset.py misc_len = 7 # helpers def base64_len(s): """return length of s when encoded base64.""" groups_of_3, leftover = divmod(len(s), 3) # 4 bytes out each 3 bytes (or nonzero fraction thereof) in. # thanks, tim! n = groups_of_3 * 4 if leftover: n += 4 return n def header_encode(header, charset='iso-8859-1', keep_eols=false, maxlinelen=76, eol=nl): """encode single header line base64 encoding in given charset. defined in rfc 2045, base64 encoding identical normal base64 encoding, except each line must intelligently wrapped (respecting base64 encoding), , subsequent lines must start space. charset names character set use encode header. defaults iso-8859-1. end-of-line characters (\\r, \\n, \\r\\n) automatically converted canonical email line separator \\r\\n unless keep_eols parameter true (the default false). each line of header terminated in value of eol, defaults "\\n". set "\\r\\n" if using result of function directly in email. resulting string in form: "=?charset?b?ww/5cibtyxp66xlrihf8eibhighhbxbzdghucibbiflv+xigbwf6euly?=\\n =?charset?b?6yb3/hogysboyw1wc3rh7nigqkmgww/5cibtyxp66xlrihf8eibhighh?=" each line wrapped at, @ most, maxlinelen characters (defaults 76 characters). """ # return empty headers unchanged if not header: return header if not keep_eols: header = fix_eols(header) # base64 encode each line, in encoded chunks no greater maxlinelen in # length, after rfc chrome added in. base64ed = [] max_encoded = maxlinelen - len(charset) - misc_len max_unencoded = max_encoded * 3 // 4 in range(0, len(header), max_unencoded): base64ed.append(b2a_base64(header[i:i+max_unencoded])) # add rfc chrome each encoded chunk lines = [] line in base64ed: # ignore last character of each line if newline if line.endswith(nl): line = line[:-1] # add chrome lines.append('=?%s?b?%s?=' % (charset, line)) # glue lines , return it. baw: should able # specify leading whitespace in joiner? joiner = eol + ' ' return joiner.join(lines) def encode(s, binary=true, maxlinelen=76, eol=nl): """encode string base64. each line wrapped at, @ most, maxlinelen characters (defaults 76 characters). if binary false, end-of-line characters converted canonical email end-of-line sequence \\r\\n. otherwise left verbatim (this default). each line of encoded text end eol, defaults "\\n". set "\r\n" if using result of function directly in email. """ if not s: return s if not binary: s = fix_eols(s) encvec = [] max_unencoded = maxlinelen * 3 // 4 in range(0, len(s), max_unencoded): # baw: should encode() inherit b2a_base64()'s dubious behavior in # adding newline encoded string? enc = b2a_base64(s[i:i + max_unencoded]) if enc.endswith(nl) , eol != nl: enc = enc[:-1] + eol encvec.append(enc) return emptystring.join(encvec) # convenience , backwards compatibility w/ standard base64 module body_encode = encode encodestring = encode def decode(s, convert_eols=none): """decode raw base64 string. if convert_eols set string value, canonical email linefeeds, e.g. "\\r\\n", in decoded text converted value of convert_eols. os.linesep choice convert_eols if decoding text attachment. function not parse full mime header value encoded base64 (like =?iso-8895-1?b?bmloisbuawgh?=) -- please use high level email.header class functionality. """ if not s: return s dec = a2b_base64(s) if convert_eols: return dec.replace(crlf, convert_eols) return dec # convenience , backwards compatibility w/ standard base64 module body_decode = decode decodestring = decode
solution:
print(encode('user:abcd'))
output:
dxnlcjphymnk
reference : base64mime.py
Comments
Post a Comment