public function createBitArrayObj() { $self = $this; $Bitarray = new BitArray(); $Bitarray->addList($self); return $Bitarray; }
/** * A fast method to retrieve one row of data from the matrix as a BitArray. * * @param $y; The row to retrieve * @param $row; An optional caller-allocated BitArray, will be allocated if null or too small * @return The resulting BitArray - this reference should always be used even when passing * your own row */ public function getRow($y, $row) { if ($row == null || $row->getSize() < $this->width) { $row = new BitArray($this->width); } else { $row->clear(); } $offset = $y * $this->rowSize; for ($x = 0; $x < $this->rowSize; $x++) { $row->setBulk($x * 32, $this->bits[$offset + $x]); } return $row; }
public function merge(BitArray $bitArray2) { return new self(array_merge($this->bits, $bitArray2->toArray())); }
/** * Convert a list of words to a byte string * that has the same binary representation with a checksum * @param array $words * @return string */ public function toEntropy(array $words) { // collect up words, compact into bit array $tmp = new BitArray([]); for ($i = 0; $i < count($words); $i++) { $wordIndex = array_search($words[$i], $this->words); for ($j = 0; $j < 11; $j++) { $tmp[$i * 11 + $j] = ($wordIndex & 1 << 10 - $j) != 0; } } $concatLenBits = count($tmp); $checksumLengthBits = $concatLenBits / 33; $entropyLengthBits = $concatLenBits - $checksumLengthBits; $entropy = $tmp->slice(0, $entropyLengthBits)->toBytes(); $hashBits = new BitArray(hash("sha256", $entropy, true)); $checksum = $tmp->slice($entropyLengthBits, $checksumLengthBits); if ($checksum != $hashBits->slice(0, $checksumLengthBits)) { throw new \RuntimeException("Invalid checksum"); } return $entropy; }