Friday, 20 October 2017
arrow JDiceChecker 5.0.0.1 arrow CryptoRandomStream arrow BaseCryptoRandomStream.java (2)
BaseCryptoRandomStream.java (2nd part) - (data storage base class) Print E-mail
Get C++ DiceLock cipher architecture source code packages of DiceLock for Microsoft Visual Studio 2013 and DiceLock-x for Linux with Test Driver Programs and Response Test Vector files to verify that both them work as expected.
DiceLock for Windows and Linux
DiceLock Security governing software licenses are Free/Libre Source Code License and Educational and Research License
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
 
  /**
   * Gets the bit value (value  0 or 1) as byte at specified postion, position based in array of bits
   * 
   * @param     position  bit position where to get the bit value
   * @return    byte:     bit value in a byte, value 0 or 1
   */
    public byte GetBitPosition(int position) {
        int bytePosition;
        int bitPosition;
 
        bytePosition = (int)Math.floor(position / TypeSizes.BYTE_BITS);
        bitPosition = position % TypeSizes.BYTE_BITS;
        return this.GetByteBitPosition(this.cryptoBuffer.get(bytePosition), bitPosition);
    }
 
  /**
   * Gets the byte at specified postion, position based in array of bytes
   * 
   * @param     position  byte position where to get the byte value
   * @return    byte:     byte value
   */
    public byte GetUCPosition(int position) {
 
        return this.cryptoBuffer.get(position);
    }
 
  /**
   * Gets the short value at specified postion, position based in array of shorts
   * 
   * @param     position  short position where to get the short value
   * @return    short:    short value
   */
    public short GetUSPosition(int position) {
 
        return this.cryptoBuffer.getShort(position * TypeSizes.SHORT_BYTES);
    }
 
  /**
   * Gets the int at specified postion, position based in array of ints
   * 
   * @param     position  int position where to get the int value
   * @return    int:      int value
   */
    public int GetULPosition(int position) {
 
        return this.cryptoBuffer.getInt(position * TypeSizes.INT_BYTES);
    }
 
  /**
   * Gets the long at specified postion, position based in array of longs
   * 
   * @param     position  int position where to get the long value
   * @return    long:     long value
   */
    public long Get64Position(int position) {
 
        return this.cryptoBuffer.getLong(position * TypeSizes.LONG_BYTES);
    }
 
  /**
   * Gets the baseCryptoRandomStream portion at specified position with 
   * from baseCryptoRandomStream object, position and length based in array of bytes
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in bytes that will be the starting point of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetUCSubRandomStream(BaseCryptoRandomStream subStream, int pos) throws IndexOutOfBoundsException;
  
  /**
   * Gets the baseCryptoRandomStream portion at specified position
   * from baseCryptoRandomStream object, position and length based in array of shorts 
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in shorts that will be the starting point of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetUSSubRandomStream(BaseCryptoRandomStream subStream, int pos) throws IndexOutOfBoundsException;
  
  /**
   * Gets the baseCryptoRandomStream portion at specified position
   * from baseCryptoRandomStream object, position and length based in array of ints
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in ints that will be the starting point of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetULSubRandomStream(BaseCryptoRandomStream subStream, int pos) throws IndexOutOfBoundsException;
  
  /**
   * Gets the baseCryptoRandomStream portion at specified postion with a specified length 
   * from baseCryptoRandomStream object, position and length based in array of bytes
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in bytes that will be the starting point of subStream CryptoRandomStream portion 
   * @param     length      length in bytes of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetUCSubRandomStream(BaseCryptoRandomStream subStream, int pos, int length) throws IndexOutOfBoundsException;
  
  /**
   * Gets the baseCryptoRandomStream portion at specified position with a specified length 
   * from baseCryptoRandomStream object, position and length based in array of shorts
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in shorts that will be the starting point of subStream CryptoRandomStream portion 
   * @param     length      length in shorts of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetUSSubRandomStream(BaseCryptoRandomStream subStream, int pos, int length) throws IndexOutOfBoundsException;
  
  /**
   * Gets the baseCryptoRandomStream portion at specified position with a specified length 
   * from baseCryptoRandomStream object, position and length based in array of ints
   * 
   * @param     subStream   CryptoRandomStream that will point the specified CryptoRandomStream portion 
   * @param     pos         position in ints that will be the starting point of subStream CryptoRandomStream portion 
   * @param     length      length in ints of subStream CryptoRandomStream portion 
   * @throws    IndexOutOfBoundsException
   */
    abstract public void GetULSubRandomStream(BaseCryptoRandomStream subStream, int pos, int length) throws IndexOutOfBoundsException;
  
  /**
   * Reduces considered length of BaseCryptoRandomStream, real length is mantained,
   * but any access to BaseCryptoRandomStream through the interface will be limited to
   * the new considered length. BaseCryptoRandomStream will remain with its original
   * length and using the specified memory.
   * Reduces length in bits.
   * 
   * @param     value   number of bits of new length 
   */
    public void ReduceBitLength(int value) {
 
        if ( this.reducedBitLength != 0 ) {
          this.reducedBitLength -= value; 
        }
        else {
          this.reducedBitLength = this.bitLength - value;
        }
    }
 
  /**
   * Reduces considered length of BaseCryptoRandomStream, real length is mantained,
   * but any access to BaseCryptoRandomStream through the interface will be limited to
   * the new considered length. BaseCryptoRandomStream will remain with its original
   * length and using the specified memory.
   * Reduces length in unsigned chars.
   * 
   * @param     value   number of bytes of new length 
   */
    public void ReduceUCLength(int value) {
 
      if ( this.reducedBitLength != 0 ) {
        this.reducedBitLength -= (value * TypeSizes.BYTE_BITS); 
        }
        else {
          this.reducedBitLength = this.bitLength - (value * TypeSizes.BYTE_BITS);
        }
    }
 
  /**
   * Reduces considered length of BaseCryptoRandomStream, real length is mantained,
   * but any access to BaseCryptoRandomStream through the interface will be limited to
   * the new considered length. BaseCryptoRandomStream will remain with its original
   * length and using the specified memory.
   * Reduces length in unsigned short ints.
   * 
   * @param     value   number of shorts of new length 
   */
    public void ReduceUSLength(int value) {
 
        if ( this.reducedBitLength != 0 ) {
          this.reducedBitLength -= (value * TypeSizes.SHORT_BITS); 
        }
        else {
          this.reducedBitLength = this.bitLength - (value * TypeSizes.SHORT_BITS);
        }
    }
 
  /**
   * Reduces considered length of BaseCryptoRandomStream, real length is mantained,
   * but any access to BaseCryptoRandomStream through the interface will be limited to
   * the new considered length. BaseCryptoRandomStream will remain with its original
   * length and using the specified memory.
   * Reduces length in unsigned long ints.
   * 
   * @param     value   number of ints of new length 
   */
    public void ReduceULLength(int value) {
 
        if ( this.reducedBitLength != 0 ) {
          this.reducedBitLength -= (value * TypeSizes.LONG_BITS); 
        }
        else {
          this.reducedBitLength = this.bitLength - (value * TypeSizes.LONG_BITS);
        }
    }
 
  /**
   * Indicates if BaseCryptoRandomStream's length has been reduced in a
   * fictitious way
   * 
   * @return    boolean: true if length stream has been reduced,  false if stream has its original length
   */
    public boolean ReducedLength() {
 
        return (this.reducedBitLength != 0);
    }
 
  /**
   * Restores original length of BaseCryptoRandomStream and removes any
   * fictitious reduced length
   * 
   */
    public void RestoreLength() {
 
        this.reducedBitLength = 0;
    }
 
  /**
   * Operator equal, compares the BaseCryptoRandomStream object with the
   * BaseCryptoRandomStream parameter
   * 
   * @param     otherStream    stream to be content compared with this stream
   */
    public boolean Equals(BaseCryptoRandomStream otherStream) {
      ByteBuffer thisBuffer;
        int        thisLimit = 0;
        ByteBuffer paramBuffer;
        int        paramLimit = 0;
        int        result;
 
        if (otherStream == null) {
          return false;
        }
        else {
          if (this.GetBitLength() != otherStream.GetBitLength()) {
            return false;
          }
          else {
            thisBuffer = this.cryptoBuffer;
            if ( this.ReducedLength() ) {
              thisLimit = this.cryptoBuffer.limit();
              thisBuffer.limit(this.GetUCLength());
            }
            paramBuffer = otherStream.cryptoBuffer;
            if ( otherStream.ReducedLength() ) {
              paramLimit = otherStream.cryptoBuffer.limit();
              paramBuffer.limit(otherStream.GetUCLength());
            }
            result = thisBuffer.compareTo(paramBuffer);
            if ( this.ReducedLength() ) {
              this.cryptoBuffer.limit(thisLimit );
            }
            paramBuffer = otherStream.cryptoBuffer;
            if ( otherStream.ReducedLength() ) {
              otherStream.cryptoBuffer.limit(paramLimit);
            }
            return (result == 0);
          }
        }
   }
 
  /**
   * Operator copy, copies the content of BaseCryptoRandomStream object
   * to the BaseCryptoRandomStream object passed as parameter
   * 
   * @param     target     target stream to copy the content of this stream
   * @throws    OutOfMemoryError
   */
    public void Copy(BaseCryptoRandomStream target) throws OutOfMemoryError {
 
        try {
          if ( this.GetUCLength() != target.GetUCLength() )
            throw new OutOfMemoryError("Streams have different lengths !");
          else {
            for (int i = 0; i < this.cryptoBuffer.limit(); i++) {
              target.cryptoBuffer.put(i, this.cryptoBuffer.get(i));
            }
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
    }
 
  /**
   * Gets the CryptoRandomStream type of the object
   * 
   * @return    CryptoRandomStreams:     the concrete type class of the stream
   */
    abstract public CryptoRandomStreams GetCryptoRandomStreamType();
 
}