Socket
Socket
Sign inDemoInstall

bcrypto

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bcrypto - npm Package Compare versions

Comparing version 5.4.0 to 5.5.2

deps/checks/check_asm.sh

6

CMakeLists.txt

@@ -8,9 +8,5 @@ # CMakeLists.txt - cmake build for bcrypto

include(cmake/AppendCCompilerFlag.cmake)
include(deps/torsion/cmake/AppendCCompilerFlag.cmake)
include(NodeJS)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 99)
option(BCRYPTO_ENABLE_LIBSECP256K1 "Use libsecp256k1" ON)

@@ -17,0 +13,0 @@

@@ -16,4 +16,5 @@ # CMakeLists.txt - cmake build for secp256k1

include(../../cmake/AppendCCompilerFlag.cmake)
include(../torsion/cmake/AppendCCompilerFlag.cmake)
include(CheckCSourceCompiles)
include(CheckTypeSize)
include(NodeJS)

@@ -38,8 +39,11 @@ include(TestBigEndian)

set(CMAKE_C_STANDARD 90)
set(CMAKE_C_VISIBILITY_PRESET hidden)
if(MSVC)
append_c_compiler_flag(secp256k1_cflags /wd4244
/wd4267
/wd4334)
append_c_compiler_flag(secp256k1_cflags /wd4244 # implicit integer demotion
/wd4267 # implicit size_t demotion
/wd4334) # implicit 32->64 bit shift
else()
# Note: libsecp256k1's build doesn't set
# -Wno-nonnull-compare for some reason.
append_c_compiler_flag(secp256k1_cflags -pedantic

@@ -49,4 +53,5 @@ -Wall

-Wcast-align
-Wcast-align=strict
-Wconditional-uninitialized
-Wnested-externs
-Wno-implicit-fallthrough
-Wno-long-long

@@ -57,3 +62,4 @@ -Wno-nonnull-compare

-Wshadow
-Wstrict-prototypes)
-Wstrict-prototypes
-Wundef)
endif()

@@ -60,0 +66,0 @@

@@ -5,2 +5,5 @@ # CMakeLists.txt - cmake build for libtorsion

set(TORSION_PKG_VERSION 0.0.0)
set(TORSION_ABI_VERSION 0:0:0)
#

@@ -11,12 +14,34 @@ # Initialization

cmake_minimum_required(VERSION 3.4)
project(libtorsion LANGUAGES C)
project(libtorsion VERSION ${TORSION_PKG_VERSION} LANGUAGES C)
#
# Toolchain/Platform Fixes
#
if(WASI)
# No idea why this isn't set.
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
endif()
if(EMSCRIPTEN)
# CMAKE_CROSSCOMPILING_EMULATOR is mistakenly quoted by emcmake.
string(REPLACE "\"" "" CMAKE_CROSSCOMPILING_EMULATOR
"${CMAKE_CROSSCOMPILING_EMULATOR}")
endif()
#
# Includes
#
include(../../cmake/AppendCCompilerFlag.cmake)
include(../../cmake/CheckCThreadLocalStorage.cmake)
include(cmake/AppendCCompilerFlag.cmake)
include(cmake/CheckCThreadLocalStorage.cmake)
include(cmake/LibtoolEmulator.cmake)
include(cmake/TargetLinkOptions.cmake)
include(CheckCSourceCompiles)
include(NodeJS)
include(CheckIncludeFile)
include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckTypeSize)
include(CTest)
include(GNUInstallDirs)

@@ -27,46 +52,108 @@ #

option(TORSION_ENABLE_ASM "Use inline x86-64 assembly if available" ON)
option(TORSION_ENABLE_CRT "Enable chinese remainder theorem for RSA" OFF)
option(TORSION_ENABLE_DEBUG "Enable debug build" OFF)
option(TORSION_ENABLE_ASM "Use inline assembly if available" ON)
option(TORSION_ENABLE_COVERAGE "Enable coverage" OFF)
option(TORSION_ENABLE_DEBUG "Enable debug build (forces -g or /Zi)" OFF)
option(TORSION_ENABLE_INT128 "Use __int128 if available" ON)
option(TORSION_ENABLE_LIBSECP256K1 "Use libsecp256k1 field element backend" OFF)
option(TORSION_ENABLE_PTHREAD "Use pthread as a fallback for TLS" ON)
option(TORSION_ENABLE_TLS "Enable thread-local storage" ON)
option(TORSION_ENABLE_MPI "Export MPI functions" OFF)
option(TORSION_ENABLE_PIC "Enable PIC" ON)
option(TORSION_ENABLE_PTHREAD "Use pthread if present in libc" ON)
option(TORSION_ENABLE_RNG "Enable RNG" ON)
option(TORSION_ENABLE_SHARED "Build shared library" ON)
option(TORSION_ENABLE_TESTS "Build tests" ON)
option(TORSION_ENABLE_TLS "Use thread-local storage if available" ON)
option(TORSION_ENABLE_VERIFY "Enable scalar bounds checks" OFF)
if(WASI OR EMSCRIPTEN)
set(TORSION_INITIAL_MEMORY "16777216" CACHE STRING "WASM initial memory")
set(TORSION_MAX_MEMORY "2147483648" CACHE STRING "WASM maximum memory")
set(TORSION_STACK_SIZE "5242880" CACHE STRING "WASM stack size")
endif()
if(EMSCRIPTEN)
set(TORSION_ENVIRONMENT "node" CACHE STRING "Emscripten environment")
endif()
#
# Flags
# Global Flags
#
set(torsion_cflags)
if(NOT PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
unset(CMAKE_C_STANDARD_REQUIRED)
unset(CMAKE_C_EXTENSIONS)
unset(CMAKE_C_STANDARD)
unset(CMAKE_C_VISIBILITY_PRESET)
unset(CMAKE_OSX_DEPLOYMENT_TARGET)
endif()
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 90)
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)
set(CMAKE_C_VISIBILITY_PRESET hidden)
if(MSVC)
append_c_compiler_flag(torsion_cflags /wd4146
/wd4244
/wd4267
/wd4334)
else()
append_c_compiler_flag(torsion_cflags -pedantic
-Wall
-Wextra
-Wcast-align
-Wno-implicit-fallthrough
-Wno-long-long
-Wno-overlength-strings
-Wshadow)
if(APPLE)
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)
endif()
if(TORSION_ENABLE_DEBUG)
if(MSVC)
append_c_compiler_flag(torsion_cflags /Zi)
else()
append_c_compiler_flag(torsion_cflags -g)
#
# Compiler Fixes
#
# Encourage the user to build with xlc_r.
if(CMAKE_C_COMPILER_ID STREQUAL "XL" AND CMAKE_SYSTEM_NAME STREQUAL "AIX")
check_symbol_exists(_THREAD_SAFE "" TORSION_HAVE_XLC_R)
if(NOT TORSION_HAVE_XLC_R)
message(WARNING "Please use a thread-safe compiler invocation.\n"
"For example, `cmake . -DCMAKE_C_COMPILER=xlc_r`.")
endif()
endif()
# Not sure why cmake fails to do this for tcc.
if(CMAKE_C_COMPILER_ID STREQUAL "TinyCC" AND NOT CMAKE_SKIP_RPATH)
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,'${PROJECT_SOURCE_DIR}'")
endif()
# Modern glibc (stdlib.h in particular) breaks pcc.
check_symbol_exists(__PCC__ "" TORSION_HAVE_PCC)
check_symbol_exists(__GLIBC__ "limits.h" TORSION_HAVE_GLIBC)
if(TORSION_HAVE_PCC AND TORSION_HAVE_GLIBC)
check_type_size(__float128 TORSION_SIZEOF_FLOAT128)
if (NOT TORSION_SIZEOF_FLOAT128)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__float128='long double'")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__TC__=__SC__")
endif()
endif()
# NWCC has issues with -fPIC and shared libraries.
check_symbol_exists(__NWCC__ "" TORSION_HAVE_NWCC)
if(TORSION_HAVE_NWCC)
set(TORSION_ENABLE_PIC OFF)
set(TORSION_ENABLE_SHARED OFF)
endif()
# chibicc doesn't support .so versioning.
# Furthermore, CMake doesn't know about chibicc's -fPIC.
check_symbol_exists(__chibicc__ "" TORSION_HAVE_CHIBICC)
if(TORSION_HAVE_CHIBICC)
set(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")
set(TORSION_ENABLE_SHARED OFF)
endif()
# OpenWatcom-Linux can't create shared libraries.
if(CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(TORSION_ENABLE_SHARED OFF)
endif()
endif()
# dietlibc is for static linking only.
check_symbol_exists(__dietlibc__ "" TORSION_HAVE_DIETLIBC)
if(TORSION_HAVE_DIETLIBC)
set(TORSION_ENABLE_SHARED OFF)
endif()
#

@@ -79,7 +166,33 @@ # Feature Testing

int main(void) {
unsigned char ptr[32];
int x = 0;
__asm__ ("" : "+r" (x) ::);
__asm__ __volatile__ ("" :: "r" (ptr) : "memory");
return x;
unsigned long z = 953;
unsigned long x = 109;
unsigned long y = 577;
unsigned long c;
__asm__ __volatile__ (
# if defined(__amd64__) || defined(__amd64)
"movq $0, %q1\\n"
# elif defined(__x86_64__) || defined(__x86_64)
"movq $0, %q1\\n"
# elif defined(__i386__) || defined(__i386) || defined(i386)
"movl $0, %k1\\n"
# elif defined(__arm__) || defined(__aarch64__)
"mov %1, #0\\n"
# elif defined(__powerpc__) || defined(__powerpc64__) || defined(__PPC__)
"li %1, 0\\n"
# elif defined(__riscv)
"lui %1, 0\\n"
# else
""
# endif
: "+r" (z), "=&r" (c)
# if defined(__wasm__) || defined(__EMSCRIPTEN__)
:
# elif defined(__TINYC__)
: "rm" (x), "rm" (y)
# else
: "%rm" (x), "rm" (y)
# endif
: "cc", "memory"
);
return z & 0x7f;
}

@@ -91,63 +204,68 @@ ]=] TORSION_HAS_ASM)

