History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/crypto/provider/PBEKey.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/PBEKey.java+154−0
@@ -0,0 +1,154 @@
1/*
2 * Copyright (c) 1997, 2007, 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
26package com.sun.crypto.provider;
27
28import java.io.UnsupportedEncodingException;
29import java.security.KeyRep;
30import java.security.spec.InvalidKeySpecException;
31import javax.crypto.SecretKey;
32import javax.crypto.spec.PBEKeySpec;
33
34/**
35 * This class represents a PBE key.
36 *
37 * @author Jan Luehe
38 *
39 */
40final class PBEKey implements SecretKey {
41
42 static final long serialVersionUID = -2234768909660948176L;
43
44 private byte[] key;
45
46 private String type;
47
48 /**
49 * Creates a PBE key from a given PBE key specification.
50 *
51 * @param key the given PBE key specification
52 */
53 PBEKey(PBEKeySpec keySpec, String keytype) throws InvalidKeySpecException {
54 char[] passwd = keySpec.getPassword();
55 if (passwd == null) {
56 // Should allow an empty password.
57 passwd = new char[0];
58 }
59 for (int i=0; i<passwd.length; i++) {
60 if ((passwd[i] < '\u0020') || (passwd[i] > '\u007E')) {
61 throw new InvalidKeySpecException("Password is not ASCII");
62 }
63 }
64 this.key = new byte[passwd.length];
65 for (int i=0; i<passwd.length; i++)
66 this.key[i] = (byte) (passwd[i] & 0x7f);
67 java.util.Arrays.fill(passwd, ' ');
68 type = keytype;
69 }
70
71 public byte[] getEncoded() {
72 return (byte[])this.key.clone();
73 }
74
75 public String getAlgorithm() {
76 return type;
77 }
78
79 public String getFormat() {
80 return "RAW";
81 }
82
83 /**
84 * Calculates a hash code value for the object.
85 * Objects that are equal will also have the same hashcode.
86 */
87 public int hashCode() {
88 int retval = 0;
89 for (int i = 1; i < this.key.length; i++) {
90 retval += this.key[i] * i;
91 }
92 return(retval ^= getAlgorithm().toLowerCase().hashCode());
93 }
94
95 public boolean equals(Object obj) {
96 if (obj == this)
97 return true;
98
99 if (!(obj instanceof SecretKey))
100 return false;
101
102 SecretKey that = (SecretKey)obj;
103
104 if (!(that.getAlgorithm().equalsIgnoreCase(type)))
105 return false;
106
107 byte[] thatEncoded = that.getEncoded();
108 boolean ret = java.util.Arrays.equals(this.key, thatEncoded);
109 java.util.Arrays.fill(thatEncoded, (byte)0x00);
110 return ret;
111 }
112
113 /**
114 * readObject is called to restore the state of this key from
115 * a stream.
116 */
117 private void readObject(java.io.ObjectInputStream s)
118 throws java.io.IOException, ClassNotFoundException
119 {
120 s.defaultReadObject();
121 key = (byte[])key.clone();
122 }
123
124
125 /**
126 * Replace the PBE key to be serialized.
127 *
128 * @return the standard KeyRep object to be serialized
129 *
130 * @throws java.io.ObjectStreamException if a new object representing
131 * this PBE key could not be created
132 */
133 private Object writeReplace() throws java.io.ObjectStreamException {
134 return new KeyRep(KeyRep.Type.SECRET,
135 getAlgorithm(),
136 getFormat(),
137 getEncoded());
138 }
139
140 /**
141 * Ensures that the password bytes of this key are
142 * set to zero when there are no more references to it.
143 */
144 protected void finalize() throws Throwable {
145 try {
146 if (this.key != null) {
147 java.util.Arrays.fill(this.key, (byte)0x00);
148 this.key = null;
149 }
150 } finally {
151 super.finalize();
152 }
153 }
154}