app/src/main/java/com/isode/stroke/stringcodecs/HMACSHA1.java
changeset 1049 41c9aa696059
parent 1048 cd41ebc93e78
child 1050 e7c2612c13b8
equal deleted inserted replaced
1048:cd41ebc93e78 1049:41c9aa696059
     1 /*
       
     2  * Copyright (c) 2010, Isode Limited, London, England.
       
     3  * All rights reserved.
       
     4  */
       
     5 /*
       
     6  * Copyright (c) 2010, Remko Tronçon.
       
     7  * All rights reserved.
       
     8  */
       
     9 package com.isode.stroke.stringcodecs;
       
    10 
       
    11 import com.isode.stroke.base.ByteArray;
       
    12 
       
    13 public class HMACSHA1 {
       
    14 
       
    15     private static final int B = 64;
       
    16 
       
    17     public static ByteArray getResult(ByteArray key, ByteArray data) {
       
    18         assert key.getSize() <= B;
       
    19 
       
    20         /* And an assert that does something */
       
    21         if (key.getSize() > B) {
       
    22             throw new IllegalStateException("Invalid key size.");
       
    23         }
       
    24 
       
    25         // Create the padded key
       
    26         ByteArray paddedKey = new ByteArray(key);
       
    27         for (int i = key.getSize(); i < B; ++i) {
       
    28             paddedKey.append((byte) 0x0);
       
    29         }
       
    30 
       
    31         // Create the first value
       
    32         ByteArray x = new ByteArray(paddedKey);
       
    33         byte[] xInner = x.getData();
       
    34         for (int i = 0; i < xInner.length; ++i) {
       
    35             xInner[i] ^= 0x36;
       
    36         }
       
    37         x.append(data);
       
    38 
       
    39         // Create the second value
       
    40         ByteArray y = new ByteArray(paddedKey);
       
    41         byte[] yInner = y.getData();
       
    42         for (int i = 0; i < yInner.length; ++i) {
       
    43             yInner[i] ^= 0x5c;
       
    44         }
       
    45         y.append(SHA1.getHash(x));
       
    46 
       
    47         return SHA1.getHash(y);
       
    48     }
       
    49 }