-
-
Notifications
You must be signed in to change notification settings - Fork 646
Tutorial for ASN1 DER Primitive Encoder
ITU-T X.690 ASN.1 DER format is used for network protocol, digital certificate or signature format. The KJUR.asn1.DER* classes are used for hexadecimal encode of such ASN.1 DER objects.
KJUR.asn1 ASN.1 DER classes are very similar to ASN.1 classes of BouncyCastle JCE Library. If you know it you can learn KJUR.asn1 classes easily.
When you want to generate this data structure:
SEQUENCE {
INTEGER 3
UTF8String 'apple'
OBJECT IDENTIFIER serialNumber (2 5 4 5) }
you can get its hexadecimal encoded data by following code:
var seq =
new KJUR.asn1.DERSequence({'array':
[new KJUR.asn1.DERInteger({'int':3}),
new KJUR.asn1.DERUTF8String({'str':'apple'}),
new KJUR.asn1.DERObjectIdentifier({'oid':'2.5.4.5'})]});
var hDATA = seq.getEncodedHex(); // result will be 300f0201030c056170706c650603550405
ASN.1 primitive classes constructor may have a argument as initial parameters such like {'int': 3}. Superclass of the ASN.1 primitive class is KJUR.asn1.ASN1Object and it has getEncodedHex() method. It returns a hexadecimal encoded string of the ASN1Object.
ASN.1 has tag(T), length(L) and value(V) structure. For example, above hexadecimal data has following TLV structure.
[30][0f] - SEQUENCE len=0f
[02][01][03] - INTEGER len=1 Val=3
[0c][05][6170706c65] - UTF8STRING len=5 Val=apple
[06][03][550405] - OID len=3 Val=2.5.4.5