-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f42cef1
commit d52c111
Showing
5 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Thanks https://github.com/axic/swarmhash | ||
|
||
var keccak = require("eth-lib/lib/hash").keccak256; | ||
var Bytes = require("eth-lib/lib/bytes"); | ||
|
||
var swarmHashBlock = function swarmHashBlock(length, data) { | ||
return keccak(Bytes.flatten([Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length))), "0x0000", data])); | ||
}; | ||
|
||
var swarmHash = function swarmHash(data) { | ||
var length = Bytes.length(data); | ||
|
||
if (length <= 4096) { | ||
return swarmHashBlock(length, data); | ||
} | ||
|
||
var maxSize = 4096; | ||
while (maxSize * (4096 / 32) < length) { | ||
maxSize *= 4096 / 32; | ||
} | ||
|
||
var innerNodes = []; | ||
for (var i = 0; i < length; i += maxSize) { | ||
var size = maxSize < length - i ? maxSize : length - i; | ||
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size))); | ||
} | ||
|
||
return swarmHashBlock(length, Bytes.flatten(innerNodes)); | ||
}; | ||
|
||
module.exports = swarmHash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Thanks https://github.com/axic/swarmhash | ||
|
||
const keccak = require("eth-lib/lib/hash").keccak256; | ||
const Bytes = require("eth-lib/lib/bytes"); | ||
|
||
const swarmHashBlock = (length, data) => | ||
keccak(Bytes.flatten([ | ||
Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length))), | ||
"0x0000", | ||
data])); | ||
|
||
const swarmHash = (data) => { | ||
const length = Bytes.length(data); | ||
|
||
if (length <= 4096) { | ||
return swarmHashBlock(length, data); | ||
} | ||
|
||
let maxSize = 4096; | ||
while ((maxSize * (4096 / 32)) < length) { | ||
maxSize *= (4096 / 32); | ||
} | ||
|
||
let innerNodes = []; | ||
for (let i = 0; i < length; i += maxSize) { | ||
const size = (maxSize < (length - i)) ? maxSize : (length - i); | ||
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size))); | ||
} | ||
|
||
return swarmHashBlock(length, Bytes.flatten(innerNodes)); | ||
}; | ||
|
||
module.exports = swarmHash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters