Created
February 18, 2019 21:39
-
-
Save ateska/95cbbe2a65e77d4024348b0f187cdc14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| func asn1_int_to_bytes(length: UInt) -> Data { | |
| var l = length | |
| var d = Data() | |
| while (l > 0) { | |
| let b: UInt8 = UInt8(l & 0xFF) | |
| d.append(contentsOf: [b]) | |
| l = length >> 8 | |
| } | |
| return d | |
| } | |
| func asn1_il(tag: UInt8, length: UInt) -> Data { | |
| if (length < 128) { | |
| return Data([tag, UInt8(length)]) | |
| } | |
| let d = asn1_int_to_bytes(length:length) | |
| return Data([tag, 0x80 | UInt8(d.count)]) + d | |
| } | |
| func asn1_UTF8String(value: String) -> Data { | |
| let b = value.data(using: .utf8)! | |
| return asn1_il(tag:12, length:UInt(b.count)) + b | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment