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/PKCS5Padding.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/PKCS5Padding.java+128−0
@@ -0,0 +1,128 @@
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+
26+package com.sun.crypto.provider;
27+
28+import javax.crypto.ShortBufferException;
29+
30+/**
31+ * This class implements padding as specified in the PKCS#5 standard.
32+ *
33+ * @author Gigi Ankeny
34+ *
35+ *
36+ * @see Padding
37+ */
38+final class PKCS5Padding implements Padding {
39+
40+ private int blockSize;
41+
42+ PKCS5Padding(int blockSize) {
43+ this.blockSize = blockSize;
44+ }
45+
46+ /**
47+ * Adds the given number of padding bytes to the data input.
48+ * The value of the padding bytes is determined
49+ * by the specific padding mechanism that implements this
50+ * interface.
51+ *
52+ * @param in the input buffer with the data to pad
53+ * @param off the offset in <code>in</code> where the padding bytes
54+ * are appended
55+ * @param len the number of padding bytes to add
56+ *
57+ * @exception ShortBufferException if <code>in</code> is too small to hold
58+ * the padding bytes
59+ */
60+ public void padWithLen(byte[] in, int off, int len)
61+ throws ShortBufferException
62+ {
63+ if (in == null)
64+ return;
65+
66+ if ((off + len) > in.length) {
67+ throw new ShortBufferException("Buffer too small to hold padding");
68+ }
69+
70+ byte paddingOctet = (byte) (len & 0xff);
71+ for (int i = 0; i < len; i++) {
72+ in[i + off] = paddingOctet;
73+ }
74+ return;
75+ }
76+
77+ /**
78+ * Returns the index where the padding starts.
79+ *
80+ * <p>Given a buffer with padded data, this method returns the
81+ * index where the padding starts.
82+ *
83+ * @param in the buffer with the padded data
84+ * @param off the offset in <code>in</code> where the padded data starts
85+ * @param len the length of the padded data
86+ *
87+ * @return the index where the padding starts, or -1 if the input is
88+ * not properly padded
89+ */
90+ public int unpad(byte[] in, int off, int len) {
91+ if ((in == null) ||
92+ (len == 0)) { // this can happen if input is really a padded buffer
93+ return 0;
94+ }
95+
96+ byte lastByte = in[off + len - 1];
97+ int padValue = (int)lastByte & 0x0ff;
98+ if ((padValue < 0x01)
99+ || (padValue > blockSize)) {
100+ return -1;
101+ }
102+
103+ int start = off + len - ((int)lastByte & 0x0ff);
104+ if (start < off) {
105+ return -1;
106+ }
107+
108+ for (int i = 0; i < ((int)lastByte & 0x0ff); i++) {
109+ if (in[start+i] != lastByte) {
110+ return -1;
111+ }
112+ }
113+
114+ return start;
115+ }
116+
117+ /**
118+ * Determines how long the padding will be for a given input length.
119+ *
120+ * @param len the length of the data to pad
121+ *
122+ * @return the length of the padding
123+ */
124+ public int padLength(int len) {
125+ int paddingOctet = blockSize - (len % blockSize);
126+ return paddingOctet;
127+ }
128+}