Skip to content

Commit 791fb26

Browse files
authored
Merge pull request #64 from xp-forge/feature/encrypted
Add support for encrypted values
2 parents 97ab305 + c41fef2 commit 791fb26

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace com\mongodb;
2+
3+
class Encrypted {
4+
private $ciphertext;
5+
6+
public function __construct($ciphertext) {
7+
$this->ciphertext= (string)$ciphertext;
8+
}
9+
10+
/** @return int */
11+
public function length() { return strlen($this->ciphertext); }
12+
13+
/** @return string */
14+
public function ciphertext() { return $this->ciphertext; }
15+
16+
}

src/main/php/com/mongodb/io/BSON.class.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace com\mongodb\io;
22

33
use Traversable, StdClass;
4-
use com\mongodb\{ObjectId, Timestamp, Regex, Int64, Code, Decimal128, MinKey, MaxKey};
4+
use com\mongodb\{ObjectId, Timestamp, Regex, Int64, Code, Decimal128, MinKey, MaxKey, Encrypted};
55
use lang\{FormatException, IllegalArgumentException};
66
use util\{Bytes, Date, TimeZone, UUID};
77

@@ -58,6 +58,8 @@ public function bytes($name, $value) {
5858
return "\xff".$name."\x00";
5959
} else if ($value instanceof MaxKey) {
6060
return "\x7f".$name."\x00";
61+
} else if ($value instanceof Encrypted) {
62+
return "\x05".$name."\x00".pack('Vc', $value->length(), 6).$value->ciphertext();
6163
} else if ($value instanceof Traversable || $value instanceof StdClass) {
6264
return "\x03".$name."\x00".$this->sections($value);
6365
} else if (is_string($value)) {
@@ -132,6 +134,7 @@ public function value($name, $bytes, &$offset) {
132134
switch ($binary['subtype']) {
133135
case 0: case 2: return $value;
134136
case 3: case 4: return new UUID($value);
137+
case 6: return new Encrypted($value);
135138
default: throw new FormatException('Cannot handle binary subtype '.$binary['subtype']);
136139
}
137140
} else if ("\x07" === $kind) { // ObjectId

src/test/php/com/mongodb/unittest/BSONTest.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace com\mongodb\unittest;
22

33
use com\mongodb\io\BSON;
4-
use com\mongodb\{Code, Decimal128, Document, Int64, MaxKey, MinKey, ObjectId, Regex, Timestamp};
4+
use com\mongodb\{Code, Decimal128, Document, Int64, MaxKey, MinKey, ObjectId, Regex, Timestamp, Encrypted};
55
use lang\{FormatException, IllegalArgumentException};
66
use test\{Assert, Expect, Test, Values};
77
use util\{Bytes, Date, UUID};
@@ -60,6 +60,7 @@ private function values() {
6060

6161
// Special types
6262
yield [new Bytes('abc'), "\x05test\x00\x03\x00\x00\x00\x00abc"];
63+
yield [new Encrypted('ciphertext'), "\x05test\x00\x0a\x00\x00\x00\x06ciphertext"];
6364
yield [new Date('2020-08-03 17:41 +0200'), "\x09test\x00\xe0\xae\xfb\xb4\x73\x01\x00\x00"];
6465
yield [new Date('1969-08-03 17:41 +0200'), "\x09test\x00\xe0\x3e\xbd\xf9\xfc\xff\xff\xff"];
6566
yield [new Timestamp(1596543032, 1), "\x11test\x00\x01\x00\x00\x00\x38\x50\x29\x5f"];

0 commit comments

Comments
 (0)