value = $value; } } /** * Gets the value of this bit string. * * @return string bit string consisting only of 0-s and 1-s */ public function getValue() { return $this->value; } /** * Encodes the contents of this ASN1BitString as DER. * * @return array DER encoding of this ASN1BitString */ public function encodeDER() { $bytes = array(); $padding = 0; foreach (str_split($this->value, 8) as $byte) { while (strlen($byte) < 8) { $byte = '0' . $byte; $padding++; } $this->append($bytes, bindec($byte)); } $this->prepend($bytes, $padding); $this->prepend($bytes, ASN1DER::encodeLength(count($bytes))); $this->prepend($bytes, ASN1DER::encodeType(ASN1_TAG_BIT_STRING)); return $bytes; } /** * Decodes an ASN1BitString from the given byte stream. * * @throws GTException * @param array $bytes V bytes from the encoding of ASN1BitString TLV * @return void */ public function decodeDER($bytes) { $this->value = ''; $padding = $this->readByte($bytes); if ($padding < 0 || $padding > 7) { throw new GTException("Invalid ASN1BitString padding: {$padding}"); } while (count($bytes) > 0) { $byte = $this->readByte($bytes); $byte = decbin($byte); while (strlen($byte) < 8) { $byte = '0' . $byte; } $this->value .= $byte; } } } ?>