Coverage for /pythoncovmergedfiles/medio/medio/src/pdfminer.six/pdfminer/arcfour.py: 21%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

24 statements  

1"""Python implementation of Arcfour encryption algorithm. 

2See https://en.wikipedia.org/wiki/RC4 

3This code is in the public domain. 

4 

5""" 

6 

7from typing import Sequence 

8 

9 

10class Arcfour: 

11 def __init__(self, key: Sequence[int]) -> None: 

12 # because Py3 range is not indexable 

13 s = [i for i in range(256)] 

14 j = 0 

15 klen = len(key) 

16 for i in range(256): 

17 j = (j + s[i] + key[i % klen]) % 256 

18 (s[i], s[j]) = (s[j], s[i]) 

19 self.s = s 

20 (self.i, self.j) = (0, 0) 

21 

22 def process(self, data: bytes) -> bytes: 

23 (i, j) = (self.i, self.j) 

24 s = self.s 

25 r = b"" 

26 for c in iter(data): 

27 i = (i + 1) % 256 

28 j = (j + s[i]) % 256 

29 (s[i], s[j]) = (s[j], s[i]) 

30 k = s[(s[i] + s[j]) % 256] 

31 r += bytes((c ^ k,)) 

32 (self.i, self.j) = (i, j) 

33 return r 

34 

35 encrypt = decrypt = process