UNPKG

5.9 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.rlphash = exports.ripemd160FromArray = exports.ripemd160FromString = exports.ripemd160 = exports.sha256FromArray = exports.sha256FromString = exports.sha256 = exports.keccakFromArray = exports.keccakFromHexString = exports.keccakFromString = exports.keccak256 = exports.keccak = void 0;
23const keccak_1 = require("ethereum-cryptography/keccak");
24const createHash = require('create-hash');
25const rlp = __importStar(require("rlp"));
26const bytes_1 = require("./bytes");
27const helpers_1 = require("./helpers");
28/**
29 * Creates Keccak hash of a Buffer input
30 * @param a The input data (Buffer)
31 * @param bits (number = 256) The Keccak width
32 */
33const keccak = function (a, bits = 256) {
34 (0, helpers_1.assertIsBuffer)(a);
35 switch (bits) {
36 case 224: {
37 return (0, keccak_1.keccak224)(a);
38 }
39 case 256: {
40 return (0, keccak_1.keccak256)(a);
41 }
42 case 384: {
43 return (0, keccak_1.keccak384)(a);
44 }
45 case 512: {
46 return (0, keccak_1.keccak512)(a);
47 }
48 default: {
49 throw new Error(`Invald algorithm: keccak${bits}`);
50 }
51 }
52};
53exports.keccak = keccak;
54/**
55 * Creates Keccak-256 hash of the input, alias for keccak(a, 256).
56 * @param a The input data (Buffer)
57 */
58const keccak256 = function (a) {
59 return (0, exports.keccak)(a);
60};
61exports.keccak256 = keccak256;
62/**
63 * Creates Keccak hash of a utf-8 string input
64 * @param a The input data (String)
65 * @param bits (number = 256) The Keccak width
66 */
67const keccakFromString = function (a, bits = 256) {
68 (0, helpers_1.assertIsString)(a);
69 const buf = Buffer.from(a, 'utf8');
70 return (0, exports.keccak)(buf, bits);
71};
72exports.keccakFromString = keccakFromString;
73/**
74 * Creates Keccak hash of an 0x-prefixed string input
75 * @param a The input data (String)
76 * @param bits (number = 256) The Keccak width
77 */
78const keccakFromHexString = function (a, bits = 256) {
79 (0, helpers_1.assertIsHexString)(a);
80 return (0, exports.keccak)((0, bytes_1.toBuffer)(a), bits);
81};
82exports.keccakFromHexString = keccakFromHexString;
83/**
84 * Creates Keccak hash of a number array input
85 * @param a The input data (number[])
86 * @param bits (number = 256) The Keccak width
87 */
88const keccakFromArray = function (a, bits = 256) {
89 (0, helpers_1.assertIsArray)(a);
90 return (0, exports.keccak)((0, bytes_1.toBuffer)(a), bits);
91};
92exports.keccakFromArray = keccakFromArray;
93/**
94 * Creates SHA256 hash of an input.
95 * @param a The input data (Buffer|Array|String)
96 */
97const _sha256 = function (a) {
98 a = (0, bytes_1.toBuffer)(a);
99 return createHash('sha256').update(a).digest();
100};
101/**
102 * Creates SHA256 hash of a Buffer input.
103 * @param a The input data (Buffer)
104 */
105const sha256 = function (a) {
106 (0, helpers_1.assertIsBuffer)(a);
107 return _sha256(a);
108};
109exports.sha256 = sha256;
110/**
111 * Creates SHA256 hash of a string input.
112 * @param a The input data (string)
113 */
114const sha256FromString = function (a) {
115 (0, helpers_1.assertIsString)(a);
116 return _sha256(a);
117};
118exports.sha256FromString = sha256FromString;
119/**
120 * Creates SHA256 hash of a number[] input.
121 * @param a The input data (number[])
122 */
123const sha256FromArray = function (a) {
124 (0, helpers_1.assertIsArray)(a);
125 return _sha256(a);
126};
127exports.sha256FromArray = sha256FromArray;
128/**
129 * Creates RIPEMD160 hash of the input.
130 * @param a The input data (Buffer|Array|String|Number)
131 * @param padded Whether it should be padded to 256 bits or not
132 */
133const _ripemd160 = function (a, padded) {
134 a = (0, bytes_1.toBuffer)(a);
135 const hash = createHash('rmd160').update(a).digest();
136 if (padded === true) {
137 return (0, bytes_1.setLengthLeft)(hash, 32);
138 }
139 else {
140 return hash;
141 }
142};
143/**
144 * Creates RIPEMD160 hash of a Buffer input.
145 * @param a The input data (Buffer)
146 * @param padded Whether it should be padded to 256 bits or not
147 */
148const ripemd160 = function (a, padded) {
149 (0, helpers_1.assertIsBuffer)(a);
150 return _ripemd160(a, padded);
151};
152exports.ripemd160 = ripemd160;
153/**
154 * Creates RIPEMD160 hash of a string input.
155 * @param a The input data (String)
156 * @param padded Whether it should be padded to 256 bits or not
157 */
158const ripemd160FromString = function (a, padded) {
159 (0, helpers_1.assertIsString)(a);
160 return _ripemd160(a, padded);
161};
162exports.ripemd160FromString = ripemd160FromString;
163/**
164 * Creates RIPEMD160 hash of a number[] input.
165 * @param a The input data (number[])
166 * @param padded Whether it should be padded to 256 bits or not
167 */
168const ripemd160FromArray = function (a, padded) {
169 (0, helpers_1.assertIsArray)(a);
170 return _ripemd160(a, padded);
171};
172exports.ripemd160FromArray = ripemd160FromArray;
173/**
174 * Creates SHA-3 hash of the RLP encoded version of the input.
175 * @param a The input data
176 */
177const rlphash = function (a) {
178 return (0, exports.keccak)(rlp.encode(a));
179};
180exports.rlphash = rlphash;
181//# sourceMappingURL=hash.js.map
\No newline at end of file