if(TORSION_ENABLE_ASM)
if(TORSION_ENABLE_TESTS AND NOT APPLE)
check_c_source_compiles([=[
#if defined(__amd64__) || defined(__x86_64__)
# error "not an x86 platform"
#endif
#ifndef __i386__
# error "not an x86 platform"
#endif
# include <time.h>
int main(void) {
unsigned int n1 = 0;
unsigned int n0 = 100;
unsigned int d = 3;
unsigned int q0, r0;
__asm__ __volatile__ (
"divl %k4\\n"
: "=a" (q0), "=d" (r0)
: "0" (n0), "1" (n1), "rm" (d)
);
return 0;
struct timespec ts;
(void)clock_gettime(CLOCK_REALTIME, &ts);
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec & ts.tv_nsec & 0x7f;
}
]=] TORSION_HAS_ASM_X86)
]=] TORSION_HAS_CLOCK_GETTIME)
else()
set(TORSION_HAS_ASM_X86)
set(TORSION_HAS_CLOCK_GETTIME)
endif()
if(TORSION_ENABLE_ASM)
if(TORSION_ENABLE_TESTS AND NOT EMSCRIPTEN)
check_c_source_compiles([=[
#if !defined(__amd64__) && !defined(__x86_64__)
# error "not an x64 platform"
#endif
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
int main(void) {
unsigned int dst[8], src[8];
__asm__ __volatile__ (
"movups (%%rsi), %%xmm0\\n"
"movups 16(%%rsi), %%xmm1\\n"
"pxor %%xmm1, %%xmm0\\n"
"movups %%xmm0,(%%rdi)\\n"
"movups %%xmm1,16(%%rdi)\\n"
:
: "D" (dst), "S" (src)
: "xmm0", "xmm1", "cc", "memory"
);
return 0;
unsigned char data[32];
int pfds[2];
int status;
pid_t pid;
pipe(pfds);
pid = fork();
close(pfds[1]);
read(pfds[0], data, 32);
close(pfds[0]);
waitpid(pid, &status, 0);
WIFEXITED(status);
WEXITSTATUS(status);
return status;
}
]=] TORSION_HAS_ASM_X64)
]=] TORSION_HAS_FORK)
else()
set(TORSION_HAS_ASM_X64)
set(TORSION_HAS_FORK)
endif()
if(TORSION_ENABLE_INT128)
if(TORSION_ENABLE_TESTS)
check_c_source_compiles([=[
typedef char check_64bit_t[sizeof(void *) >= 8 ? 1 : -1];
typedef signed __int128 int128_t;
typedef unsigned __int128 uint128_t;
# include <stddef.h>
# include <sys/time.h>
int main(void) {
uint128_t r = 1;
r <<= 64;
r *= 113;
r >>= 65;
return r & 1;
struct timeval tv;
(void)gettimeofday(&tv, NULL);
return tv.tv_sec & tv.tv_usec & 0x7f;
}
]=] TORSION_HAS_GETTIMEOFDAY)
else()
set(TORSION_HAS_GETTIMEOFDAY)
endif()
if(TORSION_ENABLE_INT128)
check_c_source_compiles([=[
typedef signed __int128 xint128_t;
typedef unsigned __int128 xuint128_t;
typedef char check_voidptr_t[sizeof(void *) >= 8 ? 1 : -1];
typedef char check_int128_t[sizeof(xint128_t) == 16 ? 1 : -1];
typedef char check_uint128_t[sizeof(xuint128_t) == 16 ? 1 : -1];
int main(int argc, char **argv) {
xint128_t c = argv[0][0];
xuint128_t r = argc + c;
while (argc--) r *= r;
return r >> 121;
}
]=] TORSION_HAS_INT128)

@@ -158,24 +276,123 @@ else()

