GeezerVison is showing 1time.io — 1time for Developers — One-Time Secret API and Protocol Spec Build with 1time Zero-knowledge temporary storage for passing secrets between people and agents. Encrypted one-time links; the server holds nothing but ciphertext. Save scheme v3 · Updated 2026-11-15 Give this to your coding agent agent-spec.md # Build a 1time.io client
> Paste this whole document into your coding agent. It is the complete spec for creating and reading 1time.io one-time links from any language.
> Canonical implementation: https://github.com/shingrus/1time/blob/master/frontend/src/lib/protocol.mjs
Save scheme v3 · API version 1
## What you are building
1time.io is zero-knowledge. All encryption happens in your client. The server stores ciphertext and a hash; it never receives the plaintext, the decryption key, the passphrase, or anything that can read the secret. The key travels only in the link's URL fragment (`#…`), which is never sent to a server.
Implement:
1. `createTextLink(text, {expiresInSeconds, views, passphrase?, host?}) → link`
2. `readTextLink(link, {passphrase?}) → {text, viewsLeft, expiresIn}`
3. `createFileLink(bytes, {name, type, expiresInSeconds, views, passphrase?, host?}) → link`
4. `readFileLink(link, {passphrase?}) → {name, type, size, bytes, viewsLeft}`
5. `checkStatus(host, ids) → {id: exists}` (optional)
6. `checkCompatibility(host) → bool` (recommended)
## Hard rules
- Save with `v: 3` and `readTokenHash`. Never send `hashedKey` on a save. That is the deprecated v2 scheme: it uploads a value that can read and destroy the secret.
- Generate `randomKey` with a cryptographically secure RNG.
- Never send `randomKey`, the passphrase, or a full link to any server, log, analytics or crash report. The key belongs only in the URL fragment, never in a path or query string.
- `/api/get` and `/api/getFile` are destructive: every successful call uses up a view. Retry only when the response is HTTP `503` and the JSON body is `{"status":"retry"}`. Never retry on a timeout, a network error, or any other status: the view may already be gone.
- HTTPS only. Plain `http://` is acceptable only for loopback hosts (`127.0.0.1`, `localhost`, `::1`).
- Identify your client (see section 6).
## 1. Keys
Alphabet (64 characters):
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
1. `randomKey`: 20 characters. For each secure random byte, take `alphabet[byte % 64]` (unbiased, because 256 is a multiple of 64).
2. `fullSecretKey = passphrase + randomKey`: plain string concatenation, passphrase first; use `""` when there is no passphrase.
3. HKDF-SHA256 (RFC 5869, extract-then-expand) with `ikm = UTF-8(fullSecretKey)`, `salt = UTF-8("onetimelink:v2")`, output length 32 bytes:
- `info = "encrypt"` gives the AES-256-GCM key
- `info = "auth"` gives 32 bytes; lowercase hex of them is `readToken` (64 characters)
4. `readTokenHash = lowercase_hex(SHA-256(UTF-8(readToken)))`
The most common porting bug: `readTokenHash` hashes the 64-character hex string, not the 32 raw bytes it encodes. Get this wrong and every link you create answers `wrong key`.
The salt says `v2` even under save scheme v3. That is correct; do not change it.
Library pointers: Go `crypto/hkdf` (1.24+), Python `cryptography.hazmat.primitives.kdf.hkdf.HKDF`, Swift CryptoKit `HKDF<SHA256>`, Kotlin/Java BouncyCastle `HKDFBytesGenerator`, Node and browsers WebCrypto `HKDF`.
## 2. Encryption
AES-256-GCM, a fresh random 12-byte IV per payload, 128-bit tag appended to the ciphertext, no associated data.
Text. Plaintext is `UTF-8(text)`. The wire value is a string:
base64url(iv) + "." + base64url(ciphertext || tag)
base64url uses `-` and `_`, without `=` padding. To decrypt, split on `.`.
Files. Plaintext is a packed blob:
uint32_be(len(meta)) || meta || fileBytes
where `meta` is UTF-8 JSON such as `{"name":"report.pdf","type":"application/pdf","size":1234}` (`size` is the length of `fileBytes`; use `application/octet-stream` when the type is unknown).
The wire value is raw bytes: `iv || ciphertext || tag`, with no base64 and no dot. To decrypt, the first 12 bytes are the IV.
## 3. Links
- Text: `https://{host}/v/#{randomKey}{id}`
- File: `https://{host}/f/#{randomKey}{id}`
- `id` is returned by the server: 22 base64url characters.
- The fragment is exactly 42 alphabet characters: the first 20 are `randomKey`, the last 22 are `id`. Reject anything else as a broken link before calling the API; otherwise a truncated link looks exactly like an already-read secret.
- The passphrase is never part of the link. The recipient receives it through a separate channel.
- A link's origin is its API host. Self-hosted instances use the same paths.
## 4. HTTP API
Base URL: `https://1time.io/api/`, or any self-hosted origin. Every endpoint is `POST`; other methods answer `405`. Append `?src=<app-short-name>` to every request and set a matching `User-Agent`, e.g. `https://1time.io/api/saveSecret?src=webssh` with `User-Agent: WebSSH/32.8`. Use `?src=dev` until your client has a name, and include a contact URL in the `User-Agent` (see section 6).
Most outcomes, including failures, return HTTP `200`: always check the `status` field.
### POST /api/saveSecret
`Content-Type: application/json`
{
"secretMessage": "<base64url iv>.<base64url ciphertext+tag>",
"readTokenHash": "<64 lowercase hex>",
"v": 3,
"duration": 86400,
"views": 1
}
- `duration`: seconds, `1` to `2592000` (30 days). Missing or out of range silently becomes `86400`.
- `views`: `1` to `10`, optional, default `1`. Out of range is silently clamped.
Response: `{"status":"ok","newId":"<22 chars>"}`, or `{"status":"error","newId":"0"}` for a malformed body, wrong scheme fields, an empty message, or a server error. No reason is given.
Validate `duration` and `views` yourself and reject bad values. The server corrects them silently, so a "90-day" link would expire in one day.
### POST /api/saveFile
`multipart/form-data` fields:
- `file`: encrypted bytes (any filename, e.g. `encrypted.bin`)
- `readTokenHash`: 64 lowercase hex
- `v`: `3`
- `duration`: seconds, same rule as above
- `views`: `1` to `10`, optional
Response: same as `saveSecret`.
Chunked upload, recommended for anything over a few MiB: a dropped connection then costs one chunk instead of the whole file. Encrypt the file exactly as above, cut the encrypted bytes into 4 MiB slices, and send each slice, in order, as its own request with the same fields, the slice as `file`, and three query parameters:
POST /api/saveFile?src=<app>&u=<uploadId>&i=<index>&n=<chunkCount>
- `u`: an upload id you generate, 22 characters from the key alphabet (secure RNG). One per file.
- `i`: `0` to `n − 1`. `n`: `1` to `63`. Every slice but the last must be exactly 4 MiB (4194304 bytes).
- `readTokenHash`, `v`, `duration`, `views` and `n` must be the same on every slice; a slice that differs is rejected, and a missing or out-of-range `duration`, or an out-of-range `views`, gets `400` instead of being clamped.
- Slices before the last answer `200 {"status":"ok"}`. The request that completes the file answers `200 {"status":"ok","newId":"<22 chars>"}`; until then the file does not exist and no link can be built. The stored file is the same encrypted blob a single request would have stored.
- Resending a slice is safe as long as its bytes are identical: it lands in the same place, and for an hour after the file is complete any slice returns the same `newId`. A slice index accepts only the bytes first sent for it; different bytes get `400`, so if you re-encrypt the file (for example after a restart), start a new upload with a new `u`. After that hour the upload id is forgotten, and a resend starts a new upload with a new `newId`. Retry a slice after a network error, a timeout, `408`, `429` or any `5xx` (including Cloudflare's `52x`, and `503 {"status":"retry"}` while another request is finishing the file), with backoff. Do not retry `400 {"status":"error"}`: the slice or its fields are wrong.
- An upload unfinished for an hour after its last slice is discarded.
- A host that predates chunked uploads answers the first slice with a `newId`. If any slice before the last returns one, send the whole encrypted file in one request instead.
### POST /api/get (destructive)
{"id": "<22 chars>", "hashedKey": "<readToken, 64 lowercase hex>"}
A read sends the `readToken` itself, in a field historically named `hashedKey`. Reads are identical for every save scheme.
- `200 {"status":"ok","cryptedMessage":…,"viewsLeft":…,"expiresIn":…}`: decrypt `cryptedMessage`. `viewsLeft` is what remains after this read (`0` means it is now deleted); `expiresIn` is seconds. Uses a view.
- `200 {"status":"wrong key"}`: passphrase missing or wrong. Ask for it and try again. Does not use a view.
- `200 {"status":"no message"}`: already read, expired, or never existed.
- `200 {"status":"error"}`: malformed request or server error. Do not retry.
- `503 {"status":"retry"}`: contention; rejected before anything was used. Safe to retry.
### POST /api/getFile (destructive)
Same request body as `/api/get`. Success is binary and every failure is JSON, so branch on `Content-Type`, not on the status code alone.
- Success: `200`, `Content-Type: application/octet-stream`, body is the encrypted bytes. Headers: `X-1Time-Views-Left`, and `X-1Time-Expires-In` (seconds, may be absent).
- `200` JSON `{"status":"wrong key"}` or `{"status":"no message"}`: as for `/api/get`.
- `400 {"status":"error"}` (`application/json`): malformed `id` or `hashedKey`.
- `503 {"status":"retry"}` (`application/json`): safe to retry.
- `500 {"status":"error"}` (`application/json`): server error; do not retry.
### POST /api/secretStatus (never uses a view)
{"ids": ["<22 chars>", "..."]}
Up to 128 ids; extra and malformed ids are silently dropped.
Response: `{"status":"ok","secrets":{"<id>":true}}`. `true` means still available, `false` means read or expired.
### POST /api/ss (capabilities)
No body. The response includes `{"apiVersion":1,"saveSchemes":[0,3]}`; other fields are statistics, ignore them.
Before saving, confirm `saveSchemes` contains `3`. If it does not, the host is too old: fail with a clear error. Cache the result per host.
## 5. Limits (hosted 1time.io)
- Expiry: 1 second to 30 days, default 1 day
- Views or downloads: 1 to 10, default 1
- `saveSecret` request body: 25 MiB (about 18 MiB of plaintext after encryption and base64)
- File size (plaintext): just under 250 MiB; 64 KiB is reserved for the IV, tag and multipart overhead. A chunked upload is at most 63 slices of 4 MiB. A single-request (multipart) upload is capped at exactly 100 MiB of body by the CDN, so files over about 100 MiB must be sent in slices
- `get` and `getFile` request body: 1 KiB
- `secretStatus`: 128 ids, 8 KiB
- Rate limit, text saves: about 45 per minute per IP, burst 10, then `429`
- Rate limit, `saveFile` requests (each slice counts): about 120 per minute per IP, burst 63, then `429`
- Rate limit, reads: about 60 per minute per IP, burst 15, then `429`
- Rate limit, `secretStatus`: about 240 per minute per IP, burst 30, then `429`
On `429` for a save: back off and retry. On `429` for a read: surface it to the user rather than retrying automatically.
Browsers: the hosted API does not send CORS headers, so JavaScript on another website cannot call it. Call it from a native app, a server, a CLI, or a browser extension's background context.
## 6. Attribution
Self-reported and never used for access control. It lets us see your integration's traffic and reach you before a protocol change.
- Pick a short lowercase name for your app, the way the existing clients do: `cli` (terminal client), `ext` (browser extension), `zap` (Zapier app), `webssh` (WebSSH). Send it on every API URL as `?src=<app-short-name>`, and use the same name in `User-Agent: <ClientName>/<version>`, e.g. `?src=webssh` with `WebSSH/32.8`.
- Until your client has a name, send `?src=dev` with `User-Agent: 1time-dev-client/0.1`. Both mark a client written from this spec; replace them before you release.
- Names already taken: `dev`, `cli`, `ext`, `zap`, `webssh`.
- Always set your own User-Agent: generic library defaults (`python-requests`, `Go-http-client`, `node`) are indistinguishable from scripts and scanners.
- Put a contact URL in the User-Agent comment, the way crawlers do: `User-Agent: MyApp/1.2 (+https://myapp.example)`. A homepage, a repository or a `mailto:` address all work. Nothing else in a request says who you are, so this is the only way we can reach you before a protocol change.
- Built something? Tell us at https://github.com/shingrus/1time/issues and we will list it on https://1time.io/developers/
## 7. Test vectors
Produced by the canonical `protocol.mjs`; V1 is also pinned in the Go backend tests. Encryption uses a random IV, so it is not deterministic: test your encryption by round-tripping it through your decryption, then against the live API.
V1: key derivation, no passphrase
fullSecretKey K7bQ2mXp9vRt4nLw8zYc
readToken 5e6c13f429c1e6eb0c69bc2e67e98e7aa18db0a4bac73f943858694ac3fbe957
readTokenHash 3bfecb0a1d1bc37a3e70eb843af6578ee1fa34c19fd059a89dfbdda4523bf396
V2: key derivation with passphrase "correct horse"
fullSecretKey correct horseK7bQ2mXp9vRt4nLw8zYc
readToken 236c0385cdff9e6707c0ab1d20f936c0e6ab75dc5ce09170ca528cd39cc3a10f
readTokenHash 323a850509f83d441ac10a8383cccbe356044213dd6439e9c7375eb788f35acc
V3: text decryption
fullSecretKey K7bQ2mXp9vRt4nLw8zYc
cryptedMessage 9uCJlv0mLOve6cWh.-Z5xKVVauhg2NAM1n-AjlKZsT4m5sWjAYgKGql9AIl0
plaintext hello from 1time
V4: file decryption (86 bytes, shown as standard base64)
fullSecretKey K7bQ2mXp9vRt4nLw8zYc
encrypted uGUZGLv58lm1RelH+ZZA4pRL3caBNF/dcugFY6hiiHORfAi88QNuc90luBF+J3EGdFOWymIH6cMAK+qXiKakZvex55ygVWLjqazgBG2eCDYw2BDTSdA=
meta {"name":"hello.txt","type":"text/plain","size":5}
content hello
V5: link parsing
link https://1time.io/v/#K7bQ2mXp9vRt4nLw8zYcAbCdEfGhIjKlMnOpQrStUv
origin https://1time.io
randomKey K7bQ2mXp9vRt4nLw8zYc
id AbCdEfGhIjKlMnOpQrStUv
## 8. Done checklist
- [ ] V1 to V5 pass as unit tests
- [ ] Saves send `v: 3` and `readTokenHash`, never `hashedKey`
- [ ] Live round trip on https://1time.io: a text link and a file link created by your client open correctly in a browser
- [ ] Links created in the 1time.io web app, with and without a passphrase, open in your client
- [ ] A second read returns `no message`
- [ ] A wrong passphrase returns `wrong key`, and the correct one still works afterwards
- [ ] Reads retry only on `503` with `{"status":"retry"}`
- [ ] `duration` and `views` are validated client-side
- [ ] `?src=` and `User-Agent` are set: `?src=dev` and `1time-dev-client/0.1` while building, your app's short name before release
- [ ] The released `User-Agent` carries a contact URL: `MyApp/1.2 (+https://myapp.example)`
- [ ] No key, passphrase, readToken or link appears in logs, analytics or crash reports
## Versioning
Current save scheme: v3. Scheme 0 (v2, `hashedKey` on save) is still accepted so old clients keep working, but it is deprecated; new clients must not use it. Check `/api/ss` for what a host supports.
PowerShell: ready-made client A tested script for creating and reading one-time text links, using only what ships with PowerShell 7.2 or later. Windows PowerShell 5.1 is not supported: it has no AES-GCM. Save it as 1time.ps1, set your contact URL at the top, then: usage . ./1time.ps1
$link = 'my secret' | New-OneTimeLink -ExpiresInSeconds 3600
(Read-OneTimeLink $link).Text 1time.ps1 #Requires -Version 7.2
# Put your own contact URL here (homepage, repository or mailto:); it is sent in the User-Agent.
$OneTimeContactUrl = ''
function ConvertTo-OneTimeBase64Url([byte[]]$Bytes) {
[Convert]::ToBase64String($Bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
}
function ConvertFrom-OneTimeBase64Url([string]$Text) {
$standard = $Text.Replace('-', '+').Replace('_', '/')
[Convert]::FromBase64String($standard.PadRight($standard.Length + (4 - $standard.Length % 4) % 4, '='))
}
function New-OneTimeRandomKey {
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
$random = [Security.Cryptography.RandomNumberGenerator]
-join (1..20 | ForEach-Object { $alphabet[$random::GetInt32($alphabet.Length)] })
}
function Get-OneTimeKeys([string]$Passphrase, [string]$RandomKey) {
$utf8 = [Text.Encoding]::UTF8
$material = $utf8.GetBytes($Passphrase + $RandomKey)
$salt = $utf8.GetBytes('onetimelink:v2')
$sha256 = [Security.Cryptography.HashAlgorithmName]::SHA256
$authBytes = [Security.Cryptography.HKDF]::DeriveKey($sha256, $material, 32, $salt, $utf8.GetBytes('auth'))
$readToken = [Convert]::ToHexString($authBytes).ToLowerInvariant()
$aesKey = [Security.Cryptography.HKDF]::DeriveKey($sha256, $material, 32, $salt, $utf8.GetBytes('encrypt'))
$hashBytes = [Security.Cryptography.SHA256]::HashData($utf8.GetBytes($readToken))
[pscustomobject]@{
AesKey = $aesKey
ReadToken = $readToken
ReadTokenHash = [Convert]::ToHexString($hashBytes).ToLowerInvariant()
}
}
function ConvertTo-OneTimeCiphertext([string]$Text, [byte[]]$AesKey) {
$plain = [Text.Encoding]::UTF8.GetBytes($Text)
$iv = [Security.Cryptography.RandomNumberGenerator]::GetBytes(12)
$cipher = [byte[]]::new($plain.Length)
$tag = [byte[]]::new(16)
$aes = [Security.Cryptography.AesGcm]::new($AesKey)
try { $aes.Encrypt($iv, $plain, $cipher, $tag) } finally { $aes.Dispose() }
(ConvertTo-OneTimeBase64Url $iv) + '.' + (ConvertTo-OneTimeBase64Url ([byte[]]($cipher + $tag)))
}
function ConvertFrom-OneTimeCiphertext([string]$Wire, [byte[]]$AesKey) {
$ivText, $bodyText = $Wire.Split('.')
$iv = ConvertFrom-OneTimeBase64Url $ivText
$body = ConvertFrom-OneTimeBase64Url $bodyText
$cipher = [byte[]]::new($body.Length - 16)
$tag = [byte[]]::new(16)
[Array]::Copy($body, $cipher, $cipher.Length)
[Array]::Copy($body, $cipher.Length, $tag, 0, 16)
$plain = [byte[]]::new($cipher.Length)
$aes = [Security.Cryptography.AesGcm]::new($AesKey)
try { $aes.Decrypt($iv, $cipher, $tag, $plain) } finally { $aes.Dispose() }
[Text.Encoding]::UTF8.GetString($plain)
}
function Get-OneTimeOrigin([string]$Url) {
$uri = $null
if (-not [Uri]::TryCreate($Url, 'Absolute', [ref]$uri) -or $uri.Scheme -notin 'http', 'https') {
throw 'Expected an http(s) URL.'
}
if ($uri.Scheme -eq 'http' -and $uri.Host -notin '127.0.0.1', 'localhost', '[::1]') {
throw 'Plain http is only allowed for loopback hosts; use https.'
}
$uri.GetLeftPart('Authority')
}
function ConvertFrom-OneTimeLink([string]$Link) {
$origin = Get-OneTimeOrigin $Link
$uri = [Uri]$Link
$fragment = $uri.Fragment.TrimStart('#')
if ($uri.AbsolutePath -cne '/v/' -or $fragment -cnotmatch '^[A-Za-z0-9_-]{42}\z') {
throw 'Not a 1time text link: expected /v/# followed by 42 characters.'
}
[pscustomobject]@{ Origin = $origin; RandomKey = $fragment.Substring(0, 20); Id = $fragment.Substring(20) }
}
function Invoke-OneTimeApi([string]$Origin, [string]$Endpoint, [hashtable]$Body) {
$userAgent = if ($OneTimeContactUrl) { "1time-pwsh/1.0 (+$OneTimeContactUrl)" } else { '1time-pwsh/1.0' }
$request = @{
Method = 'Post'
Uri = "$Origin/api/${Endpoint}?src=pwsh"
Body = $Body | ConvertTo-Json -Compress
ContentType = 'application/json'
UserAgent = $userAgent
TimeoutSec = 60
SkipHttpErrorCheck = $true
StatusCodeVariable = 'statusCode'
}
for ($attempt = 1; ; $attempt++) {
$response = Invoke-RestMethod @request
if ($statusCode -eq 503 -and $response.status -eq 'retry' -and $attempt -lt 4) {
Start-Sleep -Milliseconds (250 * $attempt)
continue
}
if ($statusCode -ne 200) { throw "1time API answered HTTP $statusCode." }
return $response
}
}
function New-OneTimeLink {
param(
[Parameter(Mandatory, ValueFromPipeline)][string]$Secret,
[ValidateRange(1, 2592000)][int]$ExpiresInSeconds = 86400,
[ValidateRange(1, 10)][int]$Views = 1,
[string]$Passphrase = '',
[string]$HostUrl = 'https://1time.io'
)
process {
$origin = Get-OneTimeOrigin $HostUrl
$randomKey = New-OneTimeRandomKey
$keys = Get-OneTimeKeys $Passphrase $randomKey
$response = Invoke-OneTimeApi $origin 'saveSecret' @{
secretMessage = ConvertTo-OneTimeCiphertext $Secret $keys.AesKey
readTokenHash = $keys.ReadTokenHash
v = 3
duration = $ExpiresInSeconds
views = $Views
}
if ($response.status -ne 'ok' -or $response.newId -cnotmatch '^[A-Za-z0-9_-]{22}\z') {
throw 'The server did not accept the secret.'
}
"$origin/v/#$randomKey$($response.newId)"
}
}
function Read-OneTimeLink {
param(
[Parameter(Mandatory)][string]$Link,
[string]$Passphrase = ''
)
$parts = ConvertFrom-OneTimeLink $Link
$keys = Get-OneTimeKeys $Passphrase $parts.RandomKey
$response = Invoke-OneTimeApi $parts.Origin 'get' @{ id = $parts.Id; hashedKey = $keys.ReadToken }
switch ($response.status) {
'ok' {
[pscustomobject]@{
Text = ConvertFrom-OneTimeCiphertext $response.cryptedMessage $keys.AesKey
ViewsLeft = [int]$response.viewsLeft
ExpiresIn = [int]$response.expiresIn
}
}
'wrong key' { throw 'Wrong key: the passphrase is missing or incorrect.' }
'no message' { throw 'No message: already read, expired or never existed.' }
default { throw 'The server could not process the request.' }
}
}
Google Apps Script: ready-made client The same two functions for Apps Script. This one is not end-to-end zero-knowledge: the script runs on Google's servers, so the secret and its key pass through Google. Apps Script has no Web Crypto, so AES-256-GCM comes from a bundled copy of noble-ciphers and random keys are built from Utilities.getUuid(). Paste it into a script file, then: usage const link = createOneTimeLink('my secret', {expiresInSeconds: 3600});
const secret = readOneTimeLink(link).text; 1time.gs function oneTimeUnsigned_(signed) {
return Uint8Array.from(signed);
}
function oneTimeSigned_(bytes) {
return Array.from(new Int8Array(bytes.buffer, bytes.byteOffset, bytes.length));
}
function oneTimeUtf8_(text) {
return oneTimeUnsigned_(Utilities.newBlob(text).getBytes());
}
function oneTimeHex_(bytes) {
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
}
function oneTimeBase64Url_(bytes) {
return Utilities.base64EncodeWebSafe(oneTimeSigned_(bytes)).replace(/=+$/, '');
}
function oneTimeFromBase64Url_(text) {
return oneTimeUnsigned_(Utilities.base64DecodeWebSafe(text + '='.repeat((4 - (text.length % 4)) % 4)));
}
function oneTimeHmac_(key, message) {
return oneTimeUnsigned_(Utilities.computeHmacSha256Signature(oneTimeSigned_(message), oneTimeSigned_(key)));
}
// No secure random API exists in Apps Script; Utilities.getUuid() (Java's UUID.randomUUID) is the only source.
function oneTimeRandomBytes_(count) {
const uuids = Utilities.getUuid() + Utilities.getUuid() + Utilities.getUuid();
return oneTimeUnsigned_(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, uuids)).slice(0, count);
}
function oneTimeRandomKey_() {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
return Array.from(oneTimeRandomBytes_(20), (byte) => alphabet[byte & 63]).join('');
}
function oneTimeHkdf_(material, info) {
const pseudoRandomKey = oneTimeHmac_(oneTimeUtf8_('onetimelink:v2'), material);
return oneTimeHmac_(pseudoRandomKey, Uint8Array.from([...oneTimeUtf8_(info), 1]));
}
function oneTimeKeys_(passphrase, randomKey) {
const material = oneTimeUtf8_(passphrase + randomKey);
const readToken = oneTimeHex_(oneTimeHkdf_(material, 'auth'));
const hashBytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, readToken);
return {
aesKey: oneTimeHkdf_(material, 'encrypt'),
readToken,
readTokenHash: oneTimeHex_(oneTimeUnsigned_(hashBytes)),
};
}
function oneTimeEncrypt_(text, aesKey) {
const iv = oneTimeRandomBytes_(12);
const sealed = oneTimeAes_.gcm(aesKey, iv).encrypt(oneTimeUtf8_(text));
return oneTimeBase64Url_(iv) + '.' + oneTimeBase64Url_(sealed);
}
function oneTimeDecrypt_(wire, aesKey) {
const [ivText, bodyText] = wire.split('.');
const plain = oneTimeAes_.gcm(aesKey, oneTimeFromBase64Url_(ivText)).decrypt(oneTimeFromBase64Url_(bodyText));
return Utilities.newBlob(oneTimeSigned_(plain)).getDataAsString();
}
function oneTimeOrigin_(host) {
const match = /^(https:\/\/[A-Za-z0-9.-]+(?::\d{1,5})?)\/?$/.exec(host);
if (!match) throw new Error('Host must be an https URL without a path.');
return match[1];
}
function oneTimeParseLink_(link) {
const match = /^(https:\/\/[A-Za-z0-9.-]+(?::\d{1,5})?)\/v\/#([A-Za-z0-9_-]{42})$/.exec(String(link).trim());
if (!match) throw new Error('Not a 1time text link: expected https://host/v/# followed by 42 characters.');
return {origin: match[1], randomKey: match[2].slice(0, 20), id: match[2].slice(20)};
}
function oneTimeApi_(origin, endpoint, body) {
const request = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(body),
muteHttpExceptions: true,
};
for (let attempt = 1; ; attempt++) {
const response = UrlFetchApp.fetch(`${origin}/api/${endpoint}?src=gas`, request);
const code = response.getResponseCode();
let json = null;
try { json = JSON.parse(response.getContentText()); } catch (error) {}
if (code === 503 && json && json.status === 'retry' && attempt < 4) {
Utilities.sleep(250 * attempt);
continue;
}
if (code !== 200) throw new Error(`1time API answered HTTP ${code}.`);
return json || {};
}
}
function createOneTimeLink(secret, options) {
const {expiresInSeconds = 86400, views = 1, passphrase = '', host = 'https://1time.io'} = options || {};
if (typeof secret !== 'string' || !secret) throw new Error('Secret must be a non-empty string.');
if (!Number.isInteger(expiresInSeconds) || expiresInSeconds < 1 || expiresInSeconds > 2592000) {
throw new Error('expiresInSeconds must be an integer from 1 to 2592000.');
}
if (!Number.isInteger(views) || views < 1 || views > 10) throw new Error('views must be an integer from 1 to 10.');
const origin = oneTimeOrigin_(host);
const randomKey = oneTimeRandomKey_();
const keys = oneTimeKeys_(passphrase, randomKey);
const response = oneTimeApi_(origin, 'saveSecret', {
secretMessage: oneTimeEncrypt_(secret, keys.aesKey),
readTokenHash: keys.readTokenHash,
v: 3,
duration: expiresInSeconds,
views,
});
if (response.status !== 'ok' || !/^[A-Za-z0-9_-]{22}$/.test(response.newId)) {
throw new Error('The server did not accept the secret.');
}
return `${origin}/v/#${randomKey}${response.newId}`;
}
function readOneTimeLink(link, passphrase = '') {
const parts = oneTimeParseLink_(link);
const keys = oneTimeKeys_(passphrase, parts.randomKey);
const response = oneTimeApi_(parts.origin, 'get', {id: parts.id, hashedKey: keys.readToken});
if (response.status === 'ok') {
return {
text: oneTimeDecrypt_(response.cryptedMessage, keys.aesKey),
viewsLeft: Number(response.viewsLeft),
expiresIn: Number(response.expiresIn),
};
}
if (response.status === 'wrong key') throw new Error('Wrong key: the passphrase is missing or incorrect.');
if (response.status === 'no message') throw new Error('No message: already read, expired or never existed.');
throw new Error('The server could not process the request.');
}
// AES-256-GCM below is @noble/ciphers 2.4.0 (aes.js gcm), unmodified, bundled with esbuild.
// MIT License, (c) 2022 Paul Miller (paulmillr.com), (c) 2016 Thomas Pornin. https://github.com/paulmillr/noble-ciphers
var oneTimeAes_=(()=>{var z=Object.defineProperty;var at=Object.getOwnPropertyDescriptor;var gt=Object.getOwnPropertyNames;var pt=Object.prototype.hasOwnProperty;var yt=(t,e,n)=>e in t?z(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var wt=(t,e)=>{for(var n in e)z(t,n,{get:e[n],enumerable:!0})},dt=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of gt(e))!pt.call(t,r)&&r!==n&&z(t,r,{get:()=>e[r],enumerable:!(o=at(e,r))||o.enumerable});return t};var bt=t=>dt(z({},"__esModule",{value:!0}),t);var b=(t,e,n)=>yt(t,typeof e!="symbol"?e+"":e,n);var Kt={};wt(Kt,{gcm:()=>ft});function W(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"&&"BYTES_PER_ELEMENT"in t&&t.BYTES_PER_ELEMENT===1}var M=t=>t?`"${t}" `:"";function xt(t,e=""){if(typeof t!="boolean")throw new TypeError(M(e)+"expected boolean, got type="+typeof t);return t}function N(t,e=""){if(typeof t!="number")throw new TypeError(M(e)+"expected number, got "+typeof t);if(!Number.isSafeInteger(t)||t<0)throw new RangeError(M(e)+"expected integer >= 0, got "+t);return t}function w(t,e,n=""){if(W(t)&&(e===void 0||t.length===e))return t;e!==void 0&&N(e,"length");let o=W(t),r=e!==void 0?` of length ${e}`:"",i=o?`length=${t.length}`:`type=${typeof t}`,f=M(n)+"expected Uint8Array"+r+", got "+i;throw o?new RangeError(f):new TypeError(f)}function R(t,e=!0){if(t.destroyed)throw new Error("hash was destroyed");if(e&&t.finished)throw new Error("digest() was already called")}function Et(t,e){w(t,void 0,"output");let n=e.outputLen;if(!(t.length>=n))throw new RangeError('"output" expected length >= '+n)}function H(t,e){if(Et(t,e),!I(t))throw new Error("invalid output, must be aligned")}function X(t){return new Uint8Array(t.buffer,t.byteOffset,t.byteLength)}function U(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}function L(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function v(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}var C=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function q(t){return t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255}var p=C?t=>t:t=>q(t)>>>0;function At(t){for(let e=0;e<t.length;e++)t[e]=q(t[e]);return t}var O=C?t=>t:At;function tt(t,e){if(t=w(t),e=w(e),t.length!==e.length)return!1;let n=0;for(let o=0;o<t.length;o++)n|=t[o]^e[o];return n===0}function Y(t,e,n){let o=e,r=n||(()=>[]),i=(c,s)=>o(s,...r(c)).update(c).digest(),f=o(new Uint8Array(t),...r(new Uint8Array(0)));return i.outputLen=f.outputLen,i.blockLen=f.blockLen,i.create=(c,...s)=>o(c,...s),i}var et=(t,e)=>{function n(o,...r){if(w(o,void 0,"key"),t.nonceLength!==void 0){let h=r[0];w(h,t.varSizeNonce?void 0:t.nonceLength,"nonce")}let i=t.tagLength,f=t.nonceLength!==void 0?1:0;if(!t.withAAD){for(let h=f;h<r.length;h++)if(W(r[h]))throw new Error("AAD not supported")}t.withAAD&&r[f]!==void 0&&w(r[f],void 0,"AAD");let c=e(o,...r),s=(h,l)=>{if(l!==void 0){if(h!==2)throw new Error("cipher output not supported");w(l,void 0,"output")}},u=!1;return{encrypt(h,l){if(u)throw new Error("cannot encrypt() twice with same key + nonce");return u=!0,w(h,void 0,"data"),s(c.encrypt.length,l),c.encrypt(h,l)},decrypt(h,l){if(w(h,void 0,"data"),i&&h.length<i)throw new Error('"ciphertext" expected length >= tagLength='+i);return s(c.decrypt.length,l),c.decrypt(h,l)}}}return Object.assign(n,t),n};function nt(t,e,n=!0){if(e===void 0)return new Uint8Array(t);if(w(e,t,"output"),n&&!I(e))throw new Error("invalid output, must be aligned");return e}function ot(t,e,n){N(t),N(e),xt(n);let o=new Uint8Array(16),r=v(o);return r.setBigUint64(0,BigInt(e),n),r.setBigUint64(8,BigInt(t),n),o}function I(t){return t.byteOffset%4===0}function B(t){return Uint8Array.from(w(t))}var T=16,rt=new Uint8Array(16),k=U(rt),mt=225,Lt=(t,e,n,o)=>{let r=o&1;return{s3:n<<31|o>>>1,s2:e<<31|n>>>1,s1:t<<31|e>>>1,s0:t>>>1^mt<<24&-(r&1)}},P=t=>(t>>>0&255)<<24|(t>>>8&255)<<16|(t>>>16&255)<<8|t>>>24&255|0;var Ut=t=>t>64*1024?8:t>1024?4:2,Z=class{constructor(e,n){b(this,"blockLen",T);b(this,"outputLen",T);b(this,"s0",0);b(this,"s1",0);b(this,"s2",0);b(this,"s3",0);b(this,"finished",!1);b(this,"destroyed",!1);b(this,"t");b(this,"W");b(this,"windowSize");w(e,16,"key"),e=B(e);let o=v(e),r=o.getUint32(0,!1),i=o.getUint32(4,!1),f=o.getUint32(8,!1),c=o.getUint32(12,!1),s=[];for(let g=0;g<128;g++)s.push({s0:P(r),s1:P(i),s2:P(f),s3:P(c)}),{s0:r,s1:i,s2:f,s3:c}=Lt(r,i,f,c);let u=Ut(n||1024);if(![1,2,4,8].includes(u))throw new Error("ghash: invalid window size, expected 2, 4 or 8");this.W=u;let h=128/u,l=this.windowSize=2**u,y=[];for(let g=0;g<h;g++)for(let d=0;d<l;d++){let x=0,E=0,A=0,S=0;for(let m=0;m<u;m++){if(!(d>>>u-m-1&1))continue;let{s0:$,s1:ut,s2:ht,s3:lt}=s[u*g+m];x^=$,E^=ut,A^=ht,S^=lt}y.push({s0:x,s1:E,s2:A,s3:S})}this.t=y}_updateBlock(e,n,o,r){e^=this.s0,n^=this.s1,o^=this.s2,r^=this.s3;let{W:i,t:f,windowSize:c}=this,s=0,u=0,a=0,h=0,l=(1<<i)-1,y=0;for(let g of[e,n,o,r])for(let d=0;d<4;d++){let x=g>>>8*d&255;for(let E=8/i-1;E>=0;E--){let A=x>>>i*E&l,{s0:S,s1:m,s2:_,s3:$}=f[y*c+A];s^=S,u^=m,a^=_,h^=$,y+=1}}this.s0=s,this.s1=u,this.s2=a,this.s3=h}update(e){R(this),w(e),e=B(e);let n=U(e),o=Math.floor(e.length/T),r=e.length%T;for(let i=0;i<o;i++)this._updateBlock(p(n[i*4+0]),p(n[i*4+1]),p(n[i*4+2]),p(n[i*4+3]));return r&&(rt.set(e.subarray(o*T)),this._updateBlock(p(k[0]),p(k[1]),p(k[2]),p(k[3])),L(k)),this}destroy(){this.destroyed=!0;let{t:e}=this;for(let n of e)n.s0=0,n.s1=0,n.s2=0,n.s3=0}digestInto(e){R(this),H(e,this),this.finished=!0;let{s0:n,s1:o,s2:r,s3:i}=this,f=U(e);f[0]=n,f[1]=o,f[2]=r,f[3]=i,C||O(f.subarray(0,T/4))}digest(){let e=new Uint8Array(T);return this.digestInto(e),this.destroy(),e}};var G=Y(16,(t,e)=>new Z(t,e),t=>[t.length]);var J=16,Bt=4,j=new Uint8Array(J);var Tt=283;function St(t){if(![16,24,32].includes(t.length))throw new Error('"aes key" expected Uint8Array of length 16/24/32, got length='+t.length)}function Q(t){return t<<1^Tt&-(t>>7)}function st(t,e){let n=0;for(;e>0;e>>=1)n^=t&-(e&1),t=Q(t);return n}var vt=(()=>{let t=new Uint8Array(256);for(let n=0,o=1;n<256;n++,o^=Q(o))t[n]=o;let e=new Uint8Array(256);e[0]=99;for(let n=0;n<255;n++){let o=t[255-n];o|=o<<8,e[t[n]]=(o^o>>4^o>>5^o>>6^o>>7^99)&255}return L(t),e})();var Ct=t=>t<<24|t>>>8,F=t=>t<<8|t>>>24;function It(t,e){if(t.length!==256)throw new Error("wrong sbox length");let n=new Uint32Array(256).map((u,a)=>e(t[a])),o=n.map(F),r=o.map(F),i=r.map(F),f=new Uint32Array(256*256),c=new Uint32Array(256*256),s=new Uint16Array(256*256);for(let u=0;u<256;u++)for(let a=0;a<256;a++){let h=u*256+a;f[h]=n[u]^o[a],c[h]=r[u]^i[a],s[h]=t[u]<<8|t[a]}return{sbox:t,sbox2:s,T0:n,T1:o,T2:r,T3:i,T01:f,T23:c}}var it=It(vt,t=>st(t,3)<<24|t<<16|t<<8|st(t,2));var _t=(()=>{let t=new Uint8Array(16);for(let e=0,n=1;e<16;e++,n=Q(n))t[e]=n;return t})();function Ot(t){w(t);let e=t.length;St(t);let{sbox2:n}=it,o=[];(!C||!I(t))&&o.push(t=B(t));let r=O(U(t)),i=r.length,f=s=>K(n,s,s,s,s),c=new Uint32Array(e+28);c.set(r);for(let s=i;s<c.length;s++){let u=c[s-1];s%i===0?u=f(Ct(u))^_t[s/i-1]:i>6&&s%i===4&&(u=f(u)),c[s]=c[s-i]^u}return L(...o),c}function D(t,e,n,o,r,i){return t[n<<8&65280|o>>>8&255]^e[r>>>8&65280|i>>>24&255]}function K(t,e,n,o,r){return t[e&255|n&65280]|t[o>>>16&255|r>>>16&65280]<<16}function ct(t,e,n,o,r){let{sbox2:i,T01:f,T23:c}=it,s=0;e^=t[s++],n^=t[s++],o^=t[s++],r^=t[s++];let u=t.length/4-2;for(let g=0;g<u;g++){let d=t[s++]^D(f,c,e,n,o,r),x=t[s++]^D(f,c,n,o,r,e),E=t[s++]^D(f,c,o,r,e,n),A=t[s++]^D(f,c,r,e,n,o);e=d,n=x,o=E,r=A}let a=t[s++]^K(i,e,n,o,r),h=t[s++]^K(i,n,o,r,e),l=t[s++]^K(i,o,r,e,n),y=t[s++]^K(i,r,e,n,o);return{s0:a,s1:h,s2:l,s3:y}}function V(t,e,n,o,r){w(n,J,"nonce"),w(o),r=nt(o.length,r);let i=n,f=U(i),c=v(i),s=U(o),u=U(r),a=e?0:12,h=o.length,l=c.getUint32(a,e);for(let g=0;g+4<=s.length;g+=4){let{s0:d,s1:x,s2:E,s3:A}=ct(t,p(f[0]),p(f[1]),p(f[2]),p(f[3]));u[g+0]=s[g+0]^p(d),u[g+1]=s[g+1]^p(x),u[g+2]=s[g+2]^p(E),u[g+3]=s[g+3]^p(A),l=l+1>>>0,c.setUint32(a,l,e)}let y=J*Math.floor(s.length/Bt);if(y<h){let{s0:g,s1:d,s2:x,s3:E}=ct(t,p(f[0]),p(f[1]),p(f[2]),p(f[3])),A=new Uint32Array([g,d,x,E]);O(A);let S=X(A);for(let m=y,_=0;m<h;m++,_++)r[m]=o[m]^S[_];L(A)}return r}function kt(t,e,n,o,r){let i=r?r.length:0,f=t.create(n,o.length+i);r&&f.update(r);let c=ot(8*o.length,8*i,e);f.update(o),f.update(c);let s=f.digest();return L(c),s}var ft=et({blockSize:16,nonceLength:12,tagLength:16,withAAD:!0,varSizeNonce:!0},function(e,n,o){if(n.length<8)throw new Error("aes/gcm: invalid nonce length");let r=16;function i(c,s,u){let a=kt(G,!1,c,u,o);for(let h=0;h<s.length;h++)a[h]^=s[h];return a}function f(){let c=Ot(e),s=j.slice(),u=j.slice();if(V(c,!1,u,u,s),n.length===12)u.set(n);else{let h=j.slice();v(h).setBigUint64(8,BigInt(n.length*8),!1);let y=G.create(s).update(n).update(h);y.digestInto(u),y.destroy()}let a=V(c,!1,u,j);return{xk:c,authKey:s,counter:u,tagMask:a}}return{encrypt(c){let{xk:s,authKey:u,counter:a,tagMask:h}=f(),l=new Uint8Array(c.length+r),y=[s,u,a,h];I(c)||y.push(c=B(c)),V(s,!1,a,c,l.subarray(0,c.length));let g=i(u,h,l.subarray(0,l.length-r));return y.push(g),l.set(g,c.length),L(...y),l},decrypt(c){let{xk:s,authKey:u,counter:a,tagMask:h}=f(),l=[s,u,h,a];I(c)||l.push(c=B(c));let y=c.subarray(0,-r),g=c.subarray(-r),d=i(u,h,y);if(l.push(d),!tt(d,g))throw L(...l),new Error("aes-gcm: invalid tag");let x=V(s,!1,a,y);return L(...l),x}}});return bt(Kt);})();
/*! Bundled license information:
@noble/ciphers/utils.js:
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
*/
Built with 1time
Built something on 1time?
Open an issue
and we will add it here.
Learn more |