Java 类javax.crypto.MacSpi 实例源码

项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>getMacLength()</code> method
 * Assertion: return Mac length
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        notes = "",
        method = "getMacLength",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        notes = "",
        clazz = MacSpi.class,
        method = "engineGetMacLength",
        args = {}
    )
})
public void testGetMacLength() {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    for (int i = 0; i < macs.length; i++) {
        assertTrue("Length should be positive", (macs[i].getMacLength() >= 0));
    }
}
项目:crypto-gwt    文件:MacSpiFactory.java   
public MacSpi create(Object constructorParam) {
    try {
        return Adaptor.adapt(type.newInstance(), MacSpi.class);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid type: " + type, e);
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>doFinal(byte[] output, int outOffset)</code> and
 * <code>doFinal()</code> methods Assertion: Mac result is stored in
 * output buffer
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "doFinal",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineDoFinal",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "doFinal",
        args = {byte[].class, int.class}
    )
})
public void testMac11() throws NoSuchAlgorithmException, NoSuchProviderException,
        IllegalArgumentException, IllegalStateException,
        InvalidKeyException, ShortBufferException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(scs);
        byte [] res1 = macs[i].doFinal();
        byte [] res2 = new byte[res1.length + 10];
        macs[i].doFinal(res2, 0);
        for (int j = 0; j < res1.length; j++) {
            assertEquals("Not equals byte number: "
                    .concat(Integer.toString(j)), res1[j], res2[j]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>doFinal(byte[] input)</code> method
 * Assertion: update Mac and returns result
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "doFinal",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineDoFinal",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "doFinal",
        args = {byte[].class}
    )
})
public void testMac12() throws NoSuchAlgorithmException, NoSuchProviderException,
        IllegalArgumentException, IllegalStateException,
        InvalidKeyException  {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    byte [] upd = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1, (byte)0};
    SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(scs);
        byte [] res1 = macs[i].doFinal();
        byte [] res2 = macs[i].doFinal();
        assertEquals("Results are not the same", res1.length, res2.length);
        for(int t = 0; t < res1.length; t++) {
            assertEquals("Results are not the same", res1[t], res2[t]);
        }
        res2 = macs[i].doFinal(upd);
        macs[i].update(upd);
        res1 = macs[i].doFinal();
        assertEquals("Results are not the same", res1.length, res2.length);
        for(int t = 0; t < res1.length; t++) {
            assertEquals("Results are not the same", res1[t], res2[t]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>update(byte[] input, int outset, int len)</code> and
 * <code>update(byte[] input</code>
 * methods
 * Assertion: updates Mac
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "update",
        args = {byte.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineUpdate",
        args = {byte.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality.",
        method = "update",
        args = {byte[].class, int.class, int.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineUpdate",
        args = {byte[].class, int.class, int.class}
    )
})
public void testMac14() throws NoSuchAlgorithmException,
        NoSuchProviderException, IllegalArgumentException, IllegalStateException,
        InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    byte [] upd1 = {(byte)0, (byte)1, (byte)5, (byte)4, (byte)3, (byte)2};
    byte [] upd2 = {(byte)5, (byte)4, (byte)3, (byte)2};
    byte [] res1;
    byte [] res2;
    SecretKeySpec scs = new SecretKeySpec(b, "SHA1");
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(scs);
        macs[i].update(upd1, 2, 4);
        res1 = macs[i].doFinal();
        macs[i].init(scs);
        macs[i].update(upd2);
        res2 = macs[i].doFinal();
        assertEquals("Results are not the same", res1.length, res2.length);
        for(int t = 0; t < res1.length; t++) {
            assertEquals("Results are not the same", res1[t], res2[t]);
        }
        macs[i].init(scs);
        macs[i].update((byte)5);
        res1 = macs[i].doFinal();
        macs[i].init(scs);
        macs[i].update(upd1,2,1);
        res2 = macs[i].doFinal();
        assertEquals("Results are not the same", res1.length, res2.length);
        for(int t = 0; t < res1.length; t++) {
            assertEquals("Results are not the same", res1[t], res2[t]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>update(ByteBuffer input)</code>
 * <code>update(byte[] input, int offset, int len)</code>
 * methods
 * Assertion: processes Mac; if input is null then do nothing
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality",
        method = "update",
        args = {byte[].class, int.class, int.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineUpdate",
        args = {byte[].class, int.class, int.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality",
        method = "update",
        args = {java.nio.ByteBuffer.class}
    )
})
public void testUpdateByteBuffer01() throws NoSuchAlgorithmException, NoSuchProviderException,
        IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException,
        InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
    SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
    ByteBuffer byteNull = null;
    ByteBuffer byteBuff = ByteBuffer.allocate(0);
    byte [] bb1;
    byte [] bb2;
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(sks);
        bb1 = macs[i].doFinal();
        try {
            macs[i].update(byteNull);
            fail("IllegalArgumentException must be thrown because buffer is null");
        } catch (IllegalArgumentException e) {
        }
        macs[i].update(byteBuff);
        bb2 = macs[i].doFinal();
        for (int t = 0; t < bb1.length; t++) {
            assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
        }
        macs[i].init(sks);
        bb1 = macs[i].doFinal();
        macs[i].update(null, 0, 0);
        bb2 = macs[i].doFinal();
        for (int t = 0; t < bb1.length; t++) {
            assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>update(ByteBuffer input)</code>
 * <code>update(byte[] input, int offset, int len)</code>
 * methods
 * Assertion: processes Mac
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality",
        method = "update",
        args = {java.nio.ByteBuffer.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks functionality",
        method = "update",
        args = {byte[].class, int.class, int.class}
    ),
    @TestTargetNew(
        level = TestLevel.PARTIAL_COMPLETE,
        notes = "Checks IllegalStateException only but for all methods. Not enough for doFinal(byte[] output, int outOffset) - it can throw ShortBufferException",
        clazz = MacSpi.class,
        method = "engineUpdate",
        args = {byte[].class, int.class, int.class}
    )
})
public void testUpdateByteBuffer02() throws NoSuchAlgorithmException, NoSuchProviderException,
        IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException,
        InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
    SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
    byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1};
    ByteBuffer byteBuf;
    byte [] bb1;
    byte [] bb2;
    for (int i = 0; i < macs.length; i++) {
        byteBuf = ByteBuffer.allocate(5);
        byteBuf.put(bbuf);
        byteBuf.position(2);
        macs[i].init(sks);
        macs[i].update(byteBuf);
        bb1 = macs[i].doFinal();

        macs[i].init(sks);
        macs[i].update(bbuf, 2, 3);
        bb2 = macs[i].doFinal();
        for (int t = 0; t < bb1.length; t++) {
            assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
/**
 * Test for <code>reset()</code> method
 * Assertion: return Mac length
 */
@TestTargets({
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        notes = "",
        method = "reset",
        args = {}
    ),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        notes = "",
        clazz = MacSpi.class,
        method = "engineReset",
        args = {}
    )
})
public void testReset() throws InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac [] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte [] bb = {(byte)1, (byte)2, (byte)3, (byte)4, (byte)5};
    SecretKeySpec sks = new SecretKeySpec(bb, "SHA1");
    byte [] bbuf = {(byte)5, (byte)4, (byte)3, (byte)2, (byte)1};
    byte [] bb1;
    byte [] bb2;
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(sks);
        bb1 = macs[i].doFinal();
        macs[i].reset();
        bb2 = macs[i].doFinal();
        assertEquals("incorrect result",bb1.length, bb2.length);
        for (int t = 0; t < bb1.length; t++) {
           assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
        }
        macs[i].reset();
        macs[i].update(bbuf);
        bb1 = macs[i].doFinal();
        macs[i].reset();
        macs[i].update(bbuf, 0, bbuf.length);
        bb2 = macs[i].doFinal();
        assertEquals("incorrect result",bb1.length, bb2.length);
        for (int t = 0; t < bb1.length; t++) {
           assertEquals("Incorrect doFinal result", bb1[t], bb2[t]);
        }
    }
}
项目:In-the-Box-Fork    文件:MacTest.java   
protected Mock_Mac(MacSpi arg0, Provider arg1, String arg2) {
    super(arg0, arg1, arg2);
}
项目:In-the-Box-Fork    文件:MacTest.java   
public myMac(MacSpi macSpi, Provider provider,
        String algorithm) {
    super(macSpi, provider, algorithm);
}
项目:crypto-gwt    文件:MacSpiFactory.java   
public MacSpiFactory(Class<? extends javax.crypto.MacSpi> type) {
    this.type = type;
}
项目:cn1    文件:MacTest.java   
public myMac(MacSpi macSpi, Provider provider,
        String algorithm) {
    super(macSpi, provider, algorithm);
}
项目:freeVM    文件:Mac.java   
/** @ar.org.fitc.spec_ref */
protected Mac(MacSpi macSpi, Provider provider, String algorithm) {
    this.macSpi = macSpi;
    this.provider = provider;
    this.algorithm = algorithm;
}
项目:freeVM    文件:Mac.java   
/** @ar.org.fitc.spec_ref */
public final Object clone() throws CloneNotSupportedException {
    return new Mac((MacSpi) macSpi.clone(), provider, algorithm);
}
项目:freeVM    文件:MacTest.java   
public myMac(MacSpi macSpi, Provider provider,
        String algorithm) {
    super(macSpi, provider, algorithm);
}
项目:freeVM    文件:MacTest.java   
public myMac(MacSpi macSpi, Provider provider,
        String algorithm) {
    super(macSpi, provider, algorithm);
}
项目:freeVM    文件:Mac.java   
/**
 * It's used as a factory method
 * 
 * @param algorithm
 *            the algorithm
 * @param provider
 *            the provider
 * @return A new Mac object
 * @throws NoSuchAlgorithmException
 *             If the specified algorithm is not available in the default
 *             package or any of the others providers that were searched
 */
private static final Mac newInstance(String algorithm, Provider provider)
        throws NoSuchAlgorithmException {
    Service service = Util.getService(Types.MAC, algorithm, provider);

    if (service == null) {
        throw new NoSuchAlgorithmException("No such algorithm: "
                + algorithm);
    }
    return new Mac((MacSpi) service.newInstance(null), 
            service.getProvider(), algorithm);
}