01: /*
02: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: /*
27: * @(#)LocalSeqNumber.java 1.17 07/05/05
28: *
29: * (C) Copyright IBM Corp. 1999 All Rights Reserved.
30: * Copyright 1997 The Open Group Research Institute. All rights reserved.
31: */
32:
33: package sun.security.krb5.internal;
34:
35: import sun.security.krb5.Confounder;
36:
37: public class LocalSeqNumber implements SeqNumber {
38: private int lastSeqNumber;
39:
40: public LocalSeqNumber() {
41: randInit();
42: }
43:
44: public LocalSeqNumber(int start) {
45: init(start);
46: }
47:
48: public LocalSeqNumber(Integer start) {
49: init(start.intValue());
50: }
51:
52: public synchronized void randInit() {
53: /*
54: * Sequence numbers fall in the range 0 through 2^32 - 1 and wrap
55: * to zero following the value 2^32 - 1.
56: * Previous implementations used signed sequence numbers.
57: * Workaround implementation incompatibilities by not generating
58: * initial sequence numbers greater than 2^30, as done
59: * in MIT distribution.
60: */
61: // get the random confounder
62: byte[] data = Confounder.bytes(4);
63: data[0] = (byte) (data[0] & 0x3f);
64: int result = ((data[3] & 0xff) | ((data[2] & 0xff) << 8)
65: | ((data[1] & 0xff) << 16) | ((data[0] & 0xff) << 24));
66: if (result == 0) {
67: result = 1;
68: }
69: lastSeqNumber = result;
70: }
71:
72: public synchronized void init(int start) {
73: lastSeqNumber = start;
74: }
75:
76: public synchronized int current() {
77: return lastSeqNumber;
78: }
79:
80: public synchronized int next() {
81: return lastSeqNumber + 1;
82: }
83:
84: public synchronized int step() {
85: return ++lastSeqNumber;
86: }
87:
88: }
|