All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.spongycastle.jcajce.io.MacOutputStream Maven / Gradle / Ivy

Go to download

Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for the Android platform. Android unfortunately ships with a stripped-down version of Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full, up-to-date version of the Bouncy Castle cryptographic libs.

The newest version!
package org.spongycastle.jcajce.io;

import java.io.IOException;
import java.io.OutputStream;

import javax.crypto.Mac;

/**
 * An output stream which calculates a MAC based on the data that is written to it.
 */
public final class MacOutputStream
    extends OutputStream
{
    private Mac mac;

    /**
     * Base constructor - specify the MAC algorithm to use.
     *
     * @param mac the MAC implementation to use as the basis of the stream.
     */
    public MacOutputStream(
        Mac mac)
    {
        this.mac = mac;
    }

    /**
     * Write a single byte to the stream.
     *
     * @param b the byte value to write.
     * @throws IOException  in case of failure.
     */
    public void write(int b)
        throws IOException
    {
        mac.update((byte)b);
    }

    /**
     * Write a block of data of length len starting at offset off in the byte array bytes to
     * the stream.
     *
     * @param bytes byte array holding the data.
     * @param off offset into bytes that the data starts at.
     * @param len the length of the data block to write.
     * @throws IOException in case of failure.
     */
    public void write(
        byte[] bytes,
        int off,
        int len)
        throws IOException
    {
        mac.update(bytes, off, len);
    }

    /**
     * Execute doFinal() and return the calculated MAC.
     *
     * @return the MAC calculated from the output written to the stream.
     */
    public byte[] getMac()
    {
        return mac.doFinal();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy