]> gitweb.pimeys.fr Git - NK2015_Client_Python_Alpha.git/blob - rsa_source/rsa/bigfile.py
on ajoute le module rsa car le client aussi en a besoin
[NK2015_Client_Python_Alpha.git] / rsa_source / rsa / bigfile.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 '''Large file support
17
18 - break a file into smaller blocks, and encrypt them, and store the
19 encrypted blocks in another file.
20
21 - take such an encrypted files, decrypt its blocks, and reconstruct the
22 original file.
23
24 The encrypted file format is as follows, where || denotes byte concatenation:
25
26 FILE := VERSION || BLOCK || BLOCK ...
27
28 BLOCK := LENGTH || DATA
29
30 LENGTH := varint-encoded length of the subsequent data. Varint comes from
31 Google Protobuf, and encodes an integer into a variable number of bytes.
32 Each byte uses the 7 lowest bits to encode the value. The highest bit set
33 to 1 indicates the next byte is also part of the varint. The last byte will
34 have this bit set to 0.
35
36 This file format is called the VARBLOCK format, in line with the varint format
37 used to denote the block sizes.
38
39 '''
40
41 from rsa import key, common, pkcs1, varblock
42
43 def encrypt_bigfile(infile, outfile, pub_key):
44 '''Encrypts a file, writing it to 'outfile' in VARBLOCK format.
45
46 :param infile: file-like object to read the cleartext from
47 :param outfile: file-like object to write the crypto in VARBLOCK format to
48 :param pub_key: :py:class:`rsa.PublicKey` to encrypt with
49
50 '''
51
52 if not isinstance(pub_key, key.PublicKey):
53 raise TypeError('Public key required, but got %r' % pub_key)
54
55 key_bytes = common.bit_size(pub_key.n) // 8
56 blocksize = key_bytes - 11 # keep space for PKCS#1 padding
57
58 # Write the version number to the VARBLOCK file
59 outfile.write(chr(varblock.VARBLOCK_VERSION))
60
61 # Encrypt and write each block
62 for block in varblock.yield_fixedblocks(infile, blocksize):
63 crypto = pkcs1.encrypt(block, pub_key)
64
65 varblock.write_varint(outfile, len(crypto))
66 outfile.write(crypto)
67
68 def decrypt_bigfile(infile, outfile, priv_key):
69 '''Decrypts an encrypted VARBLOCK file, writing it to 'outfile'
70
71 :param infile: file-like object to read the crypto in VARBLOCK format from
72 :param outfile: file-like object to write the cleartext to
73 :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with
74
75 '''
76
77 if not isinstance(priv_key, key.PrivateKey):
78 raise TypeError('Private key required, but got %r' % priv_key)
79
80 for block in varblock.yield_varblocks(infile):
81 cleartext = pkcs1.decrypt(block, priv_key)
82 outfile.write(cleartext)
83
84 __all__ = ['encrypt_bigfile', 'decrypt_bigfile']
85