Defuse\Crypto\Encoding::saveBytesToChecksummedAsciiSafeString PHP Method

saveBytesToChecksummedAsciiSafeString() public static method

INTERNAL USE ONLY: Applies a version header, applies a checksum, and then encodes a byte string into a range of printable ASCII characters.
public static saveBytesToChecksummedAsciiSafeString ( string $header, string $bytes ) : string
$header string
$bytes string
return string
    public static function saveBytesToChecksummedAsciiSafeString($header, $bytes)
    {
        // Headers must be a constant length to prevent one type's header from
        // being a prefix of another type's header, leading to ambiguity.
        if (Core::ourStrlen($header) !== self::SERIALIZE_HEADER_BYTES) {
            throw new Ex\EnvironmentIsBrokenException('Header must be ' . self::SERIALIZE_HEADER_BYTES . ' bytes.');
        }
        return Encoding::binToHex($header . $bytes . \hash(self::CHECKSUM_HASH_ALGO, $header . $bytes, true));
    }

Usage Example

 /**
  * @expectedException \Defuse\Crypto\Exception\BadFormatException
  * @expectedExceptionMessage not a hex string
  */
 public function testBadHexEncoding()
 {
     $header = Core::secureRandom(Core::HEADER_VERSION_SIZE);
     $str = Encoding::saveBytesToChecksummedAsciiSafeString($header, Core::secureRandom(Core::KEY_BYTE_SIZE));
     $str[0] = 'Z';
     Encoding::loadBytesFromChecksummedAsciiSafeString($header, $str);
 }