1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.support;
24
25import java.security.InvalidAlgorithmParameterException;
26import java.security.InvalidKeyException;
27import java.security.Key;
28import java.security.spec.AlgorithmParameterSpec;
29
30import javax.crypto.MacSpi;
31import javax.crypto.spec.SecretKeySpec;
32
33/**
34 * Additional class for verification of MacGeneratorSpi and MacSpi
35 *
36 */
37
38public class MyMacSpi extends MacSpi {
39
40    private int length = 0;
41    @Override
42    protected int engineGetMacLength() {
43        return length;
44    }
45
46    @Override
47    protected void engineInit(Key key, AlgorithmParameterSpec params)
48            throws InvalidKeyException, InvalidAlgorithmParameterException {
49        if (params == null) {
50            if (!(key instanceof SecretKeySpec)) {
51                throw new IllegalArgumentException("params is null and key is SecretKeySpec");
52            }
53        }
54    }
55
56    @Override
57    protected void engineUpdate(byte input) {
58    }
59
60    @Override
61    protected void engineUpdate(byte[] input, int offset, int len) {
62        if (offset >= 0 && len >= 0) {
63            length = len;
64        }
65    }
66
67    @Override
68    protected byte[] engineDoFinal() {
69        return new byte[length];
70    }
71
72    @Override
73    protected void engineReset() {
74        length++;
75    }
76}
77