Chromium Code Reviews| Index: chrome/test/pyautolib/asn1der.py |
| diff --git a/chrome/test/pyautolib/asn1der.py b/chrome/test/pyautolib/asn1der.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0a3c6220363a8f974139cfa43e38298fb6a34c74 |
| --- /dev/null |
| +++ b/chrome/test/pyautolib/asn1der.py |
| @@ -0,0 +1,61 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Helper module for ASN.1/DER encoding.""" |
| + |
| +import binascii |
| +import struct |
| + |
| +# Tags as defined by ASN.1. |
| +INTEGER = 2 |
|
Nirnimesh
2012/04/05 19:05:30
global vars local to this file should have _ prefi
bartfab (slow)
2012/04/10 15:27:20
I had actually copied this file from another place
|
| +BIT_STRING = 3 |
| +NULL = 5 |
| +OBJECT_IDENTIFIER = 6 |
| +SEQUENCE = 0x30 |
| + |
|
Nirnimesh
2012/04/05 19:05:30
nit: need 2 blank lines between functions in globa
bartfab (slow)
2012/04/10 15:27:20
Done.
|
| +def Data(tag, data): |
| + """Generic type-length-value encoder. |
| + |
| + Args: |
| + tag: the tag. |
| + data: the data for the given tag. |
|
Nirnimesh
2012/04/05 19:05:30
nit: leave a blank line before Returns: section
Re
bartfab (slow)
2012/04/10 15:27:20
Done.
|
| + Returns: |
| + encoded TLV value. |
| + """ |
| + if len(data) == 0: |
| + return struct.pack(">BB", tag, 0); |
| + assert len(data) <= 0xffff; |
| + return struct.pack(">BBH", tag, 0x82, len(data)) + data; |
| + |
| +def Integer(value): |
| + """Encodes an integer. |
| + |
| + Args: |
| + value: the long value. |
| + Returns: |
| + encoded TLV value. |
| + """ |
| + data = '%x' % value |
| + return Data(INTEGER, binascii.unhexlify('00' + '0' * (len(data) % 2) + data)) |
| + |
| +def Bitstring(value): |
| + """Encodes a bit string. |
| + |
| + Args: |
| + value: a string holding the binary data. |
| + Returns: |
| + encoded TLV value. |
| + """ |
| + return Data(BIT_STRING, '\x00' + value) |
| + |
| +def Sequence(values): |
| + """Encodes a sequence of other values. |
| + |
| + Args: |
| + values: the list of values, must be strings holding already encoded data. |
| + Returns: |
| + encoded TLV value. |
| + """ |
| + return Data(SEQUENCE, ''.join(values)) |