HSPポータル
サイトマップ お問い合わせ


HSPTV!掲示板


未解決 解決 停止 削除要請

2021
0819
ととまんぼうHSPDish(Android)上でのSHA256の計算3解決


ととまんぼう

リンク

2021/8/19(Thu) 16:07:20|NO.93631

こんにちは。

質問があるのですが、Androidアプリ上でSHA256を計算したいのですが、移植で実装することは可能でしょうか?

 初心者故お手柔らかにお願いいたします。



この記事に返信する


ととまんぼう

リンク

2021/8/22(Sun) 20:57:27|NO.93658

がんばったのですが仕様通りの結果になりませんでした・・・。

#include "hsp3dish.as"
#packopt xsize 1024 ; 横サイズ
#packopt ysize 576 ; 縦サイズ
#define ctype ROTR(%1,%2) (%1 >> %2) OR (%1 << (32 - %2))
#define ctype SHR(%1,%2) (%1 >> %2)
#define ctype CH(%1,%2,%3) (%1 AND %2) XOR ((%1 XOR $FFFFFFFF) AND %3)
#define ctype MAJ(%1,%2,%3) (%1 AND %2) XOR (%1 AND %3) XOR (%2 AND %3)
#define ctype SIGMA0(%1) ROTR(%1,2) XOR ROTR(%1,13) XOR ROTR(%1,22)
#define ctype SIGMA1(%1) ROTR(%1,6) XOR ROTR(%1,11) XOR ROTR(%1,25)
#define ctype sSIGMA0(%1) ROTR(%1,7) XOR ROTR(%1,18) XOR SHR(%1,3)
#define ctype sSIGMA1(%1) ROTR(%1,17) XOR ROTR(%1,19) XOR SHR(%1,10)
shaK = 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
shaH0 = 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
dim shaW,64
dim shaH,8

cls
redraw 0
color 255,255,255
boxf 0,0,ginfo_winx,ginfo_winy
color 0,0,0

msg = "abc"
gosub *sha256
mes "end"
redraw 1

stop




*sha256
SHAmsgsize = strlen(msg)
if SHAmsgsize \ 64 < 56{
Pmsgsize = SHAmsgsize + (56 - SHAmsgsize \ 64) + 8
}else{
Pmsgsize = SHAmsgsize + (56 + 64 - SHAmsgsize \ 64) + 8
}
mes "msg"
mes msg
mes "SHAmsgsize"
mes SHAmsgsize
mes "Pmessize"
mes Pmsgsize
memexpand msg,Pmsgsize
memset msg,0,Pmsgsize-SHAmsgsize,SHAmsgsize
poke msg,SHAmsgsize,128
shaidx = SHAmsgsize*8
poke msg,Pmsgsize-4,(shaidx AND 0xff000000) >> 24
poke msg,Pmsgsize-3,(shaidx AND 0x00ff0000) >> 16
poke msg,Pmsgsize-2,(shaidx AND 0x0000ff00) >> 8
poke msg,Pmsgsize-1,(shaidx AND 0x000000ff)
repeat 8
shaH(cnt) = shaH0(cnt)
loop

*sha2562


repeat Pmsgsize / 64;メッセージ数分ループ
SHAcnt = cnt
repeat 16;メッセージスケジュールWの生成
shaidx = (SHAcnt*64) + (cnt*4)
shaW(cnt) = (peek(msg,shaidx) << 24) + (peek(msg,shaidx+1) << 16) + (peek(msg,shaidx+2) << 8) + peek(msg,shaidx+3)
loop
repeat 48,16
shaW(cnt) = (sSIGMA1(shaW(cnt-2)) + shaW(cnt-7) + sSIGMA0(shaW(cnt-15)) + shaW(cnt-16)) AND 0xffffffff
loop
shaA = shaH(0)
shaB = shaH(1)
shaC = shaH(2)
shaD = shaH(3)
shaE = shaH(4)
shaF = shaH(5)
shaG = shaH(6)
shaH2 = shaH(7)
repeat 64
shaidx = (shaH2 + SIGMA1(shaE) + CH(shaE, shaF, shaG) + shaK(cnt) + shaW(cnt)) AND 0xffffffff
shaidx2 = (SIGMA0(shaA) + MAJ(shaA, shaB, shaC)) AND 0xffffffff
shaH2 = shaG
shaG = shaF
shaF = shaE
shaE = (shaD + shaidx) AND 0xffffffff
shaD = shaC
shaC = shaB
shaB = shaA
shaA = (shaidx + shaidx2) AND 0xffffffff
loop
shaH(0) = (shaA + shaH(0)) AND 0xffffffff
shaH(1) = (shaB + shaH(1)) AND 0xffffffff
shaH(2) = (shaC + shaH(2)) AND 0xffffffff
shaH(3) = (shaD + shaH(3)) AND 0xffffffff
shaH(4) = (shaE + shaH(4)) AND 0xffffffff
shaH(5) = (shaF + shaH(5)) AND 0xffffffff
shaH(6) = (shaG + shaH(6)) AND 0xffffffff
shaH(7) = (shaH2 + shaH(7)) AND 0xffffffff
loop

repeat 8
mes strf("%x", shaH(cnt))
loop

return



Velgail

リンク

2021/8/24(Tue) 02:01:55|NO.93673

大きく分けて2つのミスがあったために事故が発生しています。
1. HSPの右シフトは算術シフトです。【論理シフト】ではありません。
2. マクロは文字列が展開します。【計算結果となりません】

論理シフトは手で実装しないといけないのですが、まあ以下のような感じになります。
ついでにマクロは計算単位毎に括弧でくくり、よそのデータと計算結合しないようにしました。

#module #defcfunc rightShift int x,int y,local z,local i,local j z=x>>y j=0 for i,0,32-y j|=1<<i next return z&&j #global #define ctype ROTR(%1,%2) ((rightShift(%1,%2)) OR (%1 << (32 - %2))) #define ctype SHR(%1,%2) (rightShift(%1,%2)) #define ctype CH(%1,%2,%3) ((%1 AND %2) XOR ((%1 XOR $FFFFFFFF) AND %3)) #define ctype MAJ(%1,%2,%3) ((%1 AND %2) XOR (%1 AND %3) XOR (%2 AND %3)) #define ctype SIGMA0(%1) (ROTR(%1,2) XOR ROTR(%1,13) XOR ROTR(%1,22)) #define ctype SIGMA1(%1) (ROTR(%1,6) XOR ROTR(%1,11) XOR ROTR(%1,25)) #define ctype sSIGMA0(%1) (ROTR(%1,7) XOR ROTR(%1,18) XOR SHR(%1,3)) #define ctype sSIGMA1(%1) (ROTR(%1,17) XOR ROTR(%1,19) XOR SHR(%1,10))

なお途中の
poke msg,Pmsgsize-4,(shaidx AND 0xff000000) >> 24

poke msg,Pmsgsize-4,rightShift((shaidx AND 0xff000000) ,24)
と置き換えてください。

これで期待通り、"abc"のハッシュが"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"になるはずです。



ととまんぼう

リンク

2021/9/9(Thu) 14:56:04|NO.93823

マクロは文字列が展開するだけだったのですね・・・!勉強になりました。
おかげさまでSHA256の計算ができるようになりました!!ありがとうございました!



ONION software Copyright 1997-2023(c) All rights reserved.