不要将机密写入代码
安全存储凭据,而不是以明文保存
不要将机密写入代码 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Counts as a Secret
WiFi passwords, API tokens, and private keys are all secrets. Anything that grants access must never sit in plain sight inside your sketch. 🔑
The Danger of Plaintext Keys
Flash memory can be read back over the wire. A plaintext key in your code is one chip dump away from being copied by anyone.
String apiKey = "AKIA12345SECRET"; // visible to anyone who reads the flashWhy Git Makes It Worse
Commit a secret once and it lives in your history forever. Even after you delete it, the old commit still holds the exposed key.
Use a Separate Header
Put credentials in a small file like secrets.h kept out of version control. This secrets header keeps keys off GitHub while your sketch stays shareable.
#include "secrets.h" // holds WIFI_SSID and WIFI_PASS, never committedIgnore It in Git
Add your secrets file to .gitignore so it can never be committed by accident. This single line prevents most real-world key leaks.
# .gitignore
secrets.hProvision at Setup Time
Instead of baking keys in, enter them once when the device first boots. Provisioning via a config page keeps secrets off your source entirely.
Store in NVS or EEPROM
The ESP32 offers NVS, a key-value store in flash for runtime secrets. You write the token once and read it back on every boot.
preferences.begin("net", false);
preferences.putString("token", userToken);One Device, One Key
Give each unit its own credentials, never a shared master key. With per-device keys, one stolen device can be revoked without touching the rest.
Rotate and Revoke
Plan to change keys on a schedule and kill leaked ones fast. Rotation limits how long any single stolen secret stays useful.
Least Privilege
Give each token only the access it truly needs. Least privilege means a leaked device key can read its own topic, not your whole account.
Scan Before You Push
Tools can catch a key before it reaches a remote repo. A secret scanner in your workflow blocks accidental leaks before they happen.
Quick Check
One of these keeps a key off your public repo.
Recap
Never bake secrets into code. Use a gitignored header, provision at setup, store in NVS, and keep per-device keys you can rotate. 🔐
常见问题解答
「不要将机密写入代码」课时是免费的吗?
是的 — 「不要将机密写入代码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「不要将机密写入代码」这节课中我会学到什么?
安全存储凭据,而不是以明文保存 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Arduino & IoT Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「不要将机密写入代码」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?
能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 物联网常见攻击面
- 不要将机密写入代码
- 使用 TLS 加密并验证证书
- 签名并锁定固件