Code Monkey home page Code Monkey logo

store-password-safely's Introduction

store-password-safely

安全存储用户密码解决方案,HTTPS + SSL Pinning + sha256(FIXED_SALT + password) + bcrypt,以iOS + Node.js示例。

望能稍微重视下用户隐私和网络安全。

整体流程如下:

  • 实现HTTPS服务。
  • 客户端实现SSL Pinning。
  • 客户端对用户输入的password进行哈希运算,sha256(FIXED_SALT + password)。
  • 服务端使用Bcrypt算法存储、验证用户输入。

HTTPS:

创建SSL证书,以自签名为例

openssl genrsa -out privatekey.pem 1024
openssl req -new -key privatekey.pem -out certrequest.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

启动HTTPS服务

var options = {
	key: fs.readFileSync('keys/privatekey.pem'),
	cert: fs.readFileSync('keys/certificate.pem')
};

var app = express();

app.get('/', function (req, res) {
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ name: 'test' }));
});

var httpsServer = https.createServer(options, app);
httpsServer.listen(8080);

如果OS X 下端口被占用

lsof -i:8080
kill -9 PID

SSL Pinning:

通过SSL Pinning提高iOS应用的安全性

iOS安全系列之一:HTTPS

从服务端获取SSL Pinning需要的证书:

openssl s_client -connect 127.0.0.1:8080 </dev/null 2>/dev/null | openssl x509 -outform DER > myserver.cer

使用AFNetworking:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"myserver" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
securityPolicy.pinnedCertificates = @[certData];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesDomainName = NO;

manager.securityPolicy = securityPolicy;

sha256(FIXED_SALT + password):

NSHash

客户端对用户密码进行哈希运算:

NSString *password = @"123";
password = [[@"FIXED_SALT" stringByAppendingString:password] SHA256];

bcrypt:

bcrypt-nodejs

服务端使用Bcrypt算法处理

bcrypt.hash("bacon", null, null, function(err, hash) {
    // Store hash in your password DB.
});
 
// Load hash from your password DB.
bcrypt.compare("bacon", hash, function(err, res) {
    // res == true
});
bcrypt.compare("veggies", hash, function(err, res) {
    // res = false
});

License

WTFPL (Do What The Fuck You Want To Public License).

http://www.wtfpl.net

store-password-safely's People

Contributors

kaizhang890 avatar zhangkai89 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.