File: pem.py

package info (click to toggle)
mozjs52 52.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 250,248 kB
  • sloc: cpp: 818,254; ansic: 278,823; python: 205,544; sh: 27,794; asm: 13,536; makefile: 10,661; perl: 7,438; xml: 2,812; java: 1,421; exp: 499; lisp: 258; objc: 234; csh: 17; sed: 17
file content (51 lines) | stat: -rw-r--r-- 1,893 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import base64, sys

stSpam, stHam, stDump = 0, 1, 2

# The markers parameters is in form ('start1', 'stop1'), ('start2', 'stop2')...
# Return is (marker-index, substrate)
def readPemBlocksFromFile(fileObj, *markers):
    startMarkers = dict(map(lambda x: (x[1],x[0]),
                            enumerate(map(lambda x: x[0], markers))))
    stopMarkers = dict(map(lambda x: (x[1],x[0]),
                           enumerate(map(lambda x: x[1], markers))))
    idx = -1; substrate = ''
    state = stSpam
    while 1:
        certLine = fileObj.readline()
        if not certLine:
            break
        certLine = certLine.strip()
        if state == stSpam:
            if certLine in startMarkers:
                certLines = []
                idx = startMarkers[certLine]
                state = stHam
                continue
        if state == stHam:
            if certLine in stopMarkers and stopMarkers[certLine] == idx:
                state = stDump
            else:
                certLines.append(certLine)
        if state == stDump:
            if sys.version_info[0] <= 2:
                substrate = ''.join([ base64.b64decode(x) for x in certLines ])
            else:
                substrate = ''.encode().join([ base64.b64decode(x.encode()) for x in certLines ])
            break
    return idx, substrate

# Backward compatibility routine
def readPemFromFile(fileObj, 
                    startMarker='-----BEGIN CERTIFICATE-----',
                    endMarker='-----END CERTIFICATE-----'):
    idx, substrate = readPemBlocksFromFile(fileObj, (startMarker, endMarker))
    return substrate

def readBase64FromFile(fileObj):
    if sys.version_info[0] <= 2:
        return ''.join([ base64.b64decode(x) for x in fileObj.readlines() ])
    else:
        return ''.encode().join(
            [ base64.b64decode(x.encode()) for x in fileObj.readlines() ]
        )