我想在Java Card上优化SHA-3算法。我需要一个消耗更少内存的快速算法,这样可以轻松地将<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="assets/img/carousel-1.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
<div class="item">
<img src="assets/img/carousel-2.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
<div class="item">
<img src="assets/img/carousel-3.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
...
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
转换为byte[]
(或简短short[]
到[]
)。我目前的实现如下:
byte[]
其中private short[] byteToShort(byte[] b,int len)
{
short len_conv = (short)(len/2);
for ( short x = 0; x < len_conv;x++)
{
for ( short j = 0 ; j < 2 ; j++)
aux[j] = b[2*x+j];
temp_conv[x] = (short)((((short)aux[1]) & 0xFF) | ((((short)(aux[0]) & 0xFF) << 8 )));
}
return temp_conv;
}
是len
数组的实际大小,b
和aux
被定义为私有,并分配为:
temp_conv
我目前使用Java Card v 2.2.2
答案 0 :(得分:6)
不要重新发明轮子:Java Card API中有一些有用的内置静态方法,出于性能原因,这些方法通常作为本机函数实现。你的代码不能比他们好。
1)首先,使用RAM阵列时需要javacardx.framework.util.ArrayLogic.arrayCopyRepackNonAtomic
:
ArrayLogic.arrayCopyRepackNonAtomic(b, (short) 0, len, temp_conv, (short) 0);
还有arrayCopyRepack
,这对于持久数组很有用(整个操作在一个事务中完成,但速度稍慢)。
2)如果你不能使用ArrayLogic
,总会有javacard.framework.Util.getShort
,你可以使用它而不是按位魔术:
private static final void byteToShort(final byte[] bytes, final short blen, final short[] shorts)
{
short x = 0;
short y = 0;
for (; y < blen; x++, y += 2)
{
shorts[x] = Util.getShort(bytes, y);
}
}
注意还有setShort
,这可能对short[]
到byte[]
转换很有用。
3)有关您的代码的其他一些注意事项,以防您真正想要自己实现它:
aux
存储在持久性内存中。这非常慢,可能会损坏您的卡,因为aux
经常被重写,请参阅Symptoms of EEPROM damage。b[2*x+j]
无效,因为乘法缓慢。您应该使用两个循环变量而仅添加。aux
和内循环,你根本不需要它们int len
怎么样? Java Card 2.2.2中没有int
...