BER encoding in PHP

From splike.com
Jump to: navigation, search

Microsoft requires some AD/LDAP values, like unicodePwd, to be "BER-encoded". I couldn't find a PHP implementation of BER encoding, so here is one. But I don't think it really follows the the Basic Encoding Rules specification at all. It's reverse-engineered to match another tool, and it seems to work:

// BER-encoding to work with Microsoft Active Directory, probably not real BER-encoding
//   http://splike.com/wiki/BER_encoding_in_PHP
function ber_encode($string) {
	$null_padded_string = '';
	for($i = 0; $i<strlen($string); $i++) {
		$null_padded_string .= substr($string,$i,1)."\0";
	}
	return base64_encode($null_padded_string);
}