Mercurial > 510Connectbot
comparison app/src/main/java/ch/ethz/ssh2/crypto/digest/MAC.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/ch/ethz/ssh2/crypto/digest/MAC.java@4226f87534f4 |
children | 7953570e5210 |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
1 /* | |
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. | |
3 * Please refer to the LICENSE.txt for licensing details. | |
4 */ | |
5 package ch.ethz.ssh2.crypto.digest; | |
6 | |
7 import java.io.IOException; | |
8 import java.security.DigestException; | |
9 | |
10 /** | |
11 * MAC. | |
12 * | |
13 * @author Christian Plattner | |
14 * @version 2.50, 03/15/10 | |
15 */ | |
16 public final class MAC { | |
17 private Digest mac; | |
18 private int size; | |
19 | |
20 public static String[] getMacList() { | |
21 // Higher priority (stronger) first. Added SHA-2 algorithms as in RFC 6668 | |
22 return new String[] { | |
23 // "hmac-sha2-512", // fails interop w/ centos6 | |
24 "hmac-sha2-256", | |
25 "hmac-sha1", | |
26 "hmac-sha1-96", | |
27 "hmac-md5", | |
28 "hmac-md5-96" | |
29 }; | |
30 } | |
31 | |
32 public static void checkMacList(final String[] macs) { | |
33 for (String m : macs) { | |
34 getKeyLen(m); | |
35 } | |
36 } | |
37 | |
38 public static int getKeyLen(final String type) { | |
39 if (type.equals("hmac-sha1")) { | |
40 return 20; | |
41 } | |
42 | |
43 if (type.equals("hmac-sha1-96")) { | |
44 return 20; | |
45 } | |
46 | |
47 if (type.equals("hmac-md5")) { | |
48 return 16; | |
49 } | |
50 | |
51 if (type.equals("hmac-md5-96")) { | |
52 return 16; | |
53 } | |
54 | |
55 if (type.equals("hmac-sha2-256")) { | |
56 return 32; | |
57 } | |
58 | |
59 if (type.equals("hmac-sha2-512")) { | |
60 return 64; | |
61 } | |
62 | |
63 throw new IllegalArgumentException(String.format("Unknown algorithm %s", type)); | |
64 } | |
65 | |
66 public MAC(final String type, final byte[] key) throws DigestException { | |
67 if (type.equals("hmac-sha1")) { | |
68 mac = new HMAC(new SHA1(), key, 20); | |
69 } | |
70 else if (type.equals("hmac-sha1-96")) { | |
71 mac = new HMAC(new SHA1(), key, 12); | |
72 } | |
73 else if (type.equals("hmac-md5")) { | |
74 mac = new HMAC(new MD5(), key, 16); | |
75 } | |
76 else if (type.equals("hmac-md5-96")) { | |
77 mac = new HMAC(new MD5(), key, 12); | |
78 } | |
79 else if (type.equals("hmac-sha2-256")) { | |
80 mac = new HMAC(new SHA256(), key, 32); | |
81 } | |
82 else if (type.equals("hmac-sha2-512")) { | |
83 mac = new HMAC(new SHA512(), key, 64); | |
84 } | |
85 else { | |
86 throw new IllegalArgumentException(String.format("Unknown algorithm %s", type)); | |
87 } | |
88 | |
89 size = mac.getDigestLength(); | |
90 } | |
91 | |
92 public final void initMac(final int seq) { | |
93 mac.reset(); | |
94 mac.update((byte)(seq >> 24)); | |
95 mac.update((byte)(seq >> 16)); | |
96 mac.update((byte)(seq >> 8)); | |
97 mac.update((byte)(seq)); | |
98 } | |
99 | |
100 public final void update(byte[] packetdata, int off, int len) { | |
101 mac.update(packetdata, off, len); | |
102 } | |
103 | |
104 public final void getMac(byte[] out, int off) throws IOException { | |
105 try { | |
106 mac.digest(out, off); | |
107 } | |
108 catch (DigestException e) { | |
109 throw new IOException(e); | |
110 } | |
111 } | |
112 | |
113 public final int size() { | |
114 return size; | |
115 } | |
116 } |