# Hash Data

> 使用 SHA/MD5 对数据进行哈希。

Source: https://docs.sommatic.ai/zh/nodes/crypto.hash/

- 标识: `crypto.hash`
- 类别: 身份
- 版本: 2.0.0
- 状态: 可用
- 执行方式: sync

使用 SHA/MD5 对数据进行哈希。

## 配置

- `data` (any, 必填): Data to hash (objects are JSON-serialized)
- `algorithm` (string, 可选): 默认值: `sha256`. 可选值: `sha256`, `sha384`, `sha512`.

## 输出端口

- `out` (默认): 哈希后的字符串
- `error`: 哈希失败

## 输入契约

- `data` (any, 必填)
- `algorithm` (string, 可选)

## 输出契约

- `hash` (string, 可选): Hex 编码

## 在表达式中

后续节点可通过 `$("Hash Data").item.json.<路径>` 读取此节点输出中的内容（请把 `Hash Data` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Hash Data").item.json.hash` (string) = `"8c87b489ce35cf2e2f39f80e282cb2e804932a56a213…"`: Hex 编码


## 工作原理

计算值的十六进制 SHA-256、SHA-384 或 SHA-512 摘要。字符串按 UTF-8 计算；其他值先序列化为 JSON。

1. 解析配置；在 `data` 中使用纯表达式，例如 `{{ $json.email }}`。
2. 当 `data` 为 undefined、null 或空字符串时，以 `data is required` 从 `error` 输出。
3. 校验 `algorithm`（默认 `sha256`）是否为 `sha256`、`sha384` 或 `sha512`。
4. 非字符串值用 `JSON.stringify` 序列化，再对 UTF-8 文本计算摘要。
5. 从 `out` 返回 `{ hash }`（小写十六进制），并附带 `meta.crypto`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-hash-email",
  "label": "Hash customer email",
  "operator_slug": "crypto.hash",
  "operator_version": "2.0.0",
  "config": {
    "data": "{{ $json.email }}",
    "algorithm": "sha256"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "hash": "8c87b489ce35cf2e2f39f80e282cb2e804932a56a213983eeeb428407d43b52d"
  },
  "meta": {
    "crypto": {
      "op": "hash",
      "key_source": null,
      "alias": null,
      "credential_id": null,
      "algorithm": "sha256",
      "ok": true
    }
  }
}
```

## 输出端口 (代码中的行为)

### `out`

摘要已计算。

```json
{
  "hash": "8c87b489ce35cf2e2f39f80e282cb2e804932a56a213983eeeb428407d43b52d"
}
```

### `error`

缺少 `data`，或算法不受支持。

```json
{
  "error": "Unsupported hash algorithm: md5"
}
```

## 代码中的行为

- `data`: 对象通过 `JSON.stringify` 序列化，键的顺序会影响结果：`{"a":1,"b":2}` 与 `{"b":2,"a":1}` 的摘要不同。跨系统比较摘要时，请先规范化该值。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `data is required` | `error` | `data` 为空，或其表达式未解析出任何值。`0` 和 `false` 是合法值。 |
| `Unsupported hash algorithm: <algorithm>` | `error` | 仅支持 `sha256`、`sha384` 和 `sha512`。 |

## 说明与限制

- 该摘要不带密钥：相同输入始终得到相同结果，适合做相等性比较和假名化键。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "crypto.hash",
  "name": "Hash Data",
  "category": "identity",
  "version": "2.0.0",
  "description": "Hashes data with SHA-256, SHA-384 or SHA-512 (hex).",
  "capabilities": [
    "crypto",
    "hash"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "data": {
        "description": "Data to hash (objects are JSON-serialized)"
      },
      "algorithm": {
        "type": "string",
        "default": "sha256",
        "enum": [
          "sha256",
          "sha384",
          "sha512"
        ]
      }
    },
    "required": [
      "data"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "data": {},
      "algorithm": {
        "type": "string"
      }
    },
    "required": [
      "data"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "hash": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Hash",
      "description": "Hashed string",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Hash failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard"
  ],
  "tags": [
    "crypto",
    "security"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_veripass",
    "profile": {
      "name": "Veripass",
      "verified": true,
      "url": "https://me.veripass.com/veripass",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "data": {
      "description": "Data to hash (objects are JSON-serialized)"
    },
    "algorithm": {
      "type": "string",
      "default": "sha256",
      "enum": [
        "sha256",
        "sha384",
        "sha512"
      ]
    }
  },
  "required": [
    "data"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "data": {},
    "algorithm": {
      "type": "string"
    }
  },
  "required": [
    "data"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "hash": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Hash",
    "description": "Hashed string",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Hash failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
