import synta
import synta.oids as oids
def section(title):
print(f"\n{'─' * 60}\n{title}\n{'─' * 60}")
def demo_constructors():
section("Three constructors — all yield equivalent OIDs")
oid_str = synta.ObjectIdentifier("1.2.840.113549.1.1.11")
oid_comp = synta.ObjectIdentifier.from_components([1, 2, 840, 113549, 1, 1, 11])
der_value = bytes.fromhex("2a864886f70d01010b")
oid_der = synta.ObjectIdentifier.from_der_value(der_value)
print(f" from string: {oid_str}")
print(f" from components: {oid_comp}")
print(f" from_der_value: {oid_der}")
assert oid_str == oid_comp == oid_der
print(" All three are equal: OK")
def demo_components():
section("components() — tuple of integer arcs")
oid = synta.ObjectIdentifier("2.5.4.3") comps = oid.components()
print(f" OID: {oid}")
print(f" components: {comps}")
assert list(comps) == [2, 5, 4, 3]
def demo_equality():
section("__eq__ — compare against OID or dotted string")
rsa_oid = oids.RSA_ENCRYPTION
other = synta.ObjectIdentifier("1.2.840.113549.1.1.1")
assert rsa_oid == other
print(f" {rsa_oid} == ObjectIdentifier(same): True")
assert rsa_oid == "1.2.840.113549.1.1.1"
assert not (rsa_oid == "1.2.840.113549.1.1.11")
print(f" {rsa_oid} == '1.2.840.113549.1.1.1': True")
print(f" {rsa_oid} == '1.2.840.113549.1.1.11': False")
def demo_hashability():
section("__hash__ — use OIDs as dict keys")
alg_names = {
oids.RSA_ENCRYPTION: "RSA",
oids.EC_PUBLIC_KEY: "EC",
oids.ED25519: "Ed25519",
}
test_oid = synta.ObjectIdentifier("1.2.840.113549.1.1.1")
print(f" alg_names[RSA_ENCRYPTION] = {alg_names[test_oid]!r}")
assert alg_names[test_oid] == "RSA"
oid = oids.SHA256
assert hash(oid) == hash(str(oid))
print(f" hash(oid) == hash(str(oid)) for {oid}: OK")
def demo_encode_decode_roundtrip():
section("encode_oid / decode_oid round-trip")
oid = synta.ObjectIdentifier("1.3.6.1.5.5.7.48.1.1")
enc = synta.Encoder(synta.Encoding.DER)
enc.encode_oid(oid)
encoded = enc.finish()
print(f" DER: {encoded.hex()}")
dec = synta.Decoder(encoded, synta.Encoding.DER)
decoded = dec.decode_oid()
assert decoded == oid
print(f" Decoded: {decoded}")
print(" encode_oid / decode_oid round-trip: OK")
def demo_encode_oid_object():
section("encode_oid_object — encode an ObjectIdentifier instance")
oid = oids.ED25519 enc = synta.Encoder(synta.Encoding.DER)
enc.encode_oid_object(oid)
encoded = enc.finish()
dec = synta.Decoder(encoded, synta.Encoding.DER)
decoded = dec.decode_oid()
assert decoded == oid
print(f" encode_oid_object({oid}) → {encoded.hex()}")
print(f" Decoded back: {decoded}")
print(" encode_oid_object round-trip: OK")
def demo_from_der_value_implicit():
section("from_der_value — decode OID in implicit-tag context")
content = bytes.fromhex("551d13")
oid = synta.ObjectIdentifier.from_der_value(content)
print(f" content: {content.hex()}")
print(f" OID: {oid}")
assert oid == "2.5.29.19"
content_cn = bytes.fromhex("550403")
oid_cn = synta.ObjectIdentifier.from_der_value(content_cn)
print(f" id-at-commonName: {oid_cn}")
assert oid_cn == "2.5.4.3"
def main():
print("=" * 60)
print("Example 11: ObjectIdentifier constructors and operations")
print("=" * 60)
demo_constructors()
demo_components()
demo_equality()
demo_hashability()
demo_encode_decode_roundtrip()
demo_encode_oid_object()
demo_from_der_value_implicit()
print("\nAll ObjectIdentifier examples completed.")
if __name__ == "__main__":
main()