| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace ACES\Common\Salsa20;
- /**
- * FieldElement
- *
- *
- * SplFixedArray with more functions.
- *
- *
- * @author Devi Mandiri <devi.mandiri@gmail.com>
- * @link https://github.com/devi/Salt
- *
- */
- class FieldElement extends \SplFixedArray {
- public function toString() {
- $this->rewind();
- $buf = "";
- while ($this->valid()) {
- $buf .= chr($this->current());
- $this->next();
- }
- $this->rewind();
- return $buf;
- }
- public function toHex() {
- $this->rewind();
- $hextable = "0123456789ABCDEF";
- $buf = "";
- while ($this->valid()) {
- $c = $this->current();
- $buf .= $hextable[$c>>4];
- $buf .= $hextable[$c&0x0f];
- $this->next();
- }
- $this->rewind();
- return $buf;
- }
-
- // compatible to java
- public function toHexLowcase(){
- $this->rewind();
- $hextable = "0123456789abcdef";
- $buf = "";
- while ($this->valid()) {
- $c = $this->current();
- $buf .= $hextable[$c>>4];
- $buf .= $hextable[$c&0x0f];
- $this->next();
- }
- $this->rewind();
- return $buf;
- }
-
- public function toBase64() {
- return base64_encode($this->toString());
- }
- public function toJson() {
- return json_encode($this->toString());
- }
- public function slice($offset, $length = null) {
- $length = $length ? $length : $this->getSize()-$offset;
- $slice = new FieldElement($length);
- for ($i = 0;$i < $length;++$i) {
- $slice[$i] = $this->offsetGet($i+$offset);
- }
- return $slice;
- }
- public function copy($src, $size, $offset = 0, $srcOffset = 0) {
- for ($i = 0;$i < $size;++$i) {
- $this->offsetSet($i+$offset, $src[$i+$srcOffset]);
- }
- }
- public static function fromArray($array, $save_indexes = true) {
- $l = count($array);
- $fe = new FieldElement($l);
- $array = $save_indexes ? $array : array_values($array);
- foreach ($array as $k => $v) $fe[$k] = $v;
- return $fe;
- }
- public static function fromString($str) {
- return static::fromArray(unpack("C*", $str), false);
- }
- public static function fromHex($hex) {
- $hex = preg_replace('/[^0-9a-f]/', '', $hex);
- return static::fromString(pack("H*", $hex));
- }
- public static function fromBase64($base64) {
- return FieldElement::fromString(base64_decode($base64, true));
- }
- public static function fromJson($json) {
- return FieldElement::fromArray(json_decode($json, true));
- }
- }
|