@zhongdao
2017-11-04T17:10:50.000000Z
字数 5712
阅读 1648
未分类
注意: 不能把少于4个字节的整数传给函数.
<?php
$a = hash('sha256', "a", true);
$b = hash('sha256', "b", true);
$c = hash('sha256', "c", true);
$d = hash('sha256', "d", true);
$e = hash('sha256', $a . $b, true);
$f = hash('sha256', $c . $d, true);
$g = hash('sha256', $e . $f);
var_dump($g);
$ php program.php
string(64) "14ede5e8e97ad9372327728f5099b95604a39593cac3bd38a343ad76205213e7"
#define TFM_DESC
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "tfm.h"
#include "tomcrypt.h"
char *get_sha256(char *buffer, int size)
{
hash_state resource;
char *digest;
digest = malloc(32);
sha256_init(&resource);
sha256_process(&resource, buffer, size);
sha256_done(&resource, digest);
return digest;
}
char *get_merkle_root(char *old, int size)
{
char *new, output[32], input[64];
int index;
printf("activation (%d)%c", size, 10);
if (size == 1) {
new = malloc(32);
memcpy(new, old, 32);
free(old);
return new;
}
if (size % 2) {
new = malloc(32 * (size + 1));
memcpy(new, old, size * 32);
memcpy(new + size * 32, old + (size - 1) * 32, 32);
free(old);
return get_merkle_root(new, size + 1);
}
new = malloc(32 * size / 2);
for (index = 0; index < size; index += 2) {
memcpy(input, old + index * 32, 64);
memcpy(new + (index / 2) * 32, get_sha256(input, 64), 32);
}
free(old);
return get_merkle_root(new, size / 2);
}
void print_digest(char *digest) // 用unsigned char* ?
{
int index;
for (index = 0; index < 32; index++)
printf("%02hhx", digest[index]); // 注意这里的hh !
// printf 默认打印
printf("%c", 10);
}
void main()
{
ltc_mp = tfm_desc;
register_prng(&sprng_desc);
register_hash(&sha256_desc);
char *collection, *digest;
collection = malloc(128);
memcpy(collection, get_sha256("a", 1), 32);
memcpy(collection + 32, get_sha256("b", 1), 32);
memcpy(collection + 64, get_sha256("c", 1), 32);
memcpy(collection + 96, get_sha256("d", 1), 32);
print_digest(get_merkle_root(collection, 4));
}
$ tcc -c root.c -o root.o && tcc -o root.elf root.o ../libtomcrypt.a ../libtfm.a && ./root.elf
activation (4)
activation (2)
activation (1)
14ede5e8e97ad9372327728f5099b95604a39593cac3bd38a343ad76205213e7
char与signed char, unsigned char的区别?
http://blog.csdn.net/sinat_20265495/article/details/51733015
char 与 unsigned char的本质区别
http://www.cnblogs.com/languoliang/archive/2013/03/15/char.html
C语言中 unsigned char 与 signed char 不同输出格式一些认识
http://blog.csdn.net/ssopp24/article/details/52838210
1.ANSI C 提供了3种字符类型,分别是char、signed char、unsigned char。
2.三者都占1个字节
3.signed char取值范围是 -128 到 127(有符号位) ; unsigned char 取值范围是 0 到 255
4.内存中一串二进制,它的含义,就是这个类型来说明的。
5.所谓signed char 和 unsigned char 其实是相对“运算”而说的,已经脱离了我们字面含义“字符”,表示的范围有限。
char 是 signed 还是 unsigned ?
C标准中对char是 Impementation Defined,就是未明确定义
(1)那它由什么定义?坦白说,具体的编译器明确定义,一般都是用signed char或unsigned char来实现char的,也就是说不同的编译器对char是什么定义不一样
(2)为什么要这样定义?因为这三种类型的对象在存储介质中的表现形式是一样的(都是一个占8bit的01串,只是解析的时候不同)
(3)到底是signed char还是unsigned char?这得看编译器:VC编译器、x86上的GCC都把char定义为signed char,而arm-linux-gcc却把char定义为 unsigned char
#1: 3930
[8629] -> YUANXUN(100)
#2: 4479
[4140] -> YUANXUN(100)
[7159] YUANXUN/8629/1 -> YUANXUN(99) PANPENG(1)
/*
INITIAL = 33000000
RATE = (60 / PERIOD) * 24 * 365
SUPPLY(HEIGHT) = INITIAL * (1 + 0.04 / RATE) ^ HEIGHT
DEMURRAGE(HEIGHT) = SUPPLY(HEIGHT) - SUPPLY(HEIGHT - 1)
30 * 24 * 365 = 262,800.
16,000,000 * (1 + 0.4 / 262800)^262800.
*/
/* 参数说明: 初始发行的币, 几分钟生成1个区块, ? */
uint64_t calculate_supply(uint64_t initial, uint32_t period, uint32_t height) {
double _0687, _8326, _3014;
_0687 = (double) initial;
_8326 = (60 * 24 * 365) / period;
_3014 = _0687 * pow(1 + 0.04 / _8326, height);
return ((uint64_t) _3014);
}
因为ecc的签名算法,随机数的原因,输出签名的长度在69-71之间变化.
wc 的最后1列表示字节数.
$ ./engine |wc
1 13 474
$ ./engine |wc
2 15 473
$ ./engine |wc
1 14 472
$ ./engine |wc
2 14 473
$ ./engine |wc
1 14 473
$ xxd 1.block
0000000: bb7e 00b5 e58d 54c3 cd08 8333 596e dcbf .~....T....3Yn..
0000010: 13f0 b820 fa92 345a 157d 065c 6fd6 266d ... ..4Z.}.\o.&m
0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000040: 0000 0000 ffff ffff ffff ffff ffff ffff ................
0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000060: ffff ffff bf7f fd59 0100 0000 5c00 0000 .......Y....\...
0000070: 0100 0000 0100 0000 3000 0000 0000 0000 ........0.......
0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000a0: 0000 0000 0000 0000 2000 0000 1000 0000 ........ .......
00000b0: 7afd 3bd3 f876 930d be40 b59e 9ba8 6b31 z.;..v...@....k1
00000c0: 6400 0000 0000 0000 d.......
$ xxd 2.block
0000000: 58c1 5725 2eee cb38 94d0 e962 2976 87d3 X.W%...8...b)v..
0000010: 4ee0 8154 4b5e cc28 6e64 041d 89ec 8a7e N..TK^.(nd.....~
0000020: 0000 0000 bdca ccd1 f766 4054 c719 9f36 .........f@T...6
0000030: 80d5 2309 1bd7 d911 7650 d681 dcc7 e131 ..#.....vP.....1
0000040: aeea 8a33 ffff ffff ffff ffff ffff ffff ...3............
0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000060: ffff ffff ce7f fd59 0200 0000 5c00 0000 .......Y....\...
0000070: 0100 0000 0100 0000 3000 0000 0000 0000 ........0.......
0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000a0: 0000 0000 0000 0000 2000 0000 1000 0000 ........ .......
00000b0: 7afd 3bd3 f876 930d be40 b59e 9ba8 6b31 z.;..v...@....k1
00000c0: 6400 0000 0000 0000 1101 0000 0100 0000 d...............
00000d0: 0200 0000 c500 0000 0100 0000 4e00 0000 ............N...
00000e0: 304c 0302 0700 0201 2002 207f 5e56 4655 0L...... . .^VFU
00000f0: 10ab 820d 50a1 9a4b 5cfd b974 1c5f 13e9 ....P..K\..t._..
0000100: 79dc 672b 413f ab9c ec8c b502 2100 f8ce y.g+A?......!...
0000110: 98b2 ca0a 465e ba4d fbf0 a950 08b5 c661 ....F^.M...P...a
0000120: cc77 6593 6962 ddb1 a8f4 d2e8 10bf 4700 .we.ib........G.
0000130: 0000 3045 0220 5e64 de30 5cd7 a2d5 107c ..0E. ^d.0\....|
0000140: 67e9 e5a5 b5b7 4f48 8715 d85c a587 7c98 g.....OH...\..|.
0000150: 89ea 13b3 5863 0221 00a1 816a ffbd 9b35 ....Xc.!...j...5
0000160: b2cc e4d0 29ed 981b abd0 fe63 ee8b 63ff ....)......c..c.
0000170: 47e0 8b10 8758 93f0 d7bb 7e00 b5e5 8d54 G....X....~....T
0000180: c3cd 0883 3359 6edc bf13 f0b8 20fa 9234 ....3Yn..... ..4
0000190: 5a15 7d06 5c6f d626 6d20 0000 0010 0000 Z.}.\o.&m ......
00001a0: 007a fd3b d3f8 7693 0dbe 40b5 9e9b a86b .z.;..v...@....k
00001b0: 3163 0000 0000 0000 0020 0000 0010 0000 1c....... ......
00001c0: 0019 fda2 6e18 9d51 a452 6e1d 23cc ce56 ....n..Q.Rn.#..V
00001d0: 2401 0000 0000 0000 00 $........