Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
A Long class for representing a 64-bit two's-complement integer value.
The @xtuc/long npm package provides functionality for representing 64-bit two's-complement integer values in JavaScript, which are not natively supported by the language. This package allows for the creation, manipulation, and arithmetic of long integers, which is useful in applications that require precise handling of large integer values, such as cryptography, databases, and network protocols.
Creating long integers
This code sample demonstrates how to create a new long integer with the most significant 32 bits set to 0x7FFFFFFF and the least significant 32 bits set to 0xFFFFFFFF.
{"Long": require('@xtuc/long'), "longVal": new Long(0xFFFFFFFF, 0x7FFFFFFF)}
Arithmetic operations
This code sample shows how to perform addition of two long integers, resulting in a new long integer.
{"Long": require('@xtuc/long'), "result": new Long(1, 0).add(new Long(1, 0))}
Bitwise operations
This code sample illustrates how to perform a bitwise OR operation between two long integers.
{"Long": require('@xtuc/long'), "result": new Long(1, 0).or(new Long(2, 0))}
Conversion to native types
This code sample demonstrates how to convert a long integer to a native JavaScript number. Note that this can result in precision loss if the long integer's value is outside the safe range for JavaScript numbers.
{"Long": require('@xtuc/long'), "numberVal": new Long(1, 0).toNumber()}
The 'long' package is similar to '@xtuc/long' and provides a complete set of 64-bit integer arithmetic and logical operations. It is widely used and has been around for a longer time, making it a well-tested choice for handling long integers in JavaScript.
The 'bignum' package offers arbitrary-precision arithmetic operations, which means it can handle integers larger than 64 bits. It is more versatile than '@xtuc/long' but might be slower for operations that only require 64-bit precision.
The 'bigint-buffer' package is designed to work with Node.js's native BigInt type, providing buffer conversion utilities. It is a more modern approach that leverages the BigInt support in newer versions of JavaScript, whereas '@xtuc/long' provides a solution that works in environments without BigInt support.
A Long class for representing a 64 bit two's-complement integer value derived from the Closure Library for stand-alone use and extended with unsigned support.
As of ECMA-262 5th Edition, "all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type", which is "representing the doubleprecision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic". The maximum safe integer in JavaScript is 253-1.
Example: 264-1 is 18446744073709551615 but in JavaScript it evaluates to 18446744073709552000.
Furthermore, bitwise operators in JavaScript "deal only with integers in the range −231 through 231−1, inclusive, or in the range 0 through 232−1, inclusive. These operators accept any value of the Number type but first convert each such value to one of 232 integer values."
In some use cases, however, it is required to be able to reliably work with and perform bitwise operations on the full 64 bits. This is where long.js comes into play.
The class is compatible with CommonJS and AMD loaders and is exposed globally as Long
if neither is available.
var Long = require("long");
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
...
number
, high: number
, unsigned?: boolean
)Long#low: number
The low 32 bits as a signed value.
Long#high: number
The high 32 bits as a signed value.
Long#unsigned: boolean
Whether unsigned or not.
Long.ZERO: Long
Signed zero.
Long.ONE: Long
Signed one.
Long.NEG_ONE: Long
Signed negative one.
Long.UZERO: Long
Unsigned zero.
Long.UONE: Long
Unsigned one.
Long.MAX_VALUE: Long
Maximum signed value.
Long.MIN_VALUE: Long
Minimum signed value.
Long.MAX_UNSIGNED_VALUE: Long
Maximum unsigned value.
Long.isLong(obj: *
): boolean
Tests if the specified object is a Long.
Long.fromBits(lowBits: number
, highBits: number
, unsigned?: boolean
): Long
Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.
Long.fromBytes(bytes: number[]
, unsigned?: boolean
, le?: boolean
): Long
Creates a Long from its byte representation.
Long.fromBytesLE(bytes: number[]
, unsigned?: boolean
): Long
Creates a Long from its little endian byte representation.
Long.fromBytesBE(bytes: number[]
, unsigned?: boolean
): Long
Creates a Long from its big endian byte representation.
Long.fromInt(value: number
, unsigned?: boolean
): Long
Returns a Long representing the given 32 bit integer value.
Long.fromNumber(value: number
, unsigned?: boolean
): Long
Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
Long.fromString(str: string
, unsigned?: boolean
, radix?: number
)
Long.fromString(str: string
, radix: number
)
Returns a Long representation of the given string, written using the specified radix.
Long.fromValue(val: *
, unsigned?: boolean
): Long
Converts the specified value to a Long using the appropriate from* function for its type.
Long#add(addend: Long | number | string
): Long
Returns the sum of this and the specified Long.
Long#and(other: Long | number | string
): Long
Returns the bitwise AND of this Long and the specified.
Long#compare/comp(other: Long | number | string
): number
Compares this Long's value with the specified's. Returns 0
if they are the same, 1
if the this is greater and -1
if the given one is greater.
Long#divide/div(divisor: Long | number | string
): Long
Returns this Long divided by the specified.
Long#equals/eq(other: Long | number | string
): boolean
Tests if this Long's value equals the specified's.
Long#getHighBits(): number
Gets the high 32 bits as a signed integer.
Long#getHighBitsUnsigned(): number
Gets the high 32 bits as an unsigned integer.
Long#getLowBits(): number
Gets the low 32 bits as a signed integer.
Long#getLowBitsUnsigned(): number
Gets the low 32 bits as an unsigned integer.
Long#getNumBitsAbs(): number
Gets the number of bits needed to represent the absolute value of this Long.
Long#greaterThan/gt(other: Long | number | string
): boolean
Tests if this Long's value is greater than the specified's.
Long#greaterThanOrEqual/gte/ge(other: Long | number | string
): boolean
Tests if this Long's value is greater than or equal the specified's.
Long#isEven(): boolean
Tests if this Long's value is even.
Long#isNegative(): boolean
Tests if this Long's value is negative.
Long#isOdd(): boolean
Tests if this Long's value is odd.
Long#isPositive(): boolean
Tests if this Long's value is positive.
Long#isZero/eqz(): boolean
Tests if this Long's value equals zero.
Long#lessThan/lt(other: Long | number | string
): boolean
Tests if this Long's value is less than the specified's.
Long#lessThanOrEqual/lte/le(other: Long | number | string
): boolean
Tests if this Long's value is less than or equal the specified's.
Long#modulo/mod/rem(divisor: Long | number | string
): Long
Returns this Long modulo the specified.
Long#multiply/mul(multiplier: Long | number | string
): Long
Returns the product of this and the specified Long.
Long#negate/neg(): Long
Negates this Long's value.
Long#not(): Long
Returns the bitwise NOT of this Long.
Long#notEquals/neq/ne(other: Long | number | string
): boolean
Tests if this Long's value differs from the specified's.
Long#or(other: Long | number | string
): Long
Returns the bitwise OR of this Long and the specified.
Long#shiftLeft/shl(numBits: Long | number | string
): Long
Returns this Long with bits shifted to the left by the given amount.
Long#shiftRight/shr(numBits: Long | number | string
): Long
Returns this Long with bits arithmetically shifted to the right by the given amount.
Long#shiftRightUnsigned/shru/shr_u(numBits: Long | number | string
): Long
Returns this Long with bits logically shifted to the right by the given amount.
Long#rotateLeft/rotl(numBits: Long | number | string
): Long
Returns this Long with bits rotated to the left by the given amount.
Long#rotateRight/rotr(numBits: Long | number | string
): Long
Returns this Long with bits rotated to the right by the given amount.
Long#subtract/sub(subtrahend: Long | number | string
): Long
Returns the difference of this and the specified Long.
Long#toBytes(le?: boolean
): number[]
Converts this Long to its byte representation.
Long#toBytesLE(): number[]
Converts this Long to its little endian byte representation.
Long#toBytesBE(): number[]
Converts this Long to its big endian byte representation.
Long#toInt(): number
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
Long#toNumber(): number
Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
Long#toSigned(): Long
Converts this Long to signed.
Long#toString(radix?: number
): string
Converts the Long to a string written in the specified radix.
Long#toUnsigned(): Long
Converts this Long to unsigned.
Long#xor(other: Long | number | string
): Long
Returns the bitwise XOR of this Long and the given one.
WebAssembly supports 64-bit integer arithmetic out of the box, hence a tiny WebAssembly module is used to compute operations like multiplication, division and remainder more efficiently (slow operations like division are around twice as fast), falling back to floating point based computations in JavaScript where WebAssembly is not yet supported, e.g., in older versions of node.
To build an UMD bundle to dist/long.js
, run:
$> npm install
$> npm run build
Running the tests:
$> npm test
FAQs
A Long class for representing a 64-bit two's-complement integer value.
We found that @xtuc/long demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.