Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: net/tools/testserver/asn1der.py

Issue 12235003: Split out policy code from net/tools/testserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 80 chars. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/tools/testserver/OWNERS ('k') | net/tools/testserver/device_management.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Helper module for ASN.1/DER encoding."""
6
7 import binascii
8 import struct
9
10 # Tags as defined by ASN.1.
11 INTEGER = 2
12 BIT_STRING = 3
13 NULL = 5
14 OBJECT_IDENTIFIER = 6
15 SEQUENCE = 0x30
16
17 def Data(tag, data):
18 """Generic type-length-value encoder.
19
20 Args:
21 tag: the tag.
22 data: the data for the given tag.
23 Returns:
24 encoded TLV value.
25 """
26 if len(data) == 0:
27 return struct.pack(">BB", tag, 0);
28 assert len(data) <= 0xffff;
29 return struct.pack(">BBH", tag, 0x82, len(data)) + data;
30
31 def Integer(value):
32 """Encodes an integer.
33
34 Args:
35 value: the long value.
36 Returns:
37 encoded TLV value.
38 """
39 data = '%x' % value
40 return Data(INTEGER, binascii.unhexlify('00' + '0' * (len(data) % 2) + data))
41
42 def Bitstring(value):
43 """Encodes a bit string.
44
45 Args:
46 value: a string holding the binary data.
47 Returns:
48 encoded TLV value.
49 """
50 return Data(BIT_STRING, '\x00' + value)
51
52 def Sequence(values):
53 """Encodes a sequence of other values.
54
55 Args:
56 values: the list of values, must be strings holding already encoded data.
57 Returns:
58 encoded TLV value.
59 """
60 return Data(SEQUENCE, ''.join(values))
OLDNEW
« no previous file with comments | « net/tools/testserver/OWNERS ('k') | net/tools/testserver/device_management.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698