https://github.com/kizzycode/asn1der-swift
`Asn1Der` is a basic ASN.1-DER implementation that offers simple de-/encoding support for some basic types
https://github.com/kizzycode/asn1der-swift
Last synced: about 2 months ago
JSON representation
`Asn1Der` is a basic ASN.1-DER implementation that offers simple de-/encoding support for some basic types
- Host: GitHub
- URL: https://github.com/kizzycode/asn1der-swift
- Owner: KizzyCode
- Created: 2020-04-25T13:37:54.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-09T14:42:10.000Z (over 4 years ago)
- Last Synced: 2025-02-06T04:44:17.539Z (3 months ago)
- Language: Swift
- Size: 104 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE BSD 2-CLAUSE.md
Awesome Lists containing this project
README
[](https://opensource.org/licenses/BSD-2-Clause)
[](https://opensource.org/licenses/MIT)
[](https://travis-ci.org/KizzyCode/asn1der-swift)# Asn1Der
Welcome to `Asn1Der` 🎉
`Asn1Der` is a basic ASN.1-DER implementation that offers simple de-/encoding support for some basic types:
- The `ASN.1-BOOLEAN` type as `DERBoolean` object and Swift's `Bool` type
- The `ASN.1-INTEGER` type as `DERInteger` object and Swift's `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64` types
- The `ASN.1-NULL` type as `DERNull` object and Swift's `Optional.none` type (which allows the encoding of optional elements)
- The `ASN.1-OctetString` type as `DEROctetString` object and Swift's `Data` type
- The `ASN.1-Sequence` type as `DERSequence` object and Swift's `Array` type
- The `ASN.1-UTF8String` type as `DERUTF8String` object and Swift's `String` type## Example
```swift
// Declare an encoded integer with value `7`
let encodedInt = Data([0x02, 0x01, 0x07])// Decode a generic DER object
let object = try DERAny(decode: encodedInt)// Reencode the object
let reencodedObject = object.encode()
XCTAssertEqual(reencodedObject, encodedInt)// Decode an UInt32
let uint = try UInt32(decode: encodedInt)
XCTAssertEqual(uint, 7)// Reencode the integer
let reencodedInt = uint.encode()
XCTAssertEqual(reencodedInt, encodedInt)// Decode a `RawRepresentable` enum
// swiftlint:disable nesting
enum TestEnum: String, Codable {
case variantA = "Variant A", variantB = "Variant B"
}
let encodedTestEnum = Data([0x0c, 0x09, 0x56, 0x61, 0x72, 0x69, 0x61, 0x6E, 0x74, 0x20, 0x41])// Decode the enum
let testEnum: TestEnum = try DERDecoder().decode(from: encodedTestEnum)
XCTAssertEqual(testEnum, .variantA)// Reencode the enum
let reencodedTestEnum = try DEREncoder().encode(testEnum)
XCTAssertEqual(reencodedTestEnum, encodedTestEnum)
```## Support for `Decode`/`Encode`
This crate now has experimental support for `Codable` (aka `Encode & Decode`) 🥳