1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.kerberos.shared.io.decoder;
21
22
23 import java.util.Enumeration;
24
25 import org.apache.directory.server.kerberos.shared.crypto.checksum.ChecksumType;
26 import org.apache.directory.server.kerberos.shared.messages.value.Checksum;
27 import org.apache.directory.shared.asn1.der.DEREncodable;
28 import org.apache.directory.shared.asn1.der.DERInteger;
29 import org.apache.directory.shared.asn1.der.DEROctetString;
30 import org.apache.directory.shared.asn1.der.DERSequence;
31 import org.apache.directory.shared.asn1.der.DERTaggedObject;
32
33
34 /**
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 588239 $, $Date: 2007-10-25 16:24:28 +0200 (Do, 25 Okt 2007) $
37 */
38 public class ChecksumDecoder
39 {
40 /**
41 * Checksum ::= SEQUENCE {
42 * cksumtype[0] INTEGER,
43 * checksum[1] OCTET STRING
44 * }
45 * @param sequence
46 * @return The {@link Checksum}.
47 */
48 public static Checksum decode( DERSequence sequence )
49 {
50 ChecksumType type = ChecksumType.NULL;
51 byte[] data = null;
52
53 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
54 {
55 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
56 int tag = object.getTagNo();
57 DEREncodable derObject = object.getObject();
58
59 switch ( tag )
60 {
61 case 0:
62 DERInteger tag0 = ( DERInteger ) derObject;
63 type = ChecksumType.getTypeByOrdinal( tag0.intValue() );
64 break;
65
66 case 1:
67 DEROctetString tag1 = ( DEROctetString ) derObject;
68 data = tag1.getOctets();
69 break;
70 }
71 }
72
73 return new Checksum( type, data );
74 }
75 }