if(TORSION_ENABLE_PTHREAD)
set(THREADS_PREFER_PTHREAD_FLAG ON)
if(TORSION_ENABLE_PTHREAD AND NOT EMSCRIPTEN)
check_c_source_compiles([=[
# include <pthread.h>
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int main(void) {
(void)pthread_mutex_lock(&lock);
(void)pthread_mutex_unlock(&lock);
return 0;
}
]=] TORSION_HAS_PTHREAD)
else()
set(TORSION_HAS_PTHREAD)
endif()
find_package(Threads)
set(TORSION_HAS_PTHREAD ${CMAKE_USE_PTHREADS_INIT})
if(TORSION_ENABLE_TESTS)
check_c_source_compiles([=[
# include <stddef.h>
# include <time.h>
int main(void) {
return time(NULL) & 0x7f;
}
]=] TORSION_HAS_TIME)
else()
set(TORSION_HAS_PTHREAD 0)
set(TORSION_HAS_TIME)
endif()
if(TORSION_ENABLE_TLS)
check_c_thread_local_storage(TORSION_TLS)
check_c_thread_local_storage(TORSION_TLS_KEYWORD
TORSION_TLS_FLAG
TORSION_TLS_EMULATED)
# Prefer pthread stubs over emulated TLS.
if(TORSION_TLS_EMULATED AND TORSION_HAS_PTHREAD)
set(TORSION_TLS_KEYWORD "")
set(TORSION_TLS_FLAG "")
endif()
else()
set(TORSION_TLS)
set(TORSION_TLS_KEYWORD)
set(TORSION_TLS_FLAG)
endif()
if(NOT TORSION_TLS)
set(TORSION_HAS_TLS_FALLBACK ${TORSION_HAS_PTHREAD})
set(TORSION_HAS_MEMCHECK 0)
set(TORSION_HAS_ZLIB 0)
if(TORSION_ENABLE_TESTS)
check_include_file(valgrind/memcheck.h TORSION_HAS_MEMCHECK_H)
if(TORSION_HAS_MEMCHECK_H)
set(TORSION_HAS_MEMCHECK 1)
endif()
if (TORSION_ENABLE_RNG)
check_include_file(zlib.h TORSION_HAS_ZLIB_H)
check_library_exists(z compress2 "" TORSION_HAS_ZLIB_A)
if(TORSION_HAS_ZLIB_H AND TORSION_HAS_ZLIB_A)
set(TORSION_HAS_ZLIB 1)
endif()
endif()
endif()
#
# Flags
#
set(torsion_cflags)
set(torsion_ldflags)
set(torsion_exeflags)
if(MSVC)
append_c_compiler_flag(torsion_cflags /wd4146 # negation of unsigned integer
/wd4244 # implicit integer demotion
/wd4267 # implicit size_t demotion
/wd4334) # implicit 32->64 bit shift
elseif(BORLAND)
# Todo.
elseif(CMAKE_C_COMPILER_ID MATCHES "Watcom$")
append_c_compiler_flag(torsion_cflags -wcd=201 # unreachable code
-wcd=202) # unused symbol
else()
set(TORSION_HAS_TLS_FALLBACK 0)
append_c_compiler_flag(torsion_cflags -pedantic
-Wall
-Wextra
-Wcast-align
-Wcast-align=strict
-Wconditional-uninitialized
-Wmissing-prototypes
-Wno-implicit-fallthrough
-Wno-long-long
-Wno-overlength-strings
-Wshadow
-Wstrict-prototypes
-Wundef)
endif()
if(TORSION_ENABLE_COVERAGE)
list(APPEND torsion_cflags -O0 --coverage)
list(APPEND torsion_ldflags --coverage)
endif()
if(TORSION_ENABLE_DEBUG)
if(MSVC)
append_c_compiler_flag(torsion_cflags /Zi)
elseif(BORLAND)
append_c_compiler_flag(torsion_cflags -v)
elseif(CMAKE_C_COMPILER_ID MATCHES "Watcom$")
append_c_compiler_flag(torsion_cflags -d2)
else()
append_c_compiler_flag(torsion_cflags -g)
endif()
endif()
if(TORSION_TLS_FLAG)
list(APPEND torsion_cflags ${TORSION_TLS_FLAG})
endif()
if(MINGW)
# Ensure we are redistributable on windows.
list(APPEND torsion_ldflags -static-libgcc)
endif()
#

@@ -189,10 +406,2 @@ # Defines

if(WIN32)
list(APPEND torsion_defines _WIN32_WINNT=0x0600)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND torsion_defines _POSIX_C_SOURCE=200112)
endif()
if(TORSION_HAS_ASM)

@@ -202,14 +411,10 @@ list(APPEND torsion_defines TORSION_HAVE_ASM)

if(TORSION_HAS_ASM_X86)
list(APPEND torsion_defines TORSION_HAVE_ASM_X86)
if(TORSION_HAS_CLOCK_GETTIME)
list(APPEND torsion_defines TORSION_HAVE_CLOCK_GETTIME)
endif()
if(TORSION_HAS_ASM_X64)
list(APPEND torsion_defines TORSION_HAVE_ASM_X64)
if(TORSION_ENABLE_COVERAGE)
list(APPEND torsion_defines TORSION_COVERAGE)
endif()
if(TORSION_ENABLE_CRT)
list(APPEND torsion_defines TORSION_USE_CRT)
endif()
if(TORSION_ENABLE_DEBUG)

@@ -221,2 +426,10 @@ list(APPEND torsion_defines TORSION_DEBUG)

if(TORSION_HAS_FORK)
list(APPEND torsion_defines TORSION_HAVE_FORK)
endif()
if(TORSION_HAS_GETTIMEOFDAY)
list(APPEND torsion_defines TORSION_HAVE_GETTIMEOFDAY)
endif()
if(TORSION_HAS_INT128)

@@ -226,17 +439,22 @@ list(APPEND torsion_defines TORSION_HAVE_INT128)

if(TORSION_ENABLE_LIBSECP256K1)
list(APPEND torsion_defines TORSION_USE_LIBSECP256K1)
if(TORSION_ENABLE_MPI)
list(APPEND torsion_defines TORSION_HAVE_MPI)
endif()
if(TORSION_TLS)
list(APPEND torsion_defines TORSION_HAVE_TLS)
list(APPEND torsion_defines TORSION_TLS=${TORSION_TLS})
else()
list(APPEND torsion_defines TORSION_TLS=)
if(TORSION_HAS_PTHREAD)
list(APPEND torsion_defines TORSION_HAVE_PTHREAD)
endif()
if(TORSION_HAS_TLS_FALLBACK)
list(APPEND torsion_defines TORSION_HAVE_PTHREAD)
if(TORSION_ENABLE_RNG)
list(APPEND torsion_defines TORSION_HAVE_RNG)
endif()
if(TORSION_HAS_TIME)
list(APPEND torsion_defines TORSION_HAVE_TIME)
endif()
if(TORSION_TLS_KEYWORD)
list(APPEND torsion_defines TORSION_TLS=${TORSION_TLS_KEYWORD})
endif()
if(TORSION_ENABLE_VERIFY)

@@ -246,6 +464,39 @@ list(APPEND torsion_defines TORSION_VERIFY)

