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

Unified · Split

java/pkg/com/sun/crypto/provider/ISO10126Padding.java+123−0
@@ -0,0 +1,123 @@
1+/*
2+ * Copyright (c) 2003, 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 W3 XML ENC standard.
32+ *
33+ * @author Valerie Peng
34+ *
35+ *
36+ * @see Padding
37+ */
38+final class ISO10126Padding implements Padding {
39+
40+ private int blockSize;
41+
42+ ISO10126Padding(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+ byte[] padding = new byte[len];
72+ SunJCE.RANDOM.nextBytes(padding);
73+ padding[len-1] = paddingOctet;
74+ System.arraycopy(padding, 0, in, off, len);
75+ return;
76+ }
77+
78+ /**
79+ * Returns the index where the padding starts.
80+ *
81+ * <p>Given a buffer with padded data, this method returns the
82+ * index where the padding starts.
83+ *
84+ * @param in the buffer with the padded data
85+ * @param off the offset in <code>in</code> where the padded data starts
86+ * @param len the length of the padded data
87+ *
88+ * @return the index where the padding starts, or -1 if the input is
89+ * not properly padded
90+ */
91+ public int unpad(byte[] in, int off, int len) {
92+ if ((in == null) ||
93+ (len == 0)) { // this can happen if input is really a padded buffer
94+ return 0;
95+ }
96+
97+ byte lastByte = in[off + len - 1];
98+ int padValue = (int)lastByte & 0x0ff;
99+ if ((padValue < 0x01)
100+ || (padValue > blockSize)) {
101+ return -1;
102+ }
103+
104+ int start = off + len - ((int)lastByte & 0x0ff);
105+ if (start < off) {
106+ return -1;
107+ }
108+
109+ return start;
110+ }
111+
112+ /**
113+ * Determines how long the padding will be for a given input length.
114+ *
115+ * @param len the length of the data to pad
116+ *
117+ * @return the length of the padding
118+ */
119+ public int padLength(int len) {
120+ int paddingOctet = blockSize - (len % blockSize);
121+ return paddingOctet;
122+ }
123+}