|
From: Walter C. <cha...@gm...> - 2012-06-02 12:50:26
|
Hi All -
I'm migrating some code from java to jython and am having some
problems understanding how to translate the following:
byte[] salt = { (byte) 0xA5, (byte) 0x9E, (byte) 0xD8, (byte) 0x36,
(byte) 0x26, (byte) 0x99, (byte) 0xEE, (byte) 0xB3 };
Below is what the code is based on:
http://www.exampledepot.com/egs/javax.crypto/MakeDes.html
Thanks!
Walter
--
Simplicity--the art of maximizing the amount
of work not done--is essential.
from: http://www.agilemanifesto.org/principles.html
|
|
From: Philip J. <pj...@un...> - 2012-06-02 19:43:04
|
On Jun 2, 2012, at 5:50 AM, Walter Chang wrote:
> Hi All -
>
> I'm migrating some code from java to jython and am having some
> problems understanding how to translate the following:
>
> byte[] salt = { (byte) 0xA5, (byte) 0x9E, (byte) 0xD8, (byte) 0x36,
> (byte) 0x26, (byte) 0x99, (byte) 0xEE, (byte) 0xB3 };
>
> Below is what the code is based on:
You can get an array of Java bytes underneath via:
from array import array
salt = array('b', [0xA5, 0x9E, ...])
However the array type is stricter about the values passed to it than the line of Java you pasted (as you'd get this error):
OverflowError: value too large for byte
That line of Java is actually explicitly converting your ints (the hex literals) to bytes via the cast. If you want to programmatically support passing values like this to the array type you would need to do the same conversion manually beforehand
salt = array('b', (int2byte(i) for i in [0xA5, 0x9E, ...])) # the proper impl of int2byte escapes me now..
Or just pick salt values that already fit in the first place =]
--
Philip Jenvey
|
|
From: Walter C. <cha...@gm...> - 2012-06-03 22:31:05
|
Thanks for responding Philip -
import struct
import array
salt= [0xA5, 0x9E, 0xD8, 0x36,0x26, 0x99, 0xEE, 0xB3]
saltba = array.array('b')
saltba = saltba.append((struct.pack('i',val) for val in salt))
I tried playing around with struct but it returns the str not the
actual byte and sot I'm getting the following:
Traceback (most recent call last):
File "C:\Eclipse32\workspace1\jython1\src\test\testingsalt.py", line
12, in <module>
saltba = saltba.append((struct.pack('i',val) for val in salt))
TypeError: Type not compatible with array type
Any more pointers?
Thanks!
Walter
On Sat, Jun 2, 2012 at 3:42 PM, Philip Jenvey <pj...@un...> wrote:
>
> On Jun 2, 2012, at 5:50 AM, Walter Chang wrote:
>
>> Hi All -
>>
>> I'm migrating some code from java to jython and am having some
>> problems understanding how to translate the following:
>>
>> byte[] salt = { (byte) 0xA5, (byte) 0x9E, (byte) 0xD8, (byte) 0x36,
>> (byte) 0x26, (byte) 0x99, (byte) 0xEE, (byte) 0xB3 };
>>
>> Below is what the code is based on:
>
> You can get an array of Java bytes underneath via:
>
> from array import array
> salt = array('b', [0xA5, 0x9E, ...])
>
> However the array type is stricter about the values passed to it than the line of Java you pasted (as you'd get this error):
>
> OverflowError: value too large for byte
>
> That line of Java is actually explicitly converting your ints (the hex literals) to bytes via the cast. If you want to programmatically support passing values like this to the array type you would need to do the same conversion manually beforehand
>
> salt = array('b', (int2byte(i) for i in [0xA5, 0x9E, ...])) # the proper impl of int2byte escapes me now..
>
> Or just pick salt values that already fit in the first place =]
>
> --
> Philip Jenvey
>
--
Simplicity--the art of maximizing the amount
of work not done--is essential.
from: http://www.agilemanifesto.org/principles.html
|
|
From: Walter C. <cha...@gm...> - 2012-06-04 00:42:12
|
This is what I ended up doing in the end:
import array
import java.lang
salthex= [0xA5, 0x9E, 0xD8, 0x36,0x26, 0x99, 0xEE, 0xB3]
salt = array.array('b',(java.lang.Byte(val).byteValue() for val in salthex))
any better ideas?
Thanks!
Walter
On Sun, Jun 3, 2012 at 6:30 PM, Walter Chang <cha...@gm...> wrote:
> Thanks for responding Philip -
>
> import struct
> import array
>
> salt= [0xA5, 0x9E, 0xD8, 0x36,0x26, 0x99, 0xEE, 0xB3]
> saltba = array.array('b')
> saltba = saltba.append((struct.pack('i',val) for val in salt))
>
>
> I tried playing around with struct but it returns the str not the
> actual byte and so I'm getting the following:
>
> Traceback (most recent call last):
> File "C:\Eclipse32\workspace1\jython1\src\test\testingsalt.py", line
> 12, in <module>
> saltba = saltba.append((struct.pack('i',val) for val in salt))
> TypeError: Type not compatible with array type
>
> Any more pointers?
>
> Thanks!
> Walter
>
> On Sat, Jun 2, 2012 at 3:42 PM, Philip Jenvey <pj...@un...> wrote:
>>
>> On Jun 2, 2012, at 5:50 AM, Walter Chang wrote:
>>
>>> Hi All -
>>>
>>> I'm migrating some code from java to jython and am having some
>>> problems understanding how to translate the following:
>>>
>>> byte[] salt = { (byte) 0xA5, (byte) 0x9E, (byte) 0xD8, (byte) 0x36,
>>> (byte) 0x26, (byte) 0x99, (byte) 0xEE, (byte) 0xB3 };
>>>
>>> Below is what the code is based on:
>>
>> You can get an array of Java bytes underneath via:
>>
>> from array import array
>> salt = array('b', [0xA5, 0x9E, ...])
>>
>> However the array type is stricter about the values passed to it than the line of Java you pasted (as you'd get this error):
>>
>> OverflowError: value too large for byte
>>
>> That line of Java is actually explicitly converting your ints (the hex literals) to bytes via the cast. If you want to programmatically support passing values like this to the array type you would need to do the same conversion manually beforehand
>>
>> salt = array('b', (int2byte(i) for i in [0xA5, 0x9E, ...])) # the proper impl of int2byte escapes me now..
>>
>> Or just pick salt values that already fit in the first place =]
>>
>> --
>> Philip Jenvey
>>
>
>
>
> --
> Simplicity--the art of maximizing the amount
> of work not done--is essential.
> from: http://www.agilemanifesto.org/principles.html
--
Simplicity--the art of maximizing the amount
of work not done--is essential.
from: http://www.agilemanifesto.org/principles.html
|