static public function clampBits($unsafeSecret, $bytes = 64)
    {
        if ($unsafeSecret instanceof Buffer) {
            // copy-construct to avoid malformed and wrong size
            $toBuffer = new Buffer($unsafeSecret->getBinary(), $bytes);
        }
        elseif (! ctype_xdigit($unsafeSecret)) {
            // build from binary
            $toBuffer = new Buffer($unsafeSecret, $bytes);
        }
        else {
            $toBuffer = Buffer::fromHex($unsafeSecret, $bytes);
        }

        // clamping bits
        $clampSecret  = $toBuffer->toUInt8();
        $clampSecret[0] &= 0xf8; // 248
        $clampSecret[31] &= 0x7f; // 127
        $clampSecret[31] |= 0x40; // 64

        // build Buffer object from UInt8 and return byte-level representation
        $encoder = new Encoder;
        $safeSecret = $encoder->ua2bin($clampSecret);
        return $safeSecret;
    }