private void finish() throws IOException {
		write(0xD3);
		write(0x14);
		byte[] md = hash.digest();

		if (BUFFERSIZE - ciphpos >= md.length) {
			// enough space in buffer for the hash
			try {
				pos += cipher.doFinal(md, 0, md.length, buf, pos);
			} catch (ShortBufferException | IllegalBlockSizeException
					| BadPaddingException e) {
			}
		} else {
			// not enough space in buffer for the hash
			int len = md.length;
			int off = 0;

			int tlen = BUFFERSIZE - ciphpos;
			try {
				pos += cipher.update(md, off, tlen, buf, pos);
			} catch (ShortBufferException e) {
			}
			ciphpos += tlen;
			len -= tlen;
			off += tlen;

			out.write(PARTIAL_BUFFERSIZE);
			out.write(version);
			out.write(buf, 0, BUFFERSIZE - 1);
			version = buf[BUFFERSIZE - 1];
			pos = 0;
			ciphpos = 0;

			// write out the rest
			try {
				pos += cipher.doFinal(md, off, len, buf, pos);
			} catch (ShortBufferException | IllegalBlockSizeException
					| BadPaddingException e) {
			}
		}
	}