001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.kerberos.shared.io.decoder;
021    
022    
023    import java.util.Enumeration;
024    
025    import org.apache.directory.server.kerberos.shared.crypto.checksum.ChecksumType;
026    import org.apache.directory.server.kerberos.shared.messages.value.Checksum;
027    import org.apache.directory.shared.asn1.der.DEREncodable;
028    import org.apache.directory.shared.asn1.der.DERInteger;
029    import org.apache.directory.shared.asn1.der.DEROctetString;
030    import org.apache.directory.shared.asn1.der.DERSequence;
031    import org.apache.directory.shared.asn1.der.DERTaggedObject;
032    
033    
034    /**
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 588239 $, $Date: 2007-10-25 16:24:28 +0200 (Thu, 25 Oct 2007) $
037     */
038    public class ChecksumDecoder
039    {
040        /**
041         * Checksum ::=   SEQUENCE {
042         *          cksumtype[0]   INTEGER,
043         *          checksum[1]    OCTET STRING
044         * }
045         * @param sequence 
046         * @return The {@link Checksum}.
047         */
048        public static Checksum decode( DERSequence sequence )
049        {
050            ChecksumType type = ChecksumType.NULL;
051            byte[] data = null;
052    
053            for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
054            {
055                DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
056                int tag = object.getTagNo();
057                DEREncodable derObject = object.getObject();
058    
059                switch ( tag )
060                {
061                    case 0:
062                        DERInteger tag0 = ( DERInteger ) derObject;
063                        type = ChecksumType.getTypeByOrdinal( tag0.intValue() );
064                        break;
065                        
066                    case 1:
067                        DEROctetString tag1 = ( DEROctetString ) derObject;
068                        data = tag1.getOctets();
069                        break;
070                }
071            }
072    
073            return new Checksum( type, data );
074        }
075    }