diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | packages/bun-types/types.d.ts | 57 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 1 | ||||
-rw-r--r-- | src/javascript/jsc/node/types.zig | 7 |
4 files changed, 58 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore index 91b8fe02d..8012f5ee4 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,4 @@ src/deps/PLCrashReporter/ *.dSYM *.crash +misctools/sha diff --git a/packages/bun-types/types.d.ts b/packages/bun-types/types.d.ts index 528b3c0bf..ae66ecea3 100644 --- a/packages/bun-types/types.d.ts +++ b/packages/bun-types/types.d.ts @@ -877,7 +877,7 @@ declare module "bun" { * * This hashing function balances speed with cryptographic strength. This does not encrypt or decrypt data. * - * The implementation uses [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) * * The equivalent `openssl` command is: * @@ -897,7 +897,7 @@ declare module "bun" { * * This hashing function balances speed with cryptographic strength. This does not encrypt or decrypt data. * - * The implementation uses [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) * * The equivalent `openssl` command is: * @@ -909,9 +909,14 @@ declare module "bun" { export function sha(input: StringOrBuffer, encoding: DigestEncoding): string; /** - * This is not the default because it's not cryptographically secure and it's slower than {@link SHA512} * - * Consider using the ugly-named {@link SHA512_256} instead + * Hashing functions for SHA-1 + * + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + * + * SHA-1 is no longer cryptographically secure and it's slower than {@link SHA512}. + * + * Consider using the {@link SHA512_256} instead. */ export class SHA1 extends CryptoHashInterface<SHA1> { constructor(); @@ -921,6 +926,18 @@ declare module "bun" { */ static readonly byteLength: 20; } + + /** + * + * Hashing functions for MD5 + * + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + * + * If you're looking for a fast hashing function, consider {@link hash} + * instead. This is not cryptographically secure and it's slower than + * {@link hash}. The best reason to use it is compatibility. + * + */ export class MD5 extends CryptoHashInterface<MD5> { constructor(); @@ -929,6 +946,18 @@ declare module "bun" { */ static readonly byteLength: 16; } + + /** + * + * Ancient hashing function. Bun maybe shouldn't have included this. + * + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + * + * If you're looking for a fast hashing function, consider {@link hash} + * instead. This is not cryptographically secure and it's slower than + * {@link hash}. The best reason to use it is compatibility. + * + */ export class MD4 extends CryptoHashInterface<MD4> { constructor(); @@ -937,6 +966,11 @@ declare module "bun" { */ static readonly byteLength: 16; } + /** + * Smaller variant of SHA-2 + * + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + */ export class SHA224 extends CryptoHashInterface<SHA224> { constructor(); @@ -945,6 +979,11 @@ declare module "bun" { */ static readonly byteLength: 28; } + /** + * Faster variant of SHA-2, but with bigger output (64 bytes) + * + * Powered by [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go) + */ export class SHA512 extends CryptoHashInterface<SHA512> { constructor(); @@ -961,6 +1000,13 @@ declare module "bun" { */ static readonly byteLength: 48; } + /** + * SHA-2 (Secure Hash Algorithm 2) is a set of cryptographic hash functions + * designed by the United States National Security Agency (NSA) and first + * published in 2001 + * + * {@link https://en.wikipedia.org/wiki/SHA-2} + */ export class SHA256 extends CryptoHashInterface<SHA256> { constructor(); @@ -970,6 +1016,9 @@ declare module "bun" { static readonly byteLength: 32; } /** + * + * Fast variant of SHA-512, but with smaller output (32 bytes) + * * See also {@link sha} */ export class SHA512_256 extends CryptoHashInterface<SHA512_256> { diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 2d384a924..35f565bc2 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -136,6 +136,7 @@ pub const ZigString = extern struct { return @intToPtr([*]u8, @ptrToInt(this.ptr))[0..this.len]; } + /// Does nothing if the slice is not allocated pub fn deinit(this: *const Slice) void { if (!this.allocated) { return; diff --git a/src/javascript/jsc/node/types.zig b/src/javascript/jsc/node/types.zig index 1bf44e211..36978f7d4 100644 --- a/src/javascript/jsc/node/types.zig +++ b/src/javascript/jsc/node/types.zig @@ -190,10 +190,9 @@ pub const Encoding = enum(u8) { const Eight = strings.ExactSizeMatcher(8); /// Caller must verify the value is a string pub fn fromStringValue(value: JSC.JSValue, global: *JSC.JSGlobalObject) ?Encoding { - var str = JSC.ZigString.Empty; - value.toZigString(&str, global); - const slice = str.slice(); - return from(slice); + var sliced = value.toSlice(global, bun.default_allocator); + defer sliced.deinit(); + return from(sliced.slice()); } /// Caller must verify the value is a string |