if(TORSION_HAS_ZLIB)
list(APPEND torsion_defines TORSION_HAVE_ZLIB)
endif()
#
# Targets
# Feature Test Macros
#
if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
list(APPEND torsion_defines _TS_ERRNO)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
list(APPEND torsion_defines _THREAD_SAFE_ERRNO)
endif()
#
# Libraries
#
set(torsion_libs)
set(test_libs)
if(TORSION_HAS_ZLIB)
list(APPEND test_libs z)
endif()
#
# Includes
#
set(torsion_includes ${PROJECT_SOURCE_DIR}/include)
#
# Sources
#
set(torsion_sources src/aead.c

@@ -266,19 +517,260 @@ src/asn1.c

src/stream.c
src/util.c
src/entropy/env.c
src/entropy/hw.c
src/entropy/sys.c
src/rand.c)
src/util.c)
set(torsion_includes ${PROJECT_SOURCE_DIR}/include)
set(torsion_libs)
set(rng_sources src/entropy/hw.c
src/entropy/sys.c
src/rand.c)
if(TORSION_HAS_TLS_FALLBACK)
list(APPEND torsion_libs Threads::Threads)
set(test_sources test/test.c test/utils.c)
set(bench_sources test/bench.c test/hrtime.c test/utils.c)
set(ctgrind_sources test/ctgrind.c test/utils.c)
if(TORSION_ENABLE_RNG)
list(APPEND torsion_sources ${rng_sources})
endif()
add_node_library(torsion STATIC ${torsion_sources})
target_compile_definitions(torsion PRIVATE ${torsion_defines})
target_compile_options(torsion PRIVATE ${torsion_cflags})
target_include_directories(torsion PUBLIC ${torsion_includes})
target_link_libraries(torsion INTERFACE ${torsion_libs})
#
# Targets
#
if(NOT PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
# Bulding a subproject. Keep it simple.
if(COMMAND add_node_library)
add_node_library(torsion STATIC ${torsion_sources})
else()
add_library(torsion STATIC ${torsion_sources})
set_property(TARGET torsion PROPERTY POSITION_INDEPENDENT_CODE
${TORSION_ENABLE_PIC})
endif()
target_compile_definitions(torsion PRIVATE ${torsion_defines})
target_compile_options(torsion PRIVATE ${torsion_cflags})
target_include_directories(torsion PUBLIC ${torsion_includes})
target_link_options(torsion INTERFACE ${torsion_ldflags})
target_link_libraries(torsion INTERFACE ${torsion_libs})
elseif(WASI OR EMSCRIPTEN)
if(WASI)
set(torsion_ldflags -Wl,--allow-undefined
-Wl,--initial-memory=${TORSION_INITIAL_MEMORY}
-Wl,--max-memory=${TORSION_MAX_MEMORY}
-Wl,-z,stack-size=${TORSION_STACK_SIZE}
-Wl,--stack-first)
set(torsion_exeflags -mexec-model=reactor
-Wl,--export-dynamic
-Wl,--export=malloc
-Wl,--export=free)
else()
set(torsion_ldflags "SHELL:-s SINGLE_FILE=1"
"SHELL:-s ASSERTIONS=0"
"SHELL:-s NODEJS_CATCH_EXIT=0"
"SHELL:-s NODEJS_CATCH_REJECTION=0"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
"SHELL:-s INITIAL_MEMORY=${TORSION_INITIAL_MEMORY}"
"SHELL:-s MAXIMUM_MEMORY=${TORSION_MAX_MEMORY}"
"SHELL:-s TOTAL_STACK=${TORSION_STACK_SIZE}"
"SHELL:-s ENVIRONMENT=${TORSION_ENVIRONMENT}")
set(torsion_exeflags "SHELL:-s EXPORTED_FUNCTIONS=@etc/exports.json")
endif()
add_library(torsion_static STATIC ${torsion_sources})
target_compile_definitions(torsion_static PUBLIC ${torsion_defines})
target_compile_options(torsion_static PUBLIC ${torsion_cflags})
target_include_directories(torsion_static PUBLIC ${torsion_includes})
target_link_options(torsion_static INTERFACE ${torsion_ldflags})
set_property(TARGET torsion_static PROPERTY OUTPUT_NAME torsion)
add_executable(torsion_reactor ${torsion_sources})
target_compile_definitions(torsion_reactor PRIVATE ${torsion_defines}
TORSION_EXPORT)
target_compile_options(torsion_reactor PRIVATE ${torsion_cflags})
target_include_directories(torsion_reactor PRIVATE ${torsion_includes})
target_link_options(torsion_reactor PRIVATE ${torsion_ldflags}
${torsion_exeflags})
set_property(TARGET torsion_reactor PROPERTY OUTPUT_NAME torsion)
if(TORSION_ENABLE_TESTS)
add_executable(torsion_bench ${bench_sources})
target_link_libraries(torsion_bench PRIVATE torsion_static)
add_executable(torsion_test ${test_sources})
target_link_libraries(torsion_test PRIVATE torsion_static)
add_test(NAME test_wasm COMMAND torsion_test)
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
add_custom_target(bench COMMAND torsion_bench)
endif()
endif()
else()
if(TORSION_ENABLE_SHARED)
set(torsion_target torsion_shared)
set(torsion_targets torsion_static torsion_shared)
else()
set(torsion_target torsion_static)
set(torsion_targets torsion_static)
endif()
add_library(torsion_static STATIC ${torsion_sources})
target_compile_definitions(torsion_static PUBLIC ${torsion_defines})
target_compile_options(torsion_static PUBLIC ${torsion_cflags})
target_include_directories(torsion_static PUBLIC ${torsion_includes})
target_link_options(torsion_static INTERFACE ${torsion_ldflags})
target_link_libraries(torsion_static INTERFACE ${torsion_libs})
set_property(TARGET torsion_static PROPERTY POSITION_INDEPENDENT_CODE
${TORSION_ENABLE_PIC})
if(TORSION_ENABLE_SHARED)
add_library(torsion_shared SHARED ${torsion_sources})
target_compile_definitions(torsion_shared PUBLIC ${torsion_defines}
PRIVATE TORSION_EXPORT)
target_compile_options(torsion_shared PUBLIC ${torsion_cflags})
target_include_directories(torsion_shared PUBLIC ${torsion_includes})
target_link_options(torsion_shared PUBLIC ${torsion_ldflags})
target_link_libraries(torsion_shared PRIVATE ${torsion_libs})
set_property(TARGET torsion_shared PROPERTY OUTPUT_NAME torsion)
set_target_version_info(torsion_shared ${TORSION_ABI_VERSION})
endif()
if(UNIX)
set_property(TARGET torsion_static PROPERTY OUTPUT_NAME torsion)
configure_file(libtorsion-cmake.pc.in libtorsion.pc @ONLY)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES LICENSE
DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME})
install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(FILES ${PROJECT_BINARY_DIR}/libtorsion.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
install(TARGETS ${torsion_targets}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
if(MINGW)
set_target_properties(torsion_static PROPERTIES PREFIX "" SUFFIX ".lib")
if(TORSION_ENABLE_SHARED)
set_target_properties(torsion_shared PROPERTIES PREFIX ""
SUFFIX ".dll"
IMPORT_PREFIX ""
IMPORT_SUFFIX ".lib")
target_link_options(torsion_shared PRIVATE -Wl,--output-def,torsion.def)
endif()
endif()
if(WIN32)
set_property(TARGET torsion_static PROPERTY OUTPUT_NAME libtorsion)
install(DIRECTORY include/ DESTINATION include)
install(FILES LICENSE README.md DESTINATION .)
install(TARGETS ${torsion_targets}
ARCHIVE DESTINATION lib/$<CONFIG>
RUNTIME DESTINATION lib/$<CONFIG>)
endif()
if(TORSION_ENABLE_TESTS)
add_executable(torsion_bench ${bench_sources})
target_link_libraries(torsion_bench PRIVATE ${torsion_target})
add_executable(torsion_test ${test_sources})
target_link_options(torsion_test PRIVATE ${torsion_exeflags})
target_link_libraries(torsion_test PRIVATE ${torsion_target} ${test_libs})
add_test(NAME test_torsion COMMAND torsion_test)
if(TORSION_ENABLE_SHARED)
add_executable(torsion_test_static ${test_sources})
target_link_options(torsion_test_static PRIVATE ${torsion_exeflags})
target_link_libraries(torsion_test_static PRIVATE torsion_static
${test_libs})
add_test(NAME test_static COMMAND torsion_test_static)
endif()
if(TORSION_HAS_MEMCHECK)
add_executable(torsion_ctgrind ${ctgrind_sources})
target_link_options(torsion_ctgrind PRIVATE ${torsion_exeflags})
target_link_libraries(torsion_ctgrind PRIVATE ${torsion_target})
endif()
find_program(TORSION_VALGRIND valgrind)
if(TORSION_VALGRIND)
if(TORSION_HAS_MEMCHECK AND NOT TORSION_ENABLE_DEBUG)
add_test(NAME test_ctgrind COMMAND ${TORSION_VALGRIND}
--leak-check=full
--error-exitcode=1
$<TARGET_FILE:torsion_ctgrind>)
endif()
add_test(NAME test_valgrind COMMAND ${TORSION_VALGRIND}
--leak-check=full
--error-exitcode=1
$<TARGET_FILE:torsion_test>)
endif()
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
add_custom_target(bench COMMAND torsion_bench)
if(TORSION_VALGRIND)
if(TORSION_HAS_MEMCHECK AND NOT TORSION_ENABLE_DEBUG)
add_custom_target(ctgrind COMMAND ${TORSION_VALGRIND}
--leak-check=full
--error-exitcode=1
$<TARGET_FILE:torsion_ctgrind>
DEPENDS torsion_ctgrind)
endif()
add_custom_target(valgrind COMMAND ${TORSION_VALGRIND}
--leak-check=full
--error-exitcode=1
$<TARGET_FILE:torsion_test>
DEPENDS torsion_test)
endif()
endif()
endif()
endif()
#
# Output
#
foreach(name C_FLAGS SHARED_LINKER_FLAGS EXE_LINKER_FLAGS)
set(flags "")
if(DEFINED CMAKE_BUILD_TYPE AND CMAKE_BUILD_TYPE)
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
if(DEFINED CMAKE_${name}_${build_type} AND CMAKE_${name}_${build_type})
set(flags "${CMAKE_${name}_${build_type}}")
endif()
endif()
if(DEFINED CMAKE_${name} AND CMAKE_${name})
if(flags)
set(flags "${flags} ${CMAKE_${name}}")
else()
set(flags "${CMAKE_${name}}")
endif()
endif()
set(TORSION_${name} "${flags}")
endforeach()
message(STATUS "Build Options:
BUILD_TYPE: ${CMAKE_BUILD_TYPE}
INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}
SYSTEM: ${CMAKE_SYSTEM} (${CMAKE_SYSTEM_PROCESSOR})
C_COMPILER: ${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_ID})
C_FLAGS: ${TORSION_C_FLAGS}
SHARED_FLAGS: ${TORSION_SHARED_LINKER_FLAGS}
EXE_FLAGS: ${TORSION_EXE_LINKER_FLAGS}
torsion_cflags: ${torsion_cflags}
torsion_ldflags: ${torsion_ldflags}
torsion_exeflags: ${torsion_exeflags}
torsion_defines: ${torsion_defines}
torsion_libs: ${torsion_libs}
test_libs: ${test_libs}
")

@@ -97,3 +97,3 @@ /*!

exports.version = '5.4.0';
exports.version = '5.5.2';
exports.native = exports.SHA256.native;

@@ -283,3 +283,3 @@ /*!

default: {
throw new assert.AssertionError('Invalid mode.');
throw new Error('Invalid mode.');
}

@@ -373,3 +373,3 @@ }

default: {
throw new assert.AssertionError('Invalid mode.');
throw new Error('Invalid mode.');
}

@@ -376,0 +376,0 @@ }

@@ -9,2 +9,3 @@ /*!

module.exports = require('../js/bech32');
const BECH32 = require('../js/bech32');
module.exports = new BECH32(1);

@@ -9,5 +9,8 @@ /*!

let BECH32;
if (process.env.NODE_BACKEND === 'js')
module.exports = require('../js/bech32');
BECH32 = require('../js/bech32');
else
module.exports = require('../native/bech32');
BECH32 = require('../native/bech32');
module.exports = new BECH32(1);

@@ -101,13 +101,3 @@ /*!

function generate(pass, salt, rounds, minor = 'b') {
if (typeof salt === 'string') {
const [i, data] = decode64(salt, 0, BCRYPT_SALT192);
if (i !== salt.length || data == null)
throw new Error('Invalid salt string.');
salt = data;
}
const hash = derive(pass, salt, rounds, minor);
return encode(minor, rounds, salt, hash);

@@ -493,8 +483,6 @@ }

exports.native = 0;
exports.hash192 = hash192;
exports.derive = derive;
exports.generate = generate;
exports.verify = verify;
exports.hash256 = hash256;
exports.pbkdf = pbkdf;
exports.pbkdfAsync = pbkdfAsync;
exports.hash256 = hash256;

@@ -47,348 +47,360 @@ /*!

/**
* Update checksum.
* @ignore
* @param {Number} c
* @returns {Number}
* BECH32
*/
function polymod(c) {
const b = c >>> 25;
class BECH32 {
constructor(checksum) {
assert((checksum >>> 0) === checksum);
this.checksum = checksum;
this.native = 0;
}
return ((c & 0x1ffffff) << 5)
^ (0x3b6a57b2 & -((b >> 0) & 1))
^ (0x26508e6d & -((b >> 1) & 1))
^ (0x1ea119fa & -((b >> 2) & 1))
^ (0x3d4233dd & -((b >> 3) & 1))
^ (0x2a1462b3 & -((b >> 4) & 1));
}
/**
* Update checksum.
* @ignore
* @param {Number} c
* @returns {Number}
*/
/**
* Encode hrp and data as a bech32 string.
* @param {String} hrp
* @param {Buffer} data
* @returns {String}
*/
polymod(c) {
const b = c >>> 25;
function serialize(hrp, data) {
assert(typeof hrp === 'string');
assert(Buffer.isBuffer(data));
return ((c & 0x1ffffff) << 5)
^ (0x3b6a57b2 & -((b >> 0) & 1))
^ (0x26508e6d & -((b >> 1) & 1))
^ (0x1ea119fa & -((b >> 2) & 1))
^ (0x3d4233dd & -((b >> 3) & 1))
^ (0x2a1462b3 & -((b >> 4) & 1));
}
if (hrp.length === 0 || hrp.length > 83)
throw new Error('Invalid bech32 human-readable part.');
/**
* Encode hrp and data as a bech32 string.
* @param {String} hrp
* @param {Buffer} data
* @returns {String}
*/
if (hrp.length + 1 + data.length + 6 > 90)
throw new Error('Invalid bech32 data length.');
serialize(hrp, data) {
assert(typeof hrp === 'string');
assert(Buffer.isBuffer(data));
let str = '';
let chk = 1;
let i;
if (hrp.length === 0 || hrp.length > 83)
throw new Error('Invalid bech32 human-readable part.');
for (i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
if (hrp.length + 1 + data.length + 6 > 90)
throw new Error('Invalid bech32 data length.');
if (ch < 33 || ch > 126)
throw new Error('Invalid bech32 character.');
let str = '';
let chk = 1;
let i;
if (ch >= 65 && ch <= 90)
throw new Error('Invalid bech32 character.');
for (i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
chk = polymod(chk) ^ (ch >> 5);
}
if (ch < 33 || ch > 126)
throw new Error('Invalid bech32 character.');
chk = polymod(chk);
if (ch >= 65 && ch <= 90)
throw new Error('Invalid bech32 character.');
for (let i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
chk = this.polymod(chk) ^ (ch >> 5);
}
chk = polymod(chk) ^ (ch & 0x1f);
chk = this.polymod(chk);
str += hrp[i];
}
for (let i = 0; i < hrp.length; i++) {
const ch = hrp.charCodeAt(i);
str += '1';
chk = this.polymod(chk) ^ (ch & 0x1f);
for (let i = 0; i < data.length; i++) {
const ch = data[i];
str += hrp[i];
}
if (ch >> 5)
throw new Error('Invalid bech32 value.');
str += '1';
chk = polymod(chk) ^ ch;
for (let i = 0; i < data.length; i++) {
const ch = data[i];
str += CHARSET[ch];
}
if (ch >> 5)
throw new Error('Invalid bech32 value.');
for (let i = 0; i < 6; i++)
chk = polymod(chk);
chk = this.polymod(chk) ^ ch;
chk ^= 1;
str += CHARSET[ch];
}
for (let i = 0; i < 6; i++)
str += CHARSET[(chk >>> ((5 - i) * 5)) & 0x1f];
for (let i = 0; i < 6; i++)
chk = this.polymod(chk);
return str;
}
chk ^= this.checksum;
/**
* Decode a bech32 string.
* @param {String} str
* @returns {Array} [hrp, data]
*/
for (let i = 0; i < 6; i++)
str += CHARSET[(chk >>> ((5 - i) * 5)) & 0x1f];
function deserialize(str) {
assert(typeof str === 'string');
return str;
}
if (str.length < 8 || str.length > 90)
throw new Error('Invalid bech32 string length.');
/**
* Decode a bech32 string.
* @param {String} str
* @returns {Array} [hrp, data]
*/
let lower = false;
let upper = false;
let hlen = 0;
deserialize(str) {
assert(typeof str === 'string');
for (let i = 0; i < str.length; i++) {
const ch = str.charCodeAt(i);
if (str.length < 8 || str.length > 90)
throw new Error('Invalid bech32 string length.');
if (ch < 33 || ch > 126)
throw new Error('Invalid bech32 character.');
let lower = false;
let upper = false;
let hlen = 0;
if (ch >= 97 && ch <= 122)
lower = true;
else if (ch >= 65 && ch <= 90)
upper = true;
else if (ch === 49)
hlen = i;
}
for (let i = 0; i < str.length; i++) {
const ch = str.charCodeAt(i);
if (hlen === 0)
throw new Error('Invalid bech32 human-readable part.');
if (ch < 33 || ch > 126)
throw new Error('Invalid bech32 character.');
const dlen = str.length - (hlen + 1);
if (ch >= 97 && ch <= 122)
lower = true;
else if (ch >= 65 && ch <= 90)
upper = true;
else if (ch === 49)
hlen = i;
}
if (dlen < 6)
throw new Error('Invalid bech32 data length.');
if (hlen === 0)
throw new Error('Invalid bech32 human-readable part.');
if (lower && upper)
throw new Error('Invalid bech32 casing.');
const dlen = str.length - (hlen + 1);
let chk = 1;
let hrp = '';
if (dlen < 6)
throw new Error('Invalid bech32 data length.');
for (let i = 0; i < hlen; i++) {
let ch = str.charCodeAt(i);
if (lower && upper)
throw new Error('Invalid bech32 casing.');
if (ch >= 65 && ch <= 90)
ch += 32;
let chk = 1;
let hrp = '';
chk = polymod(chk) ^ (ch >> 5);
for (let i = 0; i < hlen; i++) {
let ch = str.charCodeAt(i);
hrp += String.fromCharCode(ch);
}
if (ch >= 65 && ch <= 90)
ch += 32;
chk = polymod(chk);
chk = this.polymod(chk) ^ (ch >> 5);
for (let i = 0; i < hlen; i++)
chk = polymod(chk) ^ (str.charCodeAt(i) & 0x1f);
hrp += String.fromCharCode(ch);
}
const data = Buffer.alloc(dlen - 6);
chk = this.polymod(chk);
let j = 0;
for (let i = 0; i < hlen; i++)
chk = this.polymod(chk) ^ (str.charCodeAt(i) & 0x1f);
for (let i = hlen + 1; i < str.length; i++) {
const val = TABLE[str.charCodeAt(i)];
const data = Buffer.alloc(dlen - 6);
if (val === -1)
throw new Error('Invalid bech32 character.');
let j = 0;
chk = polymod(chk) ^ val;
for (let i = hlen + 1; i < str.length; i++) {
const val = TABLE[str.charCodeAt(i)];
if (i < str.length - 6)
data[j++] = val;
}
if (val === -1)
throw new Error('Invalid bech32 character.');
if (chk !== 1)
throw new Error('Invalid bech32 checksum.');
chk = this.polymod(chk) ^ val;
assert(j === data.length);
if (i < str.length - 6)
data[j++] = val;
}
return [hrp, data];
}
if (chk !== this.checksum)
throw new Error('Invalid bech32 checksum.');
/**
* Test whether a string is a bech32 string.
* @param {String} str
* @returns {Boolean}
*/
assert(j === data.length);
function is(str) {
assert(typeof str === 'string');
return [hrp, data];
}
try {
deserialize(str);
return true;
} catch (e) {
return false;
/**
* Test whether a string is a bech32 string.
* @param {String} str
* @returns {Boolean}
*/
is(str) {
assert(typeof str === 'string');
try {
this.deserialize(str);
return true;
} catch (e) {
return false;
}
}
}
/**
* Convert serialized data to another base.
* @param {Buffer} dst
* @param {Number} dstoff
* @param {Number} dstbits
* @param {Buffer} src
* @param {Number} srcoff
* @param {Number} srcbits
* @param {Boolean} pad
* @returns {Buffer}
*/
/**
* Convert serialized data to another base.
* @param {Buffer} dst
* @param {Number} dstoff
* @param {Number} dstbits
* @param {Buffer} src
* @param {Number} srcoff
* @param {Number} srcbits
* @param {Boolean} pad
* @returns {Buffer}
*/
function convert(dst, dstoff, dstbits, src, srcoff, srcbits, pad) {
assert(Buffer.isBuffer(dst));
assert((dstoff >>> 0) === dstoff);
assert((dstbits >>> 0) === dstbits);
assert(Buffer.isBuffer(src));
assert((srcoff >>> 0) === srcoff);
assert((srcbits >>> 0) === srcbits);
assert(typeof pad === 'boolean');
assert(dstbits >= 1 && dstbits <= 8);
assert(srcbits >= 1 && srcbits <= 8);
convert(dst, dstoff, dstbits, src, srcoff, srcbits, pad) {
assert(Buffer.isBuffer(dst));
assert((dstoff >>> 0) === dstoff);
assert((dstbits >>> 0) === dstbits);
assert(Buffer.isBuffer(src));
assert((srcoff >>> 0) === srcoff);
assert((srcbits >>> 0) === srcbits);
assert(typeof pad === 'boolean');
assert(dstbits >= 1 && dstbits <= 8);
assert(srcbits >= 1 && srcbits <= 8);
const mask = (1 << dstbits) - 1;
const mask = (1 << dstbits) - 1;
let acc = 0;
let bits = 0;
let i = srcoff;
let j = dstoff;
let acc = 0;
let bits = 0;
let i = srcoff;
let j = dstoff;
for (; i < src.length; i++) {
acc = (acc << srcbits) | src[i];
bits += srcbits;
for (; i < src.length; i++) {
acc = (acc << srcbits) | src[i];
bits += srcbits;
while (bits >= dstbits) {
bits -= dstbits;
dst[j++] = (acc >>> bits) & mask;
while (bits >= dstbits) {
bits -= dstbits;
dst[j++] = (acc >>> bits) & mask;
}
}
}
const left = dstbits - bits;
const left = dstbits - bits;
if (pad) {
if (bits)
dst[j++] = (acc << left) & mask;
} else {
if (((acc << left) & mask) || bits >= srcbits)
throw new Error('Invalid bits.');
}
if (pad) {
if (bits)
dst[j++] = (acc << left) & mask;
} else {
if (((acc << left) & mask) || bits >= srcbits)
throw new Error('Invalid bits.');
}
assert(j <= dst.length);
assert(j <= dst.length);
return dst.slice(0, j);
}
return dst.slice(0, j);
}
/**
* Calculate size required for bit conversion.
* @param {Number} len
* @param {Number} srcbits
* @param {Number} dstbits
* @param {Boolean} pad
* @returns {Number}
*/
/**
* Calculate size required for bit conversion.
* @param {Number} len
* @param {Number} srcbits
* @param {Number} dstbits
* @param {Boolean} pad
* @returns {Number}
*/
function convertSize(len, srcbits, dstbits, pad) {
assert((len >>> 0) === len);
assert((srcbits >>> 0) === srcbits);
assert((dstbits >>> 0) === dstbits);
assert(typeof pad === 'boolean');
assert(srcbits >= 1 && srcbits <= 8);
assert(dstbits >= 1 && dstbits <= 8);
convertSize(len, srcbits, dstbits, pad) {
assert((len >>> 0) === len);
assert((srcbits >>> 0) === srcbits);
assert((dstbits >>> 0) === dstbits);
assert(typeof pad === 'boolean');
assert(srcbits >= 1 && srcbits <= 8);
assert(dstbits >= 1 && dstbits <= 8);
return ((len * srcbits + (dstbits - 1) * (pad | 0)) / dstbits) >>> 0;
}
return ((len * srcbits + (dstbits - 1) * (pad | 0)) / dstbits) >>> 0;
}
/**
* Convert serialized data to another base.
* @param {Buffer} data
* @param {Number} srcbits
* @param {Number} dstbits
* @param {Boolean} pad
* @returns {Buffer}
*/
/**
* Convert serialized data to another base.
* @param {Buffer} data
* @param {Number} srcbits
* @param {Number} dstbits
* @param {Boolean} pad
* @returns {Buffer}
*/
function convertBits(data, srcbits, dstbits, pad) {
assert(Buffer.isBuffer(data));
convertBits(data, srcbits, dstbits, pad) {
assert(Buffer.isBuffer(data));
const size = convertSize(data.length, srcbits, dstbits, pad);
const out = Buffer.alloc(size);
const size = this.convertSize(data.length, srcbits, dstbits, pad);
const out = Buffer.alloc(size);
return convert(out, 0, dstbits, data, 0, srcbits, pad);
}
return this.convert(out, 0, dstbits, data, 0, srcbits, pad);
}
/**
* Serialize data to bech32 address.
* @param {String} hrp
* @param {Number} version
* @param {Buffer} hash
* @returns {String}
*/
/**
* Serialize data to bech32 address.
* @param {String} hrp
* @param {Number} version
* @param {Buffer} hash
* @returns {String}
*/
function encode(hrp, version, hash) {
assert(typeof hrp === 'string');
assert((version >>> 0) === version);
assert(Buffer.isBuffer(hash));
encode(hrp, version, hash) {
assert(typeof hrp === 'string');
assert((version >>> 0) === version);
assert(Buffer.isBuffer(hash));
if (version > 31)
throw new Error('Invalid bech32 version.');
if (version > 31)
throw new Error('Invalid bech32 version.');
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
const out = POOL65;
const out = POOL65;
out[0] = version;
out[0] = version;
const data = convert(out, 1, 5, hash, 0, 8, true);
const data = this.convert(out, 1, 5, hash, 0, 8, true);
return serialize(hrp, data);
}
return this.serialize(hrp, data);
}
/**
* Deserialize data from bech32 address.
* @param {String} addr
* @returns {Array}
*/
/**
* Deserialize data from bech32 address.
* @param {String} addr
* @returns {Array}
*/
function decode(addr) {
const [hrp, data] = deserialize(addr);
decode(addr) {
const [hrp, data] = this.deserialize(addr);
if (data.length === 0 || data.length > 65)
throw new Error('Invalid bech32 data length.');
if (data.length === 0 || data.length > 65)
throw new Error('Invalid bech32 data length.');
const version = data[0];
const version = data[0];
if (version > 31)
throw new Error('Invalid bech32 version.');
if (version > 31)
throw new Error('Invalid bech32 version.');
const output = data; // Works because dstbits > srcbits.
const hash = convert(output, 0, 8, data, 1, 5, false);
const output = data; // Works because dstbits > srcbits.
const hash = this.convert(output, 0, 8, data, 1, 5, false);
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
if (hash.length < 2 || hash.length > 40)
throw new Error('Invalid bech32 data length.');
return [hrp, version, hash];
}
return [hrp, version, hash];
}
/**
* Test whether a string is a bech32 string.
* @param {String} addr
* @returns {Boolean}
*/
/**
* Test whether a string is a bech32 string.
* @param {String} addr
* @returns {Boolean}
*/
function test(addr) {
assert(typeof addr === 'string');
test(addr) {
assert(typeof addr === 'string');
try {
decode(addr);
return true;
} catch (e) {
return false;
try {
this.decode(addr);
return true;
} catch (e) {
return false;
}
}

@@ -401,9 +413,2 @@ }

exports.native = 0;
exports.serialize = serialize;
exports.deserialize = deserialize;
exports.is = is;
exports.convertBits = convertBits;
exports.encode = encode;
exports.decode = decode;
exports.test = test;
module.exports = BECH32;

@@ -101,3 +101,3 @@ /*!

MD5SHA1: Buffer.from([36]),
RIPEMD160: Buffer.from('3022300a060628cf0603003105000414', 'hex'),
RIPEMD160: Buffer.from('3021300906052b2403020105000414', 'hex'),
SHA1: Buffer.from('3021300906052b0e03021a05000414', 'hex'),

@@ -104,0 +104,0 @@ SHA224: Buffer.from('302d300d06096086480165030402040500041c', 'hex'),

@@ -16,34 +16,25 @@ /*!

function hash192(pass, salt, rounds) {
function generate(pass, salt, rounds, minor = 'b') {
if (typeof pass === 'string')
pass = Buffer.from(pass, 'utf8');
if (typeof salt === 'string')
salt = Buffer.from(salt, 'utf8');
if (salt == null)
salt = binding.NULL;
assert(Buffer.isBuffer(pass));
assert(Buffer.isBuffer(salt));
assert(typeof salt === 'string' || Buffer.isBuffer(salt));
assert((rounds >>> 0) === rounds);
assert(typeof minor === 'string');
assert(minor.length === 1);
return binding.bcrypt_hash192(pass, salt, rounds);
minor = minor.charCodeAt(0) & 0x7f;
return binding.bcrypt_generate(pass, salt, rounds, minor);
}
function hash256(pass, salt, rounds) {
function verify(pass, record) {
if (typeof pass === 'string')
pass = Buffer.from(pass, 'utf8');
if (typeof salt === 'string')
salt = Buffer.from(salt, 'utf8');
if (salt == null)
salt = binding.NULL;
assert(Buffer.isBuffer(pass));
assert(Buffer.isBuffer(salt));
assert((rounds >>> 0) === rounds);
assert(typeof record === 'string');
return binding.bcrypt_hash256(pass, salt, rounds);
return binding.bcrypt_verify(pass, record);
}

@@ -87,48 +78,2 @@

function derive(pass, salt, rounds, minor = 'b') {
if (typeof pass === 'string')
pass = Buffer.from(pass, 'utf8');
if (typeof salt === 'string')
salt = Buffer.from(salt, 'utf8');
assert(Buffer.isBuffer(pass));
assert(Buffer.isBuffer(salt));
assert((rounds >>> 0) === rounds);
assert(typeof minor === 'string');
assert(minor.length === 1);
minor = minor.charCodeAt(0) & 0x7f;
return binding.bcrypt_derive(pass, salt, rounds, minor);
}
function generate(pass, salt, rounds, minor = 'b') {
if (typeof pass === 'string')
pass = Buffer.from(pass, 'utf8');
assert(Buffer.isBuffer(pass));
assert(typeof salt === 'string' || Buffer.isBuffer(salt));
assert((rounds >>> 0) === rounds);
assert(typeof minor === 'string');
assert(minor.length === 1);
minor = minor.charCodeAt(0) & 0x7f;
if (typeof salt === 'string')
return binding.bcrypt_generate_with_salt64(pass, salt, rounds, minor);
return binding.bcrypt_generate(pass, salt, rounds, minor);
}
function verify(pass, record) {
if (typeof pass === 'string')
pass = Buffer.from(pass, 'utf8');
assert(Buffer.isBuffer(pass));
assert(typeof record === 'string');
return binding.bcrypt_verify(pass, record);
}
/*

@@ -139,8 +84,5 @@ * Expose

exports.native = 2;
exports.hash192 = hash192;
exports.hash256 = hash256;
exports.generate = generate;
exports.verify = verify;
exports.pbkdf = pbkdf;
exports.pbkdfAsync = pbkdfAsync;
exports.derive = derive;
exports.generate = generate;
exports.verify = verify;

@@ -16,44 +16,52 @@ /*!

function serialize(hrp, data) {
assert(typeof hrp === 'string');
assert(Buffer.isBuffer(data));
class BECH32 {
constructor(checksum) {
assert((checksum >>> 0) === checksum);
this.checksum = checksum;
this.native = 2;
}
return binding.bech32_serialize(hrp, data);
}
serialize(hrp, data) {
assert(typeof hrp === 'string');
assert(Buffer.isBuffer(data));
function deserialize(str) {
assert(typeof str === 'string');
return binding.bech32_deserialize(str);
}
return binding.bech32_serialize(hrp, data, this.checksum);
}
function is(str) {
assert(typeof str === 'string');
return binding.bech32_is(str);
}
deserialize(str) {
assert(typeof str === 'string');
return binding.bech32_deserialize(str, this.checksum);
}
function convertBits(data, srcbits, dstbits, pad) {
assert(Buffer.isBuffer(data));
assert((srcbits >>> 0) === srcbits);
assert((dstbits >>> 0) === dstbits);
assert(typeof pad === 'boolean');
is(str) {
assert(typeof str === 'string');
return binding.bech32_is(str, this.checksum);
}
return binding.bech32_convert_bits(data, srcbits, dstbits, pad);
}
convertBits(data, srcbits, dstbits, pad) {
assert(Buffer.isBuffer(data));
assert((srcbits >>> 0) === srcbits);
assert((dstbits >>> 0) === dstbits);
assert(typeof pad === 'boolean');
function encode(hrp, version, hash) {
assert(typeof hrp === 'string');
assert((version >>> 0) === version);
assert(Buffer.isBuffer(hash));
return binding.bech32_convert_bits(data, srcbits, dstbits, pad);
}
return binding.bech32_encode(hrp, version, hash);
}
encode(hrp, version, hash) {
assert(typeof hrp === 'string');
assert((version >>> 0) === version);
assert(Buffer.isBuffer(hash));
function decode(addr) {
assert(typeof addr === 'string');
return binding.bech32_decode(addr);
}
return binding.bech32_encode(hrp, version, hash, this.checksum);
}
function test(addr) {
assert(typeof addr === 'string');
return binding.bech32_test(addr);
decode(addr) {
assert(typeof addr === 'string');
return binding.bech32_decode(addr, this.checksum);
}
test(addr) {
assert(typeof addr === 'string');
return binding.bech32_test(addr, this.checksum);
}
}

@@ -65,9 +73,2 @@

exports.native = 2;
exports.serialize = serialize;
exports.deserialize = deserialize;
exports.is = is;
exports.convertBits = convertBits;
exports.encode = encode;
exports.decode = decode;
exports.test = test;
module.exports = BECH32;

@@ -19,34 +19,35 @@ /*!

__proto__: null,
BLAKE2B160: 0,
BLAKE2B256: 1,
BLAKE2B384: 2,
BLAKE2B512: 3,
BLAKE2S128: 4,
BLAKE2S160: 5,
BLAKE2S224: 6,
BLAKE2S256: 7,
GOST94: 8,
HASH160: 9,
HASH256: 10,
KECCAK224: 11,
KECCAK256: 12,
KECCAK384: 13,
KECCAK512: 14,
MD2: 15,
MD4: 16,
MD5: 17,
MD5SHA1: 18,
RIPEMD160: 19,
SHA1: 20,
SHA224: 21,
SHA256: 22,
SHA384: 23,
SHA512: 24,
SHA3_224: 25,
SHA3_256: 26,
SHA3_384: 27,
SHA3_512: 28,
SHAKE128: 29,
SHAKE256: 30,
WHIRLPOOL: 31
NONE: 0,
BLAKE2B160: 1,
BLAKE2B256: 2,
BLAKE2B384: 3,
BLAKE2B512: 4,
BLAKE2S128: 5,
BLAKE2S160: 6,
BLAKE2S224: 7,
BLAKE2S256: 8,
GOST94: 9,
HASH160: 10,
HASH256: 11,
KECCAK224: 12,
KECCAK256: 13,
KECCAK384: 14,
KECCAK512: 15,
MD2: 16,
MD4: 17,
MD5: 18,
MD5SHA1: 19,
RIPEMD160: 20,
SHA1: 21,
SHA224: 22,
SHA256: 23,
SHA384: 24,
SHA512: 25,
SHA3_224: 26,
SHA3_256: 27,
SHA3_384: 28,
SHA3_512: 29,
SHAKE128: 30,
SHAKE256: 31,
WHIRLPOOL: 32
};

@@ -53,0 +54,0 @@

@@ -233,3 +233,3 @@ /*!

if (hash == null)
hash = -1;
hash = binding.hashes.NONE;
else

@@ -259,3 +259,3 @@ hash = binding.hashes[hash];

if (hash == null)
hash = -1;
hash = binding.hashes.NONE;
else

@@ -262,0 +262,0 @@ hash = binding.hashes[hash];

@@ -145,3 +145,3 @@ /*!

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -185,3 +185,3 @@ }

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -431,3 +431,3 @@ }

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -512,3 +512,3 @@ }

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -595,3 +595,3 @@ }

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -630,3 +630,3 @@ }

default: {
throw new assert.AssertionError('Invalid key.');
throw new Error('Invalid key.');
}

@@ -633,0 +633,0 @@ }

{
"name": "bcrypto",
"version": "5.4.0",
"version": "5.5.2",
"description": "JS crypto library",

@@ -27,4 +27,3 @@ "keywords": [

"install": "node-gyp rebuild",
"lint": "eslint bench/*.js lib/ scripts/ test/ || exit 0",
"lint-ci": "eslint bench/*.js lib/ scripts/ test/",
"lint": "eslint bench/*.js lib/ scripts/ test/",
"test": "bmocha -S test/*-test.js",

@@ -71,2 +70,3 @@ "test-browser": "bmocha -B js -H -S test/*-test.js",

"./lib/encoding/bech32": "./lib/encoding/bech32-browser.js",
"./lib/encoding/bech32m": "./lib/encoding/bech32m-browser.js",
"./lib/encoding/cash32": "./lib/encoding/cash32-browser.js",

@@ -73,0 +73,0 @@ "./lib/gost94": "./lib/gost94-browser.js",

# bcrypto
![node.js](https://github.com/bcoin-org/bcrypto/workflows/node.js/badge.svg)
[![Node.js](https://github.com/bcoin-org/bcrypto/actions/workflows/node.js.yml/badge.svg)](https://github.com/bcoin-org/bcrypto/actions/workflows/node.js.yml)

@@ -5,0 +5,0 @@ The missing crypto module for Node.js. Bcrypto provides you with a consistent

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc