MockScrypt.java revision 76973437e9cb36eb60fb2a374366713307688136
1/* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.locksettings.recoverablekeystore; 18 19import static org.junit.Assert.assertEquals; 20 21import android.security.Scrypt; 22 23import java.nio.ByteBuffer; 24import java.nio.ByteOrder; 25import java.security.MessageDigest; 26import java.security.NoSuchAlgorithmException; 27 28public class MockScrypt extends Scrypt { 29 30 @Override 31 public byte[] scrypt(byte[] password, byte[] salt, int n, int r, int p, int outLen) { 32 assertEquals(32, outLen); 33 34 ByteBuffer byteBuffer = ByteBuffer.allocate( 35 password.length + salt.length + Integer.BYTES * 6); 36 byteBuffer.order(ByteOrder.LITTLE_ENDIAN); 37 byteBuffer.putInt(password.length); 38 byteBuffer.put(password); 39 byteBuffer.putInt(salt.length); 40 byteBuffer.put(salt); 41 byteBuffer.putInt(n); 42 byteBuffer.putInt(r); 43 byteBuffer.putInt(p); 44 byteBuffer.putInt(outLen); 45 46 try { 47 return MessageDigest.getInstance("SHA-256").digest(byteBuffer.array()); 48 } catch (NoSuchAlgorithmException e) { 49 // Should never happen 50 throw new RuntimeException(e); 51 } 52 } 53} 54