1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 from pyasn1
.codec
.der
import decoder
8 from pyasn1
.type import univ
9 from pyasn1_modules
import pem
12 def public_key_to_string(file, name
):
13 out
= "static const unsigned char " + name
+ "[65] = { "
15 substrate
= pem
.readPemFromFile(
16 f
, "-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----"
18 key
= decoder
.decode(substrate
)
20 assert ident
[0] == univ
.ObjectIdentifier(
22 ), "should be an ECPublicKey"
23 assert ident
[1] == univ
.ObjectIdentifier(
25 ), "should be a EcdsaP256 key"
27 assert isinstance(bits
, univ
.BitString
), "Should be a bit string"
28 assert len(bits
) == 520, "Should be 520 bits (65 bytes)"
29 for byte
in bits
.asOctets():
30 out
+= hex(byte
) + ", "
35 def generate(output
, test_key
, prod_key
):
36 output
.write(public_key_to_string(test_key
, "kTestKey"))
38 output
.write(public_key_to_string(prod_key
, "kProdKey"))
41 if __name__
== "__main__":
42 generate(sys
.stdout
, *sys
.argv
[1:])