Table of Contents
ASN1Kit is an ASN.1 (Abstract Syntax Notation One) Decoder for Swift.
This library can be used for ASN.1 encoding/decoding using distinguished encoding rules (DER) according to the ITU-T X.690 specification.
For more information, please find the complete X.690-0207.pdf specification.
See ReleaseNotes for all information regarding the (newest) releases.
ASN1Kit requires Swift 5.5.
Use ASN1Decoder.decode(asn1:) to decode serialized data:
let expected = Data([0xB, 0xB, 0x0])
let serialized = Data([0x23, 0x0C,
0x03, 0x02, 0x00, 0x0B,
0x03, 0x02, 0x00, 0x0B,
0x03, 0x02, 0x04, 0x0F])
expect(try Data(from: ASN1Decoder.decode(asn1: serialized))) == expectedConstruct an ASN1Object of your choice and serialize it:
let data = Data([0x0, 0x1, 0x2, 0x4]) as ASN1EncodableType
let data2 = Data([0x4, 0x3, 0x2, 0x1]) as ASN1EncodableType
let array = [data, data2]
let expected = Data([0x30, 0xC, 0x4, 0x4, 0x0, 0x1, 0x2, 0x4, 0x4, 0x4, 0x4, 0x3, 0x2, 0x1])
expect(try array.asn1encode().serialize()) == expectedASN.1-encode Swift primitives and extract the encoded value(s):
// 0x0080 = 128
let expected = Data([0x00, 0x80])
expect(try 128.asn1encode(tag: nil).data.primitive) == expected
// 0x0080 = 128
let expected = Data([0x00, 0x80])
expect(try UInt(128).asn1encode(tag: nil).data.primitive) == expectedExtract the tag of the first element of a constructed ASN1Object:
let data = Data([0x1, 0x2, 0x3, 0x4, 0x8])
let tag1 = ASN1Primitive(data: .primitive(data), tag: .taggedTag(3)) // context-specific class tag
let tag2 = ASN1Primitive(data: .primitive(data), tag: .universal(ASN1Tag.octetString))
let implicitTag = ASN1Primitive(data: .constructed([tag1, tag2]), tag: .taggedTag(83))
expect(implicitTag.data.items?.first?.tag) == .taggedTag(3)For more examples and in-depth documentation, please refer to the API Documentation.
Copyright 2019-2025 gematik GmbH
Apache License, Version 2.0
See the LICENSE for the specific language governing permissions and limitations under the License.
-
Copyright notice: Each published work result is accompanied by an explicit statement of the license conditions for use. These are regularly typical conditions in connection with open source or free software. Programs described/provided/linked here are free software, unless otherwise stated.
-
Permission notice: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
The copyright notice (Item 1) and the permission notice (Item 2) shall be included in all copies or substantial portions of the Software.
-
The software is provided "as is" without warranty of any kind, either express or implied, including, but not limited to, the warranties of fitness for a particular purpose, merchantability, and/or non-infringement. The authors or copyright holders shall not be liable in any manner whatsoever for any damages or other claims arising from, out of or in connection with the software or the use or other dealings with the software, whether in an action of contract, tort, or otherwise.
-
The software is the result of research and development activities, therefore not necessarily quality assured and without the character of a liable product. For this reason, gematik does not provide any support or other user assistance (unless otherwise stated in individual cases and without justification of a legal obligation). Furthermore, there is no claim to further development and adaptation of the results to a more current state of the art.
-
-
Gematik may remove published results temporarily or permanently from the place of publication at any time without prior notice or justification.
-
Please note: Parts of this code may have been generated using AI-supported technology. Please take this into account, especially when troubleshooting, for security analyses and possible adjustments.
This software is currently being tested to ensure its technical quality and legal compliance. Your feedback is highly valued. If you find any issues or have any suggestions or comments, or if you see any other ways in which we can improve, please reach out to: [email protected].