import type { NextRequest } from 'next/server' ; export async function middleware ( req ) { const plainText = 'Hello from the Edge!' ; const password = 'hunter2' ; const ptUtf8 = new TextEncoder ( ) . encode ( plainText ) ; const pwUtf8 = new TextEncoder ( ) . encode ( password ) ; const pwHash = await crypto . subtle . digest ( 'SHA-256' , pwUtf8 ) ; const iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ; const alg = { name : 'AES-GCM' , iv : iv } ; const encrpytKey = await crypto . subtle . importKey ( 'raw' , pwHash , alg , false , [ 'encrypt' , ] ) ; const encrypted = await crypto . subtle . encrypt ( alg , encrpytKey , ptUtf8 ) ; const decryptKey = await crypto . subtle . importKey ( 'raw' , pwHash , alg , false , [ 'decrypt' , ] ) ; const ptBuffer = await crypto . subtle . decrypt ( alg , decryptKey , encrypted ) ; const decryptedText = new TextDecoder ( ) . decode ( ptBuffer ) ; return new Response ( JSON . stringify ( { uuid : crypto . randomUUID ( ) , randomValues : crypto . getRandomValues ( new Uint32Array ( 10 ) ) , plainText , password , decryptedText , iv , } ) , { headers : { 'Content-Type' : 'application/json' } } ) ; }