ByteArray class

An instance of the ByteArray class represents a sequence of 8-bit bytes (as opposed to Unicode characters).

Constructors

ByteArray ( length : Number ) : ByteArray

Constructs a new byte array with the specified number of bytes and clears all bytes to zero.

An exception is thrown if the specified length is negative or if it is not an integer.

ByteArray ( part1 : ByteArray, part2 : ByteArray ) : ByteArray

Constructs a new byte array by concatenating the two specified parts.

ByteArray ( source : String, codec : String ) : ByteArray

Constructs a new byte array by converting a source string with the specified codec; see text encoding.

Properties

length : Number
size : Number

The number of bytes in the byte array. This value is read-only.

Member functions

getAt( index : Number ) : Number

Returns the value of the byte at the specified zero-based index in the byte array. The returned value is an integer in the range [0..255]. An exception is thrown if the index is out of range or if it is not an integer.

putAt( index : Number, value : Number )

Stores a new value for the byte at the specified zero-based index in the byte array. The specified value must be an integer in the range [0..255]. An exception is thrown if the value or the index are out of range, or if they are not integers.

getSubArray ( start : Number, length : Number ) : ByteArray

Returns the specified portion of the byte array (zero-based start index). An exception is thrown if the requested portion is out of range, or if start and length are not integers.

toString (codec : String ) : String

Returns the string obtained by converting the byte array using the specified codec, text encoding.

convertTo (codec : String ) : ByteArray

Returns a new ByteArray which is the re-encoded version of the current one. Supported codecs are Hex and Base64. This function recodes the entire ByteArray even if the data includes null bytes.

Example: ByteArray (‘abc', ‘UTF-8').convertTo(‘Hex') returns a ByteArray of length 6 with bytes 54 49 54 50 54 51.

Signatures

The ByteArray class offers the following additional functions to generate and verify signatures using standard RSA algorithms (compatible with PKCS#1 v2.0).

generateSignature( privateKeyPath : String ) : ByteArray

Returns the digital signature for the data in the receiving ByteArray using the specified private key. This signature allows a receiver to verify that the data was indeed generated by a script that has access to the specified private key (assuming that the receiver has access to the public key corresponding to the private key).

The privateKeyPath argument specifies the path to a private key file in the standard PKCS #8 format. Since this format is not encrypted or password protected, the user must provide other means to safeguard the private key from inappropriate access.

If the privateKeyPath argument is missing or null, the private key built into Switch is used instead. The resulting signature allows a receiver to verify that the data was indeed generated by a script running in Switch (since the public key corresponding to the private key is part of the public Switch documentation).

verifySignature( signature : ByteArray, publicKeyPath : String ) : Boolean

Verifies whether the specified digital signature fits the data in the receiving ByteArray using the specified public key. If this function returns true, the data was indeed generated by a sender with access to the private key corresponding to the specified public key (with the certainty level offered by PKCS#1 v2.0).

The publicKeyPath argument specifies the path to a public key file in the standard Base64 encoded X.509 format (also called PEM).

If the publicKeyPath argument is missing or null, the public key corresponding to the private key built into Switch is used instead. This avoids including an explicit reference to the public key which is part of the public Switch documentation.

Example: the following code would generate a signature for authenticating Switch with the Alwan CMYK Optimizer CLI:

var strData = "2008-10-12T14:16:22+01:00/var/tmp/foo.pdf/var/tmp/bar.pdf"; 
var binData = new ByteArray(strData, "UTF-8"); 
var binSignature = binData.generateSignature(); 
var strSignature = binSignature.toString("toBase64");

Public Key in C++ syntax:

-----BEGIN CERTIFICATE-----
MIIBgTCCASugAwIBAgIJAN+2brjXZa75MA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNV
BAYTAkJZMB4XDTA5MDUxMjA4MDAwMVoXDTA5MDYxMTA4MDAwMVowDTELMAkGA1UE
BhMCQlkwXDANBgkqhkiG9w0BAQEFAANLADBIAkEAqm5CNFaMI/Jc7++ySmLdVb9W
Y/C+l5Zt1qr8v4qObYZOgpuwucDh9QzC7wgWnCWUINs9xgcU9p6WwrSFcyAbqQID
AQABo24wbDAdBgNVHQ4EFgQUTFkQhg1pkDGONt6MFc2zF+L2vRswPQYDVR0jBDYw
NIAUTFkQhg1pkDGONt6MFc2zF+L2vRuhEaQPMA0xCzAJBgNVBAYTAkJZggkA37Zu
uNdlrvkwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAANBAKjV5bqRnkgufLyS
ddAIwhyvWa5nvVbobF26cEE+7jkC+fspsSvnJDLIW72zCKFEqtnBtEM4uCweYnDV
ODWBKjg=
-----END CERTIFICATE-----