mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Extract Sha512 to own file
This commit is contained in:
27
packages/ripple-keypairs/src/sha512.js
Normal file
27
packages/ripple-keypairs/src/sha512.js
Normal file
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const hashjs = require('hash.js');
|
||||
const BigNum = require('bn.js');
|
||||
|
||||
module.exports = class Sha512 {
|
||||
constructor() {
|
||||
this.hash = hashjs.sha512();
|
||||
}
|
||||
add(bytes) {
|
||||
this.hash.update(bytes);
|
||||
return this;
|
||||
}
|
||||
addU32(i) {
|
||||
return this.add([(i >>> 24) & 0xFF, (i >>> 16) & 0xFF,
|
||||
(i >>> 8) & 0xFF, i & 0xFF]);
|
||||
}
|
||||
finish() {
|
||||
return this.hash.digest();
|
||||
}
|
||||
first256() {
|
||||
return this.finish().slice(0, 32);
|
||||
}
|
||||
first256BN() {
|
||||
return new BigNum(this.first256());
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const BigNum = require('bn.js');
|
||||
const hashjs = require('hash.js');
|
||||
const Sha512 = require('./sha512');
|
||||
|
||||
function isVirtual() {
|
||||
throw new Error('virtual method not implemented ');
|
||||
@@ -19,13 +19,6 @@ function cachedProperty(obj, computer) {
|
||||
};
|
||||
}
|
||||
|
||||
function arrayToHex(a) {
|
||||
return a.map(function(byteValue) {
|
||||
const hex = byteValue.toString(16).toUpperCase();
|
||||
return hex.length > 1 ? hex : '0' + hex;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function toGenericArray(sequence) {
|
||||
const generic = [];
|
||||
for (let i = 0; i < sequence.length; i++) {
|
||||
@@ -34,35 +27,11 @@ function toGenericArray(sequence) {
|
||||
return generic;
|
||||
}
|
||||
|
||||
function bytesToHex(bytes) {
|
||||
return arrayToHex(bytes);
|
||||
}
|
||||
|
||||
class Sha512 {
|
||||
constructor() {
|
||||
this.hash = hashjs.sha512();
|
||||
}
|
||||
add(bytes) {
|
||||
this.hash.update(bytes);
|
||||
return this;
|
||||
}
|
||||
addU32(i) {
|
||||
return this.add([(i >>> 24) & 0xFF, (i >>> 16) & 0xFF,
|
||||
(i >>> 8) & 0xFF, i & 0xFF]);
|
||||
}
|
||||
finish() {
|
||||
return this.hash.digest();
|
||||
}
|
||||
first256() {
|
||||
return this.finish().slice(0, 32);
|
||||
}
|
||||
first256BN() {
|
||||
return new BigNum(this.first256());
|
||||
}
|
||||
}
|
||||
|
||||
function seedFromPhrase(phrase) {
|
||||
return hashjs.sha512().update(phrase).digest().slice(0, 16);
|
||||
function bytesToHex(a) {
|
||||
return a.map(function(byteValue) {
|
||||
const hex = byteValue.toString(16).toUpperCase();
|
||||
return hex.length > 1 ? hex : '0' + hex;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function createAccountID(pubKeyBytes) {
|
||||
@@ -71,13 +40,16 @@ function createAccountID(pubKeyBytes) {
|
||||
return hash160;
|
||||
}
|
||||
|
||||
function seedFromPhrase(phrase) {
|
||||
return hashjs.sha512().update(phrase).digest().slice(0, 16);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
arrayToHex,
|
||||
bytesToHex,
|
||||
cachedProperty,
|
||||
createAccountID,
|
||||
isVirtual,
|
||||
Sha512,
|
||||
toGenericArray,
|
||||
seedFromPhrase,
|
||||
createAccountID
|
||||
Sha512,
|
||||
toGenericArray
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user