Code Monkey home page Code Monkey logo

asn1parser's Introduction

ASN1Parser

tests coverage

Safety first ASN.1 parsing in Swift.

Documentation

You may import ASN1Parser.docc into Xcode, open the prebuilt static site found in docs/documentation/asn1parser/index.html, preview locally using preview-doc.sh or build the static website on your own using gen-doc.sh.

Note that the static site is sadly not hostable via github pages due to its case insensitive links which github pages routes as 404.

Quick Start

Given the a DER encoded ASN.1 representation of a public key, for example as found in .pem files:

// Public key data blob from pem file
let pemBlob = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEQPtmXeh4gkzq30Zq3LXdgcl39fgCOBRZExhNWgZTSv5NTvbRoZNx28Ln/+Wtkfc42nWdunurluAeMPr0BrnLtA=="
guard let derData = Data(base64Encoded: pemBlob) else { return }

// Parse DER into ASN.1 value, which is a tree like structure
let tree = try DERParser.parse(derData)

// Access values within the tree
let q = try tree.asSequence[1].asBitString

Feature Overview

  • runtime safe ASN.1 parsing - error handling enforced by the swift compiler
  • minimalistic - only contains the features you need
  • quick and easy to use - immediately start parsing DER or BER encoded ASN.1 without any boilerplate
  • implicit ASN.1 format specification - no need worry about explicitely specifying the expected data format. Simply access values in the ASN.1 tree and dynamically receive errors if they are not available.

Please note that the choice of implicit format specification is a deliberate one. I believe ASN.1 is not particularily suited to be 1:1 mapped to swift types. Therefore you will most likely have to introduce an abstraction layer, i.e., an explicit ASN.1 format specification in addition to the swift type defintion you'd ideally want to use throughout your remaining code. This abstraction layer is however unnecessary IMHO, as it only consists of boilerplate definitions and code for converting between the ASN.1 format and your swift type. In addition to the often wasteful and messy nature of boilerplate code, having two different abstractions for the same underlying concept can introduce inconsistencies and thus bugs.

With implicit ASN.1 format specification, you can access ASN.1 data safely and quicker while not having having to worry about introducing any accidental inconsistencies in your codebase, because an additional abstraction layer won't exist. Simply define your desired type in swift and dynamically load its values from the decoded ASN.1 representation.

Contributing

Pull requests are of course are highly appreciated :)

asn1parser's People

Contributors

dominikhorn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

asn1parser's Issues

swiftvalue availability

Hello,
The swiftvalues inside your objects (ASNInteger) are not available outside of your package. Would you mind to add an accessor like (BigIntValue) ?

I can do it if you will.
Stéphane.

ASN.1 Encoding

This package provides amazing Decoding support, but I'm struggling to find how to perform Encoding.

My use case is some CryptoKit.Curve25519.PublicKey data which I want to be encoded as DER, which unfortunately Apple's native API's can't do either (ref and ref).

Wouldn't it be quite straightforward to add toDER functions to all types to support this? I don't want to write all tags in my end-user codebase, but rather reuse the ones from a library such as this one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.