1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.krb5.internal.rcache;
  27 
  28 import java.util.Objects;
  29 
  30 /**
  31  * The class represents a new style replay cache entry. It can be either used
  32  * inside memory or in a dfl file.
  33  */
  34 public class AuthTimeWithHash extends AuthTime
  35         implements Comparable<AuthTimeWithHash> {
  36 
  37     final String hash;
  38 
  39     /**
  40      * Constructs a new <code>AuthTimeWithHash</code>.
  41      */
  42     public AuthTimeWithHash(String client, String server,
  43             int ctime, int cusec, String hash) {
  44         super(client, server, ctime, cusec);
  45         this.hash = hash;
  46     }
  47 
  48     /**
  49      * Compares if an object equals to an <code>AuthTimeWithHash</code> object.
  50      * @param o an object.
  51      * @return true if two objects are equivalent, otherwise, return false.
  52      */
  53     @Override
  54     public boolean equals(Object o) {
  55         if (this == o) return true;
  56         if (!(o instanceof AuthTimeWithHash)) return false;
  57         AuthTimeWithHash that = (AuthTimeWithHash)o;
  58         return Objects.equals(hash, that.hash)
  59                 && Objects.equals(client, that.client)
  60                 && Objects.equals(server, that.server)
  61                 && ctime == that.ctime
  62                 && cusec == that.cusec;
  63     }
  64 
  65     /**
  66      * Returns a hash code for this <code>AuthTimeWithHash</code> object.
  67      */
  68     @Override
  69     public int hashCode() {
  70         return Objects.hash(hash);
  71     }
  72 
  73     @Override
  74     public String toString() {
  75         return String.format("%d/%06d/%s/%s", ctime, cusec, hash, client);
  76     }
  77 
  78     @Override
  79     public int compareTo(AuthTimeWithHash other) {
  80         int cmp = 0;
  81         if (ctime != other.ctime) {
  82             cmp = Integer.compare(ctime, other.ctime);
  83         } else if (cusec != other.cusec) {
  84             cmp = Integer.compare(cusec, other.cusec);
  85         } else {
  86             cmp = hash.compareTo(other.hash);
  87         }
  88         return cmp;
  89     }
  90 
  91     /**
  92      * Compares with a possibly old style object. Used
  93      * in DflCache$Storage#loadAndCheck.
  94      * @return true if all AuthTime fields are the same
  95      */
  96     public boolean isSameIgnoresHash(AuthTime old) {
  97         return  client.equals(old.client) &&
  98                 server.equals(old.server) &&
  99                 ctime == old.ctime &&
 100                 cusec == old.cusec;
 101     }
 102 
 103     // Methods used when saved in a dfl file. See DflCache.java
 104 
 105     /**
 106      * Encodes to be used in a dfl file
 107      * @param withHash write new style if true
 108      */
 109     @Override
 110     public byte[] encode(boolean withHash) {
 111         String cstring;
 112         String sstring;
 113         if (withHash) {
 114             cstring = "";
 115             sstring = String.format("HASH:%s %d:%s %d:%s", hash,
 116                     client.length(), client,
 117                     server.length(), server);
 118         } else {
 119             cstring = client;
 120             sstring = server;
 121         }
 122         return encode0(cstring, sstring);
 123     }
 124 }