18. Utils
The utils object is a built-in object that provides utility functions.
Functional overview:
- Supports UTF-8 encoding/decoding.
- Provides the ability to parse X.509 certificates.
utils Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
utils.stringToUtf8bin() | Encodes string data in UTF-8. | ||
utils.utf8binToString() | Decodes binary data in UTF-8. | ||
utils.getX509CertInfo() | Gets the X.509 certificate information. | 02.02.00+ |
Details
utils.stringToUtf8bin(data)
Encodes string data in UTF-8.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
data | string | mandatory | String data | |
return | ArrayBuffer | - | Encoded binary data | When an error occurs, an exception is raised. |
utils.utf8binToString(data[,offset[,length]])
Decodes binary data in UTF-8.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
data | ArrayBuffer, Array[ArrayBuffer] | mandatory | Binary data'ArrayBuffer' or Array of 'ArrayBuffer' can be specified.If Array of 'ArrayBuffer' is specified, concatenates the binary data in the order of the array, then starts the decoding process. | |
offset | number | optional | Offset of decode processing start position If array of 'ArrayBuffer' is specified for 'data', it will be an offset over all 'data' after the concatenation of the binary data.The default value is 0. | If 'length' is used, it cannot be omitted. |
length | number | optional | Decode length [byte] The default value is calculated by subtracting the decode start position offset from the total size of 'data'. A size less than or equal to the default value must be specified. | It can be omitted even if 'offset' is used. |
return | {DecodedData}, undefined | - | {DecodedData} : Decoded data If decoding fails, 'undefined' is returned. | When an error occurs, an exception is raised. |
{DecodedData}
Name | Type | Summary | Note |
---|---|---|---|
.str | string | Decoded string data | |
.bin | ArrayBuffer | When leftover data from 1 to 3 bytes exists at the end of the binary data to be decoded, the leftover data portion is output as binary data. If leftover data does not exist, it will be 'undefined'. |
utils.getX509CertInfo(cert)
Gets the X.509 certificate information.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
cert | string | mandatory | X.509 certificate Specify a string in PEM format. Use \n for line feed code. | |
return | {CertInfo}, null | - | {CertInfo} : X.509 certificate information In case of a parsing error, 'null' is returned. |
{CertInfo}
Name | Type | Summary | Note |
---|---|---|---|
.certVersion | number | Version number | |
.serialNumber | string | Serial number | |
.issuerName | string | Issuer name | |
.subjectName | string | Subject name | |
.issuedOn | number | Issue date (unixtime[ms]) | |
.expiresOn | number | Expiration date (unixtime[ms]) | |
.signedUsing | string | Signature algorithm | |
.keySize | number | Public key size | |
.basicConstraints | string | Basic constraints | |
.subjectAltName | string | Subject alternative name | |
.certType | string | Certificate type | |
.keyUsage | string | Key usage | |
.extKeyUsage | string | Extended key usage | |
.certificatePolicies | string | Certificate policies |
Object Usage Examples
Sample 1
This is a sample of split decoding using offset for data encoded in UTF-8.
This example utilizes the offset and length of .utf8binToString().
The possibility of splitting multi-byte characters without prior consideration of their cutoff points is shown.
var testStr_1 = 'Thank you Teşekkür ありがとう';
print(testStr_1);
var testEnc_1 = utils.stringToUtf8bin(testStr_1);
print(ab2Hex_00(testEnc_1));
var chunk = 7;
for(var n = 0; n < 20; n++) {
var testDec_1 = utils.utf8binToString(testEnc_1, 0, chunk + n);
print(('00' + n).substr(-2) + '_1:' + testDec_1['str']);
//if(testDec_1['bin']) print(ab2Hex_00(testDec_1['bin']));
var rest = testDec_1['bin'];
var len = 0;
if(rest) len = rest.byteLength;
var testDec_2 = utils.utf8binToString(testEnc_1, chunk + n - len);
print(('00' + n).substr(-2) + '_2:' + testDec_2['str']);
//if(testDec_2['bin']) print(ab2Hex_00(testDec_2['bin']));
}
function ab2Hex_00(ab) {
ret_str = '';
var a = new Uint8Array(ab);
for(var i = 0; i < a.length; i++) {
ret_str += ('00' + a[i].toString(16).toUpperCase()).substr(-2);
}
return ret_str;
}
Sample 2
This is a sample that splits the UTF-8 encoded data into two parts and decodes them individually.
This example can be utilized when the decoding target is stream data.
The possibility of splitting multi-byte characters without prior consideration of their cutoff points is shown.
var testStr_1 = 'Thank you Teşekkür ありがとう';
print(testStr_1);
var testEnc_1 = utils.stringToUtf8bin(testStr_1);
print(ab2Hex_00(testEnc_1));
var chunk = 7;
for(var n = 0; n < 20; n++) {
var chunk_1 = testEnc_1.slice(0, chunk + n);
var chunk_2 = testEnc_1.slice(chunk + n);
var testDec_1 = utils.utf8binToString(chunk_1);
print(('00' + n).substr(-2) + '_1:' + testDec_1['str']);
//if(testDec_1['bin']) print(ab2Hex_00(testDec_1['bin']));
var rest = testDec_1['bin'];
var testDec_2 = utils.utf8binToString([rest, chunk_2]);
print(('00' + n).substr(-2) + '_2:' + testDec_2['str']);
//if(testDec_2['bin']) print(ab2Hex_00(testDec_2['bin']));
}
function ab2Hex_00(ab) {
ret_str = '';
var a = new Uint8Array(ab);
for(var i = 0; i < a.length; i++) {
ret_str += ('00' + a[i].toString(16).toUpperCase()).substr(-2);
}
return ret_str;
}
Sample 3
This is a sample to get X.509 certificate information.
var ca = '-----BEGIN CERTIFICATE-----\n' +
'SIGD0jCCArqgAwIBAgIBATANBgkqhkiG9w0BAQUFADB6MQswCQYDVQQGEwJKUDEO\n' +
'MAwGA1UECAwFSXdhdGUxFTATBgNVBAoMDE1vYmljb21tIEx0ZDEcMBoGA1UEAwwT\n' +
//........
'HvMvDtFO71gaEtIkpejHGLgGhQK1dQ==\n' +
'-----END CERTIFICATE-----\n';
var info = utils.getX509CertInfo(ca);
if(info) {
for(var key in info) {
if(key == 'issuedOn' || key == 'expiresOn') {
var date = new Date(info[key]);
print(key + ':', date.toString());
} else {
print(key + ':', info[key]);
}
}
} else print("Cert parse error");