Home | History | Annotate | Download | only in func
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.apache.harmony.crypto.tests.javax.crypto.func;
     17 
     18 import javax.crypto.Mac;
     19 import javax.crypto.spec.SecretKeySpec;
     20 
     21 public class MacThread extends TestThread {
     22     MacThread(String[] names) {
     23         super(names);
     24     }
     25 
     26     @Override
     27     public void test() throws Exception {
     28         int size = 256;
     29         byte[] src1 = new byte[size];
     30         byte[] src2 = new byte[size];
     31         byte[] src3 = new byte[size];
     32         int i;
     33 
     34         for (i = 0; i < size; i++) {
     35             src1[i] = (byte)i;
     36             src2[i] = (byte)i;
     37             src3[i] = (byte)(size - i - 1);
     38         }
     39         Mac m = Mac.getInstance(algName);
     40         byte[] b = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
     41         SecretKeySpec sks = new SecretKeySpec(b, "SHA1");
     42         m.init(sks);
     43 
     44         byte[] res = m.doFinal(src1);
     45         String sign1 = new String(res);
     46         m.init(sks);
     47         res = m.doFinal(src2);
     48         String sign2 = new String(res);
     49         m.init(sks);
     50         res = m.doFinal(src3);
     51         String sign3 = new String(res);
     52         if (sign1.compareTo(sign2) != 0 || sign1.compareTo(sign3) == 0 ||
     53                 sign2.compareTo(sign3) == 0) {
     54             throw new Exception ("Signature is not correct for algorithm " + algName);
     55         }
     56     }
     57 }